【问题标题】:Javascript AES encryption doesn't match iOS AES encryptionJavascript AES 加密与 iOS AES 加密不匹配
【发布时间】:2015-11-01 00:04:17
【问题描述】:

我正在像这样在 iOS 中加密一个NSString,它可以很好地编码和解码:

NSString *stringtoEncrypt = @"This string is to be encrypted";
NSString *key = @"12345678901234567890123456789012";

// Encode
NSData *plain = [stringtoEncrypt dataUsingEncoding:NSUTF8StringEncoding];
NSData *cipher = [plain AES256EncryptWithKey:key];

NSString *cipherBase64 = [cipher base64EncodedString];
NSLog(@"ciphered base64: %@", cipherBase64);

// Decode
NSData *decipheredData = [cipherBase64 base64DecodedData];
NSString *decoded = [[NSString alloc] initWithData:[decipheredData AES256DecryptWithKey:key] encoding:NSUTF8StringEncoding];
NSLog(@"%@", decoded);

NSData 扩展:

- (NSData *)AES256EncryptWithKey:(NSString *)key
{
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    NSUInteger dataLength = [self length];

    //See the doc: For block ciphers, the output size will always be less than or
    //equal to the input size plus the size of one block.
    //That's why we need to add the size of one block here
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);

    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL /* initialization vector (optional) */,
                                          [self bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        //the returned NSData takes ownership of the buffer and will free it on deallocation
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }

    free(buffer); //free the buffer;
    return nil;
}

我可以成功地将生成的 Base64 字符串传递给 Node.js 并让它解码消息。我还需要用Javascript 编写的相同编码方法。 这是我目前所拥有的:

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
...
var text = "This string is to be encrypted";
var key = "12345678901234567890123456789012";
var iv  = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00';
var encrypted = CryptoJS.AES.encrypt(text, key, {iv: iv});
console.log("Base64 encoded: " + window.btoa(encrypted.ciphertext));

但是,生成的 Base64 字符串与 iOS 生成的字符串不匹配。 有什么想法吗?

【问题讨论】:

  • 我正在转储到字符串进行比较,但猜想那是以自己的方式搞砸了。
  • 加密是基于数据的,而不是基于字符的。转储必须是数据转储,这在脚本语言中会稍微困难一些。

标签: javascript objective-c encryption encoding cryptojs


【解决方案1】:

当您将字符串作为密钥传递时,CryptoJS 使用与 OpenSSL 兼容的基于密码的加密。由于您已经拥有完整的密钥和 IV,您需要将它们转换为 CryptoJS 的原生类型,即 WordArray:

var key = CryptoJS.enc.Utf8.parse("12345678901234567890123456789012");
var iv  = CryptoJS.lib.WordArray.create([0, 0, 0, 0]); // each number is a word of 32 bit

通过在 WordArray 对象上调用 btoa(),您将强制它进行字符串化。为此使用默认的十六进制编码。之后btoa() 将这个十六进制编码的字符串编码为 Bas64,这使得它更加膨胀。

您可以直接将 WordArray 编码为 Base64:

encrypted.ciphertext.toString(CryptoJS.enc.Base64)

【讨论】:

  • 非常感谢。我确实必须进行更改才能使其正常工作。我不得不将CryptoJS.lib.WordArray([0, 0, 0, 0]); 更改为CryptoJS.lib.WordArray.create([0, 0, 0, 0]);
  • 另外,var encrypted = CryptoJS.AES.encrypt(text, key, {iv: iv}); console.log("Encrypted: " + encrypted); 似乎输出了与我从 iOS 获得的相同的 Base64 字符串,即使没有告诉它对它进行 Base64 编码,这似乎很奇怪?
  • CryptoJS.AES.encrypt() 结果不是 WordArray,默认使用 OpenSSLFormatter。在其上调用toString() 会将其序列化为Base64 格式。如果没有使用盐,那么它应该匹配encrypted.ciphertext.toString(CryptoJS.enc.Base64),但我认为也有例外。不太确定。
  • 感谢您的解释
猜你喜欢
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
  • 2013-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-01
  • 2016-06-25
相关资源
最近更新 更多