【发布时间】:2021-11-07 12:37:59
【问题描述】:
我正在开发一个需要通过 mbedtls 解密文件的应用程序,该文件由 openssl 加密。目前,解密不起作用。经过调查,我发现我无法使用这两个框架创建相同的加密文件。两种加密方式有什么区别?
OpenSSL:
-> ✗ cat message
hello world
-> ✗ openssl aes-256-ecb -nosalt -K 6261757363680000000000000000000000000000000000000000000000000000 -in message -out koekoek.bin
-> ✗ xxd koekoek.bin
00000000: 68e1 1f1e 8397 a33e ddea 5c4d 3192 11ab h......>..\M1...
MbedTLS:
(gdb) p (void)memset(decrypt_output, 0, 16)
$63 = void
(gdb) p sprintf(decrypt_output, "hello world")
$64 = 11
(gdb) p/x key
$65 = {0x62, 0x61, 0x75, 0x73, 0x63, 0x68, 0x0 <repeats 26 times>}
(gdb) p mbedtls_aes_setkey_enc(&aes, key, 256)
$66 = 0
(gdb) p mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, decrypt_output, decrypt_output)
$67 = 0
(gdb) p/x decrypt_output
$68 = {0x1b, 0x7c, 0x4d, 0x41, 0xaf, 0xa4, 0x65, 0x7f, 0x56, 0x39, 0x95, 0x2a, 0x21, 0x32, 0x10, 0xab}
(gdb)
【问题讨论】:
-
您在
openssl中加密的数据(文件)包含字符h e l l o sp w o r l d和一个 NEWLINE, PLUS 默认情况下openssl enc添加 PKCS5/7 PADDING 到块边界(在这种情况下 4 个字节包含 04)。您需要使数据完全相同相同。 PS:对于这样的键,你可以说openssl enc -K 626175636368,它是零填充; OTOH,您首先不应该使用像这样的非常低熵的键。 -
在 Dave 的评论中不是很明确,但
mbed_tls默认不填充,而opensslCLI 默认使用 PKCS#7 兼容填充。所以你必须在mbed_tls中执行填充。
标签: encryption openssl mbedtls