user2414445
user2414445

Reputation: 13

AES Descryption interopbility IOS and Python

I am trying to decrypt a file which is encrypted in Python using AES 256 CFB mode.

When I try to decrypt it in IOS it is not able decrypt it. giving some some junk string.

NSString * AES_Decrypt(NSString * key, NSString * cipherText,NSString *iv)
{
    @autoreleasepool {
    NSMutableData * mutableCipher = [[NSMutableData alloc] init];
    mutableCipher = (NSMutableData *)[cipherText dataUsingEncoding:NSUTF8StringEncoding];
    char  keyPtr[kCCKeySizeAES256+1];
    bzero( keyPtr, sizeof(keyPtr) );
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSASCIIStringEncoding];
    uint8_t iv[kCCKeySizeAES128+1];
    bzero((void *) iv, (size_t) sizeof(iv));      
    size_t numBytesEncrypted = 0;     
    NSUInteger dataLength = [mutableCipher length];
    size_t bufferSize = dataLength + kCCKeySizeAES128;       
    void *buffer_decrypt = malloc(bufferSize);        
    NSMutableData * output_decrypt = [[NSMutableData alloc] init];        
    CCCryptorStatus result = CCCrypt(kCCDecrypt , kCCAlgorithmAES128, kCCModeCFB, keyPtr, kCCKeySizeAES128, iv, [mutableCipher mutableBytes] , [mutableCipher length], buffer_decrypt, bufferSize, &numBytesEncrypted);       
    output_decrypt = [NSMutableData dataWithBytesNoCopy:buffer_decrypt length:numBytesEncrypted];
    NSString * decryptedStr = [[NSString alloc] initWithData:output_decrypt encoding:NSASCIIStringEncoding];
    NSLog(@"Decrypted String is::%@",decryptedStr);
    if(result == kCCSuccess){
        return decryptedStr;
    }
    else{
        return NULL;
    }

}
return NULL;
}

I am calling it with the following parameters.

AES_Decrypt(@"d5a75c3377fbd50a24ae372c787d598b633e82f4210f90dfc8e618c47811d812", @"”l*®*ŸÂ€­Û»†.Sì’±—ë", @"3d396f81a3443ca44ce146c91a81d3d8");

But the output of the method is not returning the expected output. Can any one help me with this.

Upvotes: 0

Views: 533

Answers (2)

Rob Napier
Rob Napier

Reputation: 299605

You cannot encode arbitrary data as an NSString this way. Not every sequence of data is a legal UTF8 string.

You're passing your key incorrectly. The key you're passing is 512-bits long (64 byte-sized characters). You're grabbing the first 256 bits of that, but those bits are almost certainly not the key you meant to pass (it looks like you meant the key to be in hex format; but you don't decode the hex).

You're ignoring the IV that is passed to you, and just using an IV made up of zeros.

In the end you're converting the data to an ASCII string, but ASCII cannot encode an arbitrary data string.

There is a lot more to encryption than just calling CCCrypt(). If you want an encryption library that handles this with both Cocoa and Python implementations, see RNCryptor.

(I know this isn't a full answer of how to correctly write this code, but correct encryption is actually a fairly complex topic that you should avoid trying to write from scratch. As you've seen here, it's very easy to do many things incorrectly, and very small mistakes can completely undermine your security. I've been very careful in writing RNCryptor, and I've still had to rev it several times to fix security errors. So I strongly recommend using a high-level framework rather than trying to build your encryption solution using low-level components like CommonCryptor.)

Upvotes: 1

RyanR
RyanR

Reputation: 7758

Are you sure the python encryption output an UTF-8 encoded string?

Upvotes: 0

Related Questions