Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
denzerd opened this issue Mar 15, 2016 · 4 comments
Open

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

denzerd opened this issue Mar 15, 2016 · 4 comments

Comments

@denzerd
Copy link

denzerd commented Mar 15, 2016

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
Copy link
Author

denzerd commented Mar 15, 2016

Solved it by using BBAES as mentioned in Issue #35

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

Best regards

@navneet83
Copy link
Owner

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

@denzerd
Copy link
Author

denzerd commented Mar 16, 2016

Tried with an iOS 8.2 device.

@bhaveshios
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants