openssl_private_encrypt() returns false with output of 0

前端 未结 2 1203
-上瘾入骨i
-上瘾入骨i 2021-01-26 19:14

I am trying to use the PHP function openssl_private_encrypt() to encrypt an uploaded file prior to saving it (see code snippet below), however it\'s bool is returning false and

相关标签:
2条回答
  • 2021-01-26 19:21

    I'm not sure about PHP but in C/C++(OpenSSL) asymmetric encryption(RSA mainly) works on data with length less than the key size. And normally it is used to encrypt hash values. If you want to encrypt large(more the ~256 bytes)amount of data you'd better use some symmetric(block) cipher like AES or TriDES. Symmetric ciphers are much faster by the way.

    PS Sorry I don't have enough reputation to put this post into comments.

    0 讨论(0)
  • 2021-01-26 19:23

    You should proper initialize private key (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);
    
    0 讨论(0)
提交回复
热议问题