AES128 decoding problems

97 views Asked by At

I have AES128 crypted web-services I use the AFHTTPRequestOperationManager and I receive the good response ( the response is crypted string ) When I try to decript the string I look the wrong response This is my code:

NSString *string = [[NSString alloc]initWithData:response encoding:NSUTF8StringEncoding];
NSLog(@"%@",string);

NSString *decodedString = [AES128Util AES128Decrypt:string key:Key];
NSLog(@"%@",decodedString);

The key is good beacause on android work correctly In the first NSLog I read the correctly coded response In the second NSLog I read the NULL string Thanks

Edit: LR1JZEOE8MgbEgyZtbqSAbO5ZL5wYBCpLX0KE4PynsFZiRBJe3lvRRr0CPbf0ufuSga8dG5j6IeDBvbn1iNeLUb7cYIb+caSXZw7t8TgrYA= This is the recovered coded string

1

There are 1 answers

3
Boris Charpentier On

A lot could be wrong with encryption and it's kinda hard to debug (or I should say it has a binary behavior working or failing...)

I would first be really pretty damn sure that my key is good.

After that you can look at the AES128Util code https://github.com/GreenWangcl/AES128-for-iOS/blob/master/ASE128Demo/ASE128/AES128Util.m

The encoding seems to be good, else I would have recommand to try Latin1 or others.

So if you have a working implementation on Android one interesting line should be this in AES128Util :

CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
                                      kCCAlgorithmAES128,
                                      kCCOptionPKCS7Padding,
                                      keyPtr,
                                      kCCBlockSizeAES128,
                                      NULL,
                                      [data bytes],
                                      dataLength,
                                      buffer,
                                      bufferSize,
                                      &numBytesCrypted);

Check the padding option in your Android implementation, maybe it's not PKCS7.

If it is, the parameters should be pretty much the same in Android and you can try to evaluate what is different.

Ps : personnaly I'm using https://github.com/RNCryptor/RNCryptor and it works fine with our AES 128 decryption (like you I also have an Android app and a server making the encryption).