【问题标题】:Trying to decrypt with aes-256-gcm with php尝试用 php 用 aes-256-gcm 解密
【发布时间】:2019-02-27 20:12:48
【问题描述】:

我想知道是否有人可以提供帮助,

我使用的是aes-256-gcm加密方式,可以加密,但不能解密。

下面是我的代码,谁能看出我哪里出错了

$textToDecrypt = $_POST['message'];
$password = '3sc3RLrpd17';
$method = 'aes-256-gcm'; 
$tag_length = 16;
$password = substr(hash('sha256', $password, true), 0, 32);
$iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
$decrypted = openssl_decrypt(base64_decode($textToDecrypt), $method, 
$password, OPENSSL_RAW_DATA, $iv, $tag_length);

加密代码

$textToEncrypt = $_POST['message'];
$password = '3sc3RLrpd17';
$method = 'aes-256-gcm'; 
$tag_length = 16;


$password = substr(hash('sha256', $password, true), 0, 32);



$iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . 
chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . 
chr(0x0) . chr(0x0) . chr(0x0);


$encrypted = base64_encode(openssl_encrypt($textToEncrypt, $method, 
$password, OPENSSL_RAW_DATA, $iv, $tag_length));

【问题讨论】:

    标签: php encryption aes-gcm


    【解决方案1】:

    您需要将 GCM tag (HMAC) 与密文一起保存,并将其传递给解密函数。它不会自动为您保存(您还应该生成一个好的 IV 并将其与密文一起存储)。

    openssl_encrypt 指定为:

    string openssl_encrypt ( string $data , string $method , string $key [, int $options = 0 [, string $iv = "" [, string &$tag = NULL [, string $aad = "" [, int $tag_length = 16 ]]]]] )

    如果你仔细观察,你会传入$tag_length,这里应该是$tag

    它应该是这样的:

    加密:

    $textToEncrypt = $_POST['message'];
    $password = '3sc3RLrpd17';
    $key = substr(hash('sha256', $password, true), 0, 32);
    $cipher = 'aes-256-gcm';
    $iv_len = openssl_cipher_iv_length($cipher);
    $tag_length = 16;
    $iv = openssl_random_pseudo_bytes($iv_len);
    $tag = ""; // will be filled by openssl_encrypt
    
    $ciphertext = openssl_encrypt($textToEncrypt, $cipher, $key, OPENSSL_RAW_DATA, $iv, $tag, "", $tag_length);
    $encrypted = base64_encode($iv.$ciphertext.$tag);
    

    解密:

    $textToDecrypt = $_POST['message'];
    $encrypted = base64_decode($textToDecrypt);
    $password = '3sc3RLrpd17';
    $key = substr(hash('sha256', $password, true), 0, 32);
    $cipher = 'aes-256-gcm';
    $iv_len = openssl_cipher_iv_length($cipher);
    $tag_length = 16;
    $iv = substr($encrypted, 0, $iv_len);
    $ciphertext = substr($encrypted, $iv_len, -$tag_length);
    $tag = substr($encrypted, -$tag_length);
    
    $decrypted = openssl_decrypt($ciphertext, $cipher, $key, OPENSSL_RAW_DATA, $iv, $tag);
    

    【讨论】:

    • 感谢您的解密,最后一行出现错误“警告:openssl_decrypt():AEAD 模式的 IV 长度设置失败”,我该如何纠正?
    • 这意味着你的$_POST['message'] 是空的。检查您是否确实获得了 $textToDecrypt 中的数据
    【解决方案2】:

    @Scribilicious 这里的问题是 $iv 和 $tag 是二进制的($encrypted 是 base64)。所以数据中可能有一个'::'。 所以这可能会更好。

    function encrypt($key, $data) {
        $encryptionKey = base64_decode($key);
        $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-gcm'));
        $encrypted = openssl_encrypt($data, 'aes-256-gcm', $encryptionKey, 0, $iv, $tag);
        return $encrypted . ':' . base64_encode($iv) . ':' . base64_encode($tag));
    }
    
    function decrypt($key, $data) {
        $encryptionKey = base64_decode($key);
        list($encryptedData, $iv, $tag) = explode(':', $data, 3);
        return openssl_decrypt($encryptedData, 'aes-256-gcm', $encryptionKey, 0, base64_decode($iv), base64_decode($tag));
    }
    
    $encrypt = encrypt('key', 'test data');
    $decrypt = decrypt('key', $encrypt);
    
    echo $encrypt . ' : ' . $decrypt;
    

    【讨论】:

      【解决方案3】:

      怎么样?

      function encrypt($key, $data) {
          $encryptionKey = base64_decode($key);
          $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-gcm'));
          $encrypted = openssl_encrypt($data, 'aes-256-gcm', $encryptionKey, 0, $iv, $tag);
          return base64_encode($encrypted . '::' . $iv . '::' . $tag);
      }
      
      
      function decrypt($key, $data) {
          $encryptionKey = base64_decode($key);
          list($encryptedData, $iv, $tag) = explode('::', base64_decode($data), 3);
          return openssl_decrypt($encryptedData, 'aes-256-gcm', $encryptionKey, 0, $iv, $tag);
      }
      
      $encrypt = encrypt('key', 'test data');
      $decrypt = decrypt('key', $encrypt);
      
      echo $encrypt . ' : ' . $decrypt;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-04
        • 1970-01-01
        • 2021-09-23
        • 1970-01-01
        • 2021-09-03
        • 1970-01-01
        • 2013-10-21
        相关资源
        最近更新 更多