ios – AES256加密在node.js和objective-c之间得到不同的结果

1.Node.js

var crypto = require('crypto');
    var key = "my password";
    var text = "text to encrypt";  
    var cipher = crypto.createCipher('aes-256-cbc',key);  
    var crypted =cipher.update(text,'utf8','base64');
    crypted+=cipher.final('base64');

结果:ZeYCYOrR / w7qSAZVYht8 Q ==

.目的-C

{
    NSString *key = @"my password";
    NSString *text = @"text to encrypt";
    NSData *plain = [secret dataUsingEncoding:NSUTF8StringEncoding];
    NSData *cipher = [plain AES256EncryptWithKey:key];
    NSLog(@"%@\n", [cipher base64Encoding] );
}

结果:raFGdTWYvSPWpkgtF9LJIg ==

[AES256EncryptWithKey:]是HERE

问题是node.js crypto.createCipher在内部使用密钥派生函数EVP_BytesToKey()从key =“my password”生成AES密钥和iv.因此,对于node.js和Common Crypto,实际的AES密钥是不同的.

答案是使用crypto.createCipheriv(algorithm,key,iv)代替crypto.createCipher(算法,密码).

从node.js文档:

In line with OpenSSL’s recommendation to use pbkdf2 instead of EVP_BytesToKey it is recommended you derive a key and iv yourself with crypto.pbkdf2 and to then use createCipheriv() to create the cipher stream.

相关文章
相关标签/搜索