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

Encrypt in java and Decrypt in IOS - Not working #48

Open
shibins opened this issue Sep 28, 2016 · 1 comment
Open

Encrypt in java and Decrypt in IOS - Not working #48

shibins opened this issue Sep 28, 2016 · 1 comment

Comments

@shibins
Copy link

shibins commented Sep 28, 2016

I am trying to compile this pkg in IOS it doesn't compile . But if I add

  • (NSString *) base64EncodingWithLineLength:(unsigned int) lineLength ;

In NSData+Base64.h programpile

But when I use

NSString * _secret = @"This the sample text has to be encrypted"; // this is the text that you want to encrypt.
NSString * key = @"shared secret"; //secret key for encryption. To make encryption stronger, we will not use this key directly. We'll first hash the key next step and then use it.
NSString * _key = [[StringEncryption alloc] sha256:key length:32]; //this is very important, 32 bytes = 256 bit
NSString * iv = [[[[StringEncryption alloc] generateRandomIV:11] base64EncodingWithLineLength:0] substringToIndex:16];
NSData * encryptedData = [[StringEncryption alloc] encrypt:[_secret dataUsingEncoding:NSUTF8StringEncoding] key:_key iv:iv];
NSLog(@"encrypted data:: %@", [encryptedData base64EncodingWithLineLength:0]); //print the encrypted text

I get coredump

@dschlaba
Copy link

dschlaba commented Oct 3, 2016

If compiling for iOS 7 and above, you no longer need the NSData+Base64 files.

Here is an updated m file that compiles in iOS10

http://pastebin.com/CjAEDsgb

Now to test:

`+ (void)test{

StringEncryption *crypt = [[StringEncryption alloc] init];

NSString *secret = @"This is the text to be encrypted"; // this is the text that you want to encrypt.

NSString *key = [[StringEncryption alloc] sha256:@"my secret key" length:32]; // this is very important, 32 bytes = 256 bit

NSString *iv = [crypt generateRandomIV:16]; // Here we are generating random initialization vector (iv).
                                            // Length of this vector = 16 bytes = 128 bits

// Now that we have input text, hashed key and random IV, we are all set for encryption:
NSData *encryptedData = [crypt encrypt:[secret dataUsingEncoding:NSUTF8StringEncoding] key:key iv:iv];

// Here is your encrypted string to pass.
NSString *encryptedString = [encryptedData base64EncodedStringWithOptions:0];

//--- SOME ACTION ---//
//--- Save to a database, pass to another device, etc. ---//

// Here is how you convert your passed string back into an NSData object.
NSData *encryptedDataSent = [[NSData alloc] initWithBase64EncodedString:encryptedString options:NSDataBase64DecodingIgnoreUnknownCharacters];

// 2. Decryption
//      Decrypt the data and then convert it back into a string.
//      You will have to use the same IV and key which was used for encryption.
NSData *decryptedData = [[StringEncryption alloc] decrypt:encryptedDataSent key:key iv:iv];
NSString *decryptedText = [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];

NSLog(@"iv=%@", iv);
NSLog(@"key=%@", key);
NSLog(@"Plain text=%@", secret);
NSLog(@"encrypted=%@", encryptedString);
NSLog(@"decrypted=%@", decryptedText);

}
`

Remember, the RandomIV has to be the same on both sides so you will need to figure out how to pass it to whatever device is doing the decrypt.

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

2 participants