mbedtls 和 openssl 之间不同的 AES-256 加密输出
Different AES-256 encryption output between mbedtls and openssl
我正在开发一个需要通过 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)
= void
(gdb) p sprintf(decrypt_output, "hello world")
= 11
(gdb) p/x key
= {0x62, 0x61, 0x75, 0x73, 0x63, 0x68, 0x0 <repeats 26 times>}
(gdb) p mbedtls_aes_setkey_enc(&aes, key, 256)
= 0
(gdb) p mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, decrypt_output, decrypt_output)
= 0
(gdb) p/x decrypt_output
= {0x1b, 0x7c, 0x4d, 0x41, 0xaf, 0xa4, 0x65, 0x7f, 0x56, 0x39, 0x95, 0x2a, 0x21, 0x32, 0x10, 0xab}
(gdb)
以下 openssl
命令产生与您的 mbedtls 脚本相同的结果:
echo -ne "hello world[=10=][=10=][=10=][=10=][=10=]" | openssl aes-256-ecb -nopad -K 6261757363680000000000000000000000000000000000000000000000000000 | xxd -p
产生:
1b7c4d41afa4657f5639952a213210ab
请注意,输入字符串的长度最多为 16 个字符(即一个 AES 块的长度),使用空 ([=13=]
) 字符,以匹配 mbedtls 默认情况下的操作(-e
选项需要 echo
以允许输入中的空字符)。此外,-n
选项与 echo
一起使用,因此 echo
不会在输入中附加换行符。最后,-nopad
选项与 openssl
命令一起使用,因此 openssl
不会添加额外的 pkcs#7 填充块。
我正在开发一个需要通过 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)
= void
(gdb) p sprintf(decrypt_output, "hello world")
= 11
(gdb) p/x key
= {0x62, 0x61, 0x75, 0x73, 0x63, 0x68, 0x0 <repeats 26 times>}
(gdb) p mbedtls_aes_setkey_enc(&aes, key, 256)
= 0
(gdb) p mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, decrypt_output, decrypt_output)
= 0
(gdb) p/x decrypt_output
= {0x1b, 0x7c, 0x4d, 0x41, 0xaf, 0xa4, 0x65, 0x7f, 0x56, 0x39, 0x95, 0x2a, 0x21, 0x32, 0x10, 0xab}
(gdb)
以下 openssl
命令产生与您的 mbedtls 脚本相同的结果:
echo -ne "hello world[=10=][=10=][=10=][=10=][=10=]" | openssl aes-256-ecb -nopad -K 6261757363680000000000000000000000000000000000000000000000000000 | xxd -p
产生:
1b7c4d41afa4657f5639952a213210ab
请注意,输入字符串的长度最多为 16 个字符(即一个 AES 块的长度),使用空 ([=13=]
) 字符,以匹配 mbedtls 默认情况下的操作(-e
选项需要 echo
以允许输入中的空字符)。此外,-n
选项与 echo
一起使用,因此 echo
不会在输入中附加换行符。最后,-nopad
选项与 openssl
命令一起使用,因此 openssl
不会添加额外的 pkcs#7 填充块。