在 iOS 中解码 OpenSSL AES256 字符串
CLI
$ echo -n "TEST1" | openssl enc -aes256 -k FUUU -nosalt -a
bYbkQJcDFZt3y3UQEMbEeg==
iOS
NSString *leSYT = @"bYbkQJcDFZt3y3UQEMbEeg==";
NSData *data = [NSData dataFromBase64String:leSYT];
NSLog(@"%@",[data AES256DecryptWithKey:@"FUUU"]);
iOS 由于失败而不会输出任何内容。 我缺少什么?
NSData 添加: http://pastie.org/426530 // NSData+Base64 by Matt Gallagher
CLI
$ echo -n "TEST1" | openssl enc -aes256 -k FUUU -nosalt -a
bYbkQJcDFZt3y3UQEMbEeg==
iOS
NSString *leSYT = @"bYbkQJcDFZt3y3UQEMbEeg==";
NSData *data = [NSData dataFromBase64String:leSYT];
NSLog(@"%@",[data AES256DecryptWithKey:@"FUUU"]);
iOS doesn't output anything since it failed.
What am I missing?
NSData additions: http://pastie.org/426530 // NSData+Base64 by Matt Gallagher
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OpenSSL 的 enc 实用程序中的
-k
选项从您的密码“FUUU”中派生出 AES 密钥和 IV。您可以使用-p
选项让 OpenSSL 打印它派生的 AES256 密钥和 IV:AES256DecryptWithKey 需要 32 字节 AES 密钥,如注释所述:
但即使您转换密钥字符串从 OpenSSL 到字节字符串(不是 64 个 ASCII 字符。32 个字节),您仍然无法解密它并取回原始字符串。这是因为 OpenSSL 使用 IV,但 AES256DecryptWithKey 不是:(
看到为 IV 传递的 NULL 了吗?这对你不起作用)
所以你需要使用加密和解密方法,它们都使用相同的 AES 密钥和 IV为了这个工作。
The
-k
option in OpenSSL's enc utility derives an AES key and IV from your passphrase "FUUU". You can use the-p
option to have OpenSSL print the AES256 key and IV that it derived:AES256DecryptWithKey is expecting a 32-byte AES key, as the comments say:
But even if you convert the key string from OpenSSL to a string of bytes (not 64 ASCII characters. 32 bytes), you still won't be able to decrypt it and get your original string back. That's because OpenSSL is using an IV, but AES256DecryptWithKey is not:
(See the NULL being passed for the IV? That's not going to work for you)
So you need to use an encryption and decryption method that both use the same AES key and IV for this to work.