navneet83 / Cross-platform-AES-encryption

Basic cross platform AES encryption
Apache License 2.0
320 stars 152 forks source link

Decryption not working in iOS. Encrypted in C#. Works in Android #40

Open denzerd opened 8 years ago

denzerd commented 8 years ago

Hi there,

I try to use your library classes to encrypt and decrypt some strings that are shared between apps. I'm using C# to create a Hybrid app with one code base for iOS and Android. So the encryption is done in C#. That's working fine that way:

CryptLib crypto = new CryptLib();
string iv = CryptLib.GenerateRandomIV (16); //Hash of 128 bits
string key = CryptLib.getHashSha256 (KEY,32); //256 bits
string encryptedText= crypto.encrypt (textToEncrypt, key, iv);

After that I cut the encrypted text into half and put the iv in. On that way I'm able to read the used iv from other apps that share the value with the encrypting app.

In Android, that's working pretty fine that way:

//The key of the encryption
String key = "MYKEYUSEDBEFORE";
//Cut out the iv that was used in encryption
int end = encryptedText.length()/2+8;
int start = end-16;
String iv = encryptedText.substring(start,end);
//Remove the iv from the encryptedText
encryptedText = encryptedText.replace(iv, "");
try
 {
    //decrypt and return the decryptedText
    key = CryptLib.SHA256(key, 32);
    CryptLib crypto = new CryptLib();
    String result = crypto.decrypt(encryptedText, key, iv);
    return result;
}
catch(Exception ignored){}

return null;

That's working perfect. I receive my text without problems. I try the same approach in iOS that way:

int end = [encryptedText length]/2+1;
int start = end - 9;
//Get key
NSString *key = @"MYKEYUSEDBEFORE;
key = [[StringEncryption alloc]  sha256:key length:32];
//Cut out iv from encryptedText
NSString *iv = [encryptedText substringWithRange:NSMakeRange(start, 16)];
//Replace iv in encryptedText
encryptedText = [encryptedText stringByReplacingOccurrencesOfString:iv withString:@""];
//Decyrpt
 NSData *resultData = [[StringEncryption alloc] decrypt:[encryptedText dataUsingEncoding:NSUTF8StringEncoding] key:key iv:iv];
    NSString *result = [[NSString alloc]initWithData:resultData encoding:NSUTF8StringEncoding];
    return result;

But result is always null.

I can confirm that the iv is correct, it's definitely the same as used for encryption and the encryptedText after replacing the iv is exactly the same as after encryption and before putting in the iv. So, what am I doing wrong?

Thanks a lot!

Best regards

denzerd commented 8 years ago

Solved it by using BBAES as mentioned in Issue #35

But would still be interesting why it's not working?

Best regards

navneet83 commented 8 years ago

This may be because of latest version of iOS. Which version of iOS are you on ?

denzerd commented 8 years ago

Tried with an iOS 8.2 device.

bhaveshios commented 7 years ago

hii naveen

i m also getting the same error in ios 9.0 . it is working proper in android, but with same key and IV it generating different encrypted string in ios , and getting null decrypt string. here is my code

NSString _secret = @"bhavesh"; NSString _key = @"vector"; NSData * encryptedData = [[StringEncryption alloc] encrypt:[_secret dataUsingEncoding:NSUTF8StringEncoding] key:_key iv:@"vector"];

 NSLog(@"encrypted data:: %@", [encryptedData  base64EncodingWithLineLength:0]); //print the encrypted text

 encryptedData = [[StringEncryption alloc] decrypt:encryptedData  key:_key iv:@"vector"];

NSString * iv = [[[[StringEncryption alloc] generateRandomIV:11] base64EncodingWithLineLength:0] substringToIndex:16];

NSString * decryptedText = [[NSString alloc] initWithData:encryptedData encoding:NSUTF8StringEncoding];
 NSLog(@"decrypted data:: %@", decryptedText); //print the decrypted text