【问题标题】:PHP - Openssl_decrypt error : wrong final block length (AES 256 CBC mode encryption/decryption)PHP - Openssl_decrypt 错误:错误的最终块长度(AES 256 CBC 模式加密/解密)
【发布时间】:2016-05-10 19:12:18
【问题描述】:

这是我的代码:

 function decrypt($code)
{

    $key = '3552ef55ecdf04324..'; // 64 bytes length
    $iv = 'd20818af907b59c3b15d258dd3969770'; // 32 bytes length
    $key = hash("sha256", $key,true); // 32 bytes length
    $iv = md5($iv,true); // 16 bytes length

    echo strlen(base64_decode($code)); // 80 bytes
   //return openssl_decrypt(base64_decode($code), 'aes-256-cbc', $key, 0 ,$iv); // return false
    $output = openssl_decrypt(base64_decode($code), 'aes-256-cbc', $key, 0 ,$iv);
    return openssl_error_string(); 

}

我使用 swift/android 加密,我使用 php 解密。

openssl_error_string() 方法返回“error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length”。

注意加密swift/android中的key和iv是一样的。 我在这里找不到问题。任何人?谢谢。

【问题讨论】:

  • 您是否指定了 PKCS#7(née PKCS#5)填充?加密数据是块长度的倍数(AES 为 16 字节)吗?我总是建议不要组合步骤,这会使调试更加困难。十六进制转储加密数据(base64_decode($code)),可能不是你想的那样。
  • 我正在指定 PKCS#7 填充。加密数据是块长度(80 字节)的倍数。这是十六进制转储:
    0000 ab bc ea d5 22 1a 56 dd eb ad 30 17 96 c8 84 6d ".V 0.m 0010 16 c8 c3 1f e6 6f f1 71 1b 63 f3 71 85 18 b0 b8 。 .oq .cq。 0020 d1 9b fa 3e df a0 a3 c3 45 d2 62 76 dc cc 94 53 > EbvS 0030 85 ac 88 77 11 60 d6 12 b4 52 db d2 2d e9 bd 3b w.. R-; 0040  f6 a1 46 5a ec 54 55 54  04 8f 00 60 09 cf af c2   FZTUT ...
    @zaph
  • 加密数据($代码)是: q7zq1SIaVt3rrTAXlsiEbRbIwx / MB / FxG2PzcYUYsLjRm / O + 36Cjw0XSYnbczJRThayIdxFg1hK0UtvSLem9O / ahRlrsVFVUBI8AYAnPr8I = 该明文必须是: a18ac4e6fbd3fc024a07a21dafbac37d828ca8a04a0e34f368f1ec54e0d4fffb B> @扎夫
  • 请向问题添加其他信息,而不是向 cmets。 cmets 也基本上具有零格式化功能。您可以编辑自己的问题。
  • 评论中的$code基于其长度是无效的Base64编码,109个字符,其中有一个不可见的字符。纯文本是 64 个字符,将用 16 个 0x10 字节填充,这样就变成了 80 个字节。

标签: android ios encryption aes php-openssl


【解决方案1】:

我解决了这个问题。事实上,我是 Android 端的 URLEncoding 参数,然后用我的 PHP 脚本对它们进行 URLDecoding。

不幸的是,'+' 的 URL 解码在 Android 中是一个空格,但在 ios 中它是正确的 ('+')。

所以在 PHP 方面,我在解码之前用“+”替换了空格字符。 我删除了 base64_decode 函数。

更新代码:

  function decrypt($code)
{

$key = '3552ef55ecdf04324d0fe72343...';
$iv  = 'd20818af907b59c3b15d258dd3969770';

$key = hash("sha256", $key, true);
$iv  = md5($iv, true);
if (preg_match('/\s/', trim($code))) {
    $code = str_replace(' ', '+', trim($code));
}

$output = openssl_decrypt($code, 'aes-256-cbc', $key, 0, $iv);
return $output;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 2018-09-09
    • 2015-06-30
    • 2019-04-14
    • 2014-05-31
    相关资源
    最近更新 更多