在 iOS 上加密并在 javascript AES 中解密

问题描述 投票:0回答:0

我在之前的iOS项目中对一组字符串(比如aD+pXgpjOdT6LSqmcAJ8MQ==可以解密为41)使用base64+AES加密解密。现在我想使用 CryptoJS 将它移植到 JS,但我无法得到正确的结果。问题出在哪里?

Objective-C 中的原始代码

=====NSData+AES.m========

#import <CommonCrypto/CommonCryptor.h>

#define key @"HxBaDjdHjuHUf7CVsqhmGK319V7pBLUZ"
static Byte ivBuff[]   = {0xA,1,81,5,4,9,7,0x17,3,1,0xD,6,9,0xC,0xF,0xB};

@implementation NSData (aes)

- (NSData *) cryptOperation:(CCOperation)operation
{
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keys[kCCKeySizeAES256 + 1];
    [key getCString:keys maxLength:sizeof(keys) encoding:NSUTF8StringEncoding];
    // Perform PKCS7Padding on the key.
    unsigned long bytes_to_pad = sizeof(keys) - [key length];
    if (bytes_to_pad > 0)
    {
        char byte = bytes_to_pad;
        for (unsigned long i = sizeof(keys) - bytes_to_pad; i < sizeof(keys); i++)
            keys[i] = byte;
    }
    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 numBytesDecrypted = 0;
    CCCryptorStatus status = CCCrypt(operation, kCCAlgorithmAES128,
                                 kCCOptionPKCS7Padding,
                                 keys, kCCKeySizeAES256,
                                 ivBuff,
                                 [self bytes], dataLength, /* input */
                                 buffer, bufferSize, /* output */
                                 &numBytesDecrypted);
    if (status == kCCSuccess)
    {
        //the returned NSData takes ownership of buffer and will free it on dealloc
        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
    }
    free(buffer); //free the buffer;
    return nil;
}

- (NSData *)AES256Encrypt
{
    return [self cryptOperation:kCCEncrypt];
}

- (NSData *)AES256Decrypt
{
    return [self cryptOperation:kCCDecrypt];
}

=============Utility.m======================

+(NSString *)decrypt:(NSString *)string
{
    if([string containsString:@"\n"] || [string containsString:@"\t"])
    {
        string = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@""];
        string = [string stringByReplacingOccurrencesOfString:@"\t" withString:@""];
    }
    NSData *keyData = [[NSData alloc] initWithBase64EncodedString:string options:0];
    NSData *dataDecrypted = [keyData AES256Decrypt];
    NSString *receivedDataDecryptString = [[NSString alloc]initWithData:dataDecrypted encoding:NSUTF8StringEncoding];
    return receivedDataDecryptString;
}

这是我写的 JavaScript 代码:

    var password = "HxBaDjdHjuHUf7CVsqhmGK319V7pBLUZ";
    var origCipher = CryptoJS.enc.Base64.parse(string);
    var origIV = [0xA,1,81,5,4,9,7,0x17,3,1,0xD,6,9,0xC,0xF,0xB];
    
    var key = CryptoJS.lib.WordArray.create(password);
    var iv = CryptoJS.lib.WordArray.create(origIV);
    //var cipher = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Hex.parse(origCipher));
    
    var plain = CryptoJS.AES.decrypt(origCipher, key, { iv: iv });
    var plaintext = CryptoJS.enc.UTF8.stringify(plain);

如果字符串为 'aD+pXgpjOdT6LSqmcAJ8MQ==',结果为 null 而不是 41

objective-c aes cryptojs
© www.soinside.com 2019 - 2024. All rights reserved.