节点。js AES对iOS加密的NSString进行解密

时间:2022-04-12 14:32:17

I am encrypting an NSString in iOS like this which encodes and decodes fine:

我正在对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 extension:

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;
}

Now I am wanting to pass the Base64 encoded string to Node.js and have it decode. I am using this method:

现在我想把Base64编码的字符串传递给Node。把它解码。我正在使用这种方法:

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);

However the results is:

然而结果是:

Decrypted content: dXYCCDBY^WYCDo be encrypted

解密内容:dXYCCDBY ^ WYCDo被加密

Any idea's what's going on?

有什么想法吗?

1 个解决方案

#1


3  

In Objective-C you're not defining the IV which defaults to a zero filled IV. Node.js says that

在Objective-C中,你没有定义IV默认为0填充的IV。Node。js说

key and iv must be 'binary' encoded strings or buffers.

密钥和iv必须是二进制编码的字符串或缓冲区。

The character 0 in your IV string is not the same as the byte \0. You're not passing a zero filled IV, but an IV filled with 0x30 bytes.

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

Fill the IV like this:

像这样填写IV:

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

#1


3  

In Objective-C you're not defining the IV which defaults to a zero filled IV. Node.js says that

在Objective-C中,你没有定义IV默认为0填充的IV。Node。js说

key and iv must be 'binary' encoded strings or buffers.

密钥和iv必须是二进制编码的字符串或缓冲区。

The character 0 in your IV string is not the same as the byte \0. You're not passing a zero filled IV, but an IV filled with 0x30 bytes.

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

Fill the IV like this:

像这样填写IV:

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