【发布时间】: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.
@zaph. R-; 0040 f6 a1 46 5a ec 54 55 54 04 8f 00 60 09 cf af c2 FZTUT ... -
加密数据($代码)是: q7zq1SIaVt3rrTAXlsiEbRbIwx / MB / FxG2PzcYUYsLjRm / O + 36Cjw0XSYnbczJRThayIdxFg1hK0UtvSLem9O / ahRlrsVFVUBI8AYAnPr8I = 该明文必须是: a18ac4e6fbd3fc024a07a21dafbac37d828ca8a04a0e34f368f1ec54e0d4fffb B> @扎夫
-
请向问题添加其他信息,而不是向 cmets。 cmets 也基本上具有零格式化功能。您可以编辑自己的问题。
-
评论中的
$code基于其长度是无效的Base64编码,109个字符,其中有一个不可见的字符。纯文本是 64 个字符,将用 16 个 0x10 字节填充,这样就变成了 80 个字节。
标签: android ios encryption aes php-openssl