【问题标题】:Node.js AES decrypt an iOS encrypted NSStringNode.js AES 解密 iOS 加密的 NSString
【发布时间】:2015-10-31 22:36:54
【问题描述】:

我正在像这样在 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 并对其进行解码。 我正在使用这种方法:

var crypto = require('crypto');

password = '12345678901234567890123456789012';
var cryptoStr = 'q6SIYHKospVNzk5ZsW8S5CURQ8qRPyDhv1TqALXhOVM=';
var iv = "0000000000000000";

var decipher = crypto.createDecipheriv('aes-256-cbc', password, iv);
var dec = decipher.update(cryptoStr,'base64','utf-8');
dec += decipher.final('utf-8'); 

console.log('Decrypted content: ' + dec);

但是结果是:

解密内容:dXYCCDBY^WYC做加密

知道发生了什么吗?

【问题讨论】:

    标签: javascript objective-c node.js encryption


    【解决方案1】:

    在 Objective-C 中,您没有定义默认为零填充 IV 的 IV。 Node.js says 那个

    keyiv 必须是“二进制”编码的字符串或缓冲区。

    IV 字符串中的字符0 与字节\0 不同。您传递的不是一个零填充的 IV,而是一个填充了 0x30 字节的 IV。

    像这样填写 IV:

    var iv = new Buffer(16);
    iv.fill(0);
    

    【讨论】:

    猜你喜欢
    • 2011-12-10
    • 2011-02-18
    • 2012-09-28
    • 1970-01-01
    • 2013-06-22
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多