【问题标题】:openssl_private_encrypt() returns false with output of 0openssl_private_encrypt() 返回 false,输出为 0
【发布时间】:2013-10-30 09:08:28
【问题描述】:

我正在尝试使用 PHP 函数 openssl_private_encrypt() 在保存上传文件之前对其进行加密(参见下面的代码 sn-p),但是它的 bool 返回 false 并且加密内容返回没有返回任何内容。没有显示或报告错误。

        $data = file_get_contents($_FILES['files']['tmp_name'][0]);

        openssl_private_encrypt($data,$encrypted,$key);

        $hash = sha1($encrypted);
        file_put_contents('/path/to/folder/'.$hash,$encrypted);

有人知道为什么这不起作用吗?

谢谢

【问题讨论】:

  • 你是如何初始化 $key 的?检查资源是否正确 (var_dump($key))
  • $key 被拉取为 file_get_contents('key.pem');

标签: php encryption openssl


【解决方案1】:

我不确定 PHP,但在 C/C++(OpenSSL)中,非对称加密(主要是 RSA)适用于长度小于密钥大小的数据。通常它用于加密哈希值。如果您想加密大量(更多约 256 字节)数据,您最好使用一些对称(块)密码,如 AES 或 TriDES。顺便说一下,对称密码要快得多。

PS 抱歉,我没有足够的声望将这篇文章放到 cmets 中。

【讨论】:

    【解决方案2】:

    你应该正确初始化私钥(http://pl1.php.net/manual/en/function.openssl-pkey-get-private.php

    $key = openssl_pkey_get_private ('file://path/to/file.pem');
    $data = file_get_contents($_FILES['files']['tmp_name'][0]);
    
    openssl_private_encrypt($data,$encrypted,$key);
    
    $hash = sha1($encrypted);
    file_put_contents('/path/to/folder/'.$hash,$encrypted);
    

    【讨论】:

      猜你喜欢
      • 2016-07-18
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 2012-05-06
      • 2021-02-20
      • 2019-05-23
      • 2016-03-11
      • 1970-01-01
      相关资源
      最近更新 更多