Sudhanshu Pareek
Sudhanshu Pareek

Reputation: 81

Decrypting in Python and Objective C

I am decrypting a data using python and getting succeed, but when i try to decrypt same data using objective c then decryption get success but when convert decrypted NSData to NSString Returns Null. I have decoded retrieved NSData by applying different encoding but returns null or unreadable data.

Please see below code.

Python

salt = b'saltysalt'
my_val = ‘TGIF40)volubly’
iv = b' ' * 16
iterations = 1003

# Generate key from values above
key = PBKDF2(my_pass, salt, length, iterations)

encrypted_value = \225O\373\235Y\363\265\326\341\206\352\214O\270\343u\300/k;e7\202;\215d\230\364
cipher = AES.new(key, AES.MODE_CBC, IV=iv)
decrypted = cipher.decrypt(encrypted_value)
print(decrypted)
Output : 5105105105105100

Objective C

NSString * my_val = @“TGIF40)volubly”
NSData* myValdata = [my_val dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *derivedKey = [NSMutableData dataWithLength:kCCKeySizeAES128];
NSData *encrypted_inputData = <00954ffb 9d59f3b5 d60ce186 ea8c4f14 b804e375 c02f6b3b 6537823b 8d6498f4>  

NSData *saltbytes = [@“saltysalt”dataUsingEncoding:NSUTF8StringEncoding];   

CCKeyDerivationPBKDF(kCCPBKDF2, myValdata.bytes, myValdata.length, saltbytes.bytes, saltbytes.length, kCCPRFHmacAlgSHA1, 1003, derivedKey.mutableBytes, derivedKey.length);
NSLog(@"%@",derivedKey);

size_t outLength;
NSMutableData *outputData = [NSMutableData dataWithLength:(encrypted_inputData.length  +  kCCKeySizeAES128)];

Printing description of key:
derivedKey  = <dfbf16cc 449619be b57b0aba eb92de70>

CCCryptorStatus result   = kCCSuccess;
result = CCCrypt(kCCDecrypt, // kCCEncrypt or kCCDecrypt
                  kCCAlgorithmAES,
                  kCCOptionPKCS7Padding | kCCModeCBC,
                  derivedKey.bytes,
                  kCCKeySizeAES256,
                  NULL,
                  encrypted_inputData.bytes,
                  encrypted_inputData.length,
                  outputData.mutableBytes,
                  outputData.length,
                  &outLength);

if (result != kCCSuccess) {
    if (error != NULL) {
        *error = [NSError errorWithDomain:@"com.your_domain.your_project_name.your_class_name."
        code:result
        userInfo:nil];
    }
    return nil;
}
[outputData setLength:outLength];
NSString* retStr =  [NSString stringWithUTF8String:[outputData bytes]];

Printing description of outputData: <0a0df388 cf535290 0b1d2712 343e3c69 d633cab7 7cf5382a 92d88207 1f5347bd>

Can anyone help please ?

Upvotes: 1

Views: 253

Answers (1)

zaph
zaph

Reputation: 112875

1. Do not specify kCCModeCBC, it is not used with CCCrypt. CCCrypt has CBC mode as the default.

CCCrypt options are:

enum {
    /* options for block ciphers */
    kCCOptionPKCS7Padding   = 0x0001,
    kCCOptionECBMode        = 0x0002
    /* stream ciphers currently have no options */
};
typedef uint32_t CCOptions;

kCCModeCBC is only used with CCCryptorCreateWithMode and has a value of 2 which if used in place of kCCModeCBC will actually specify ECB mode. This was a bad choice made by Apple.

2. You are providing a 16-byte key but specifying a 256-bit (32-byte) key:

derivedKey  = <dfbf16cc 449619be b57b0aba eb92de70> // 16-bytes

kCCKeySizeAES256, // 32-bytes
  1. When specifying the output data size use kCCBlockSizeAES128.

Upvotes: 1

Related Questions