How to Encrypt and Decrypt string using Cryptlib Library in Objective C?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to Encrypt and Decrypt string using Cryptlib Library in Objective C?



I am able to do encrypt string but not able to do decrypt the same encrypted string.

Plzzz help if anyone use the library.





Would you please provide the code you have tried to implement this?
– Faysal Ahmed
Aug 9 at 5:49





NSString *str = @"Gitesh"; NSData *strdata00 = [[StringEncryption alloc]encrypt:[str dataUsingEncoding:NSUTF8StringEncoding] key:@"ItDept#!106" iv:@"EdpDept#!106"]; NSString *usernameencrypted_str = [strdata00 base64EncodedString]; NSData *strdata01 = [[StringEncryption alloc]decrypt:[usernameencrypted_str dataUsingEncoding:NSUTF8StringEncoding] key:@"ItDept#!106" iv:@"EdpDept#!106"]; NSString *decryptstr = [strdata01 base64EncodedStringWithOptions:0]; ******* not able to do decrypt the same encrypted string NSLog(@"decryptstr = %@",decryptstr);
– shirish
Aug 9 at 9:15




2 Answers
2



Try to use AESCrypt-ObjC instead of Cryptlib Library.



Installation



Add this line to your class:


#import "AESCrypt.h"



Usage


NSString *message = @"top secret message";
NSString *password = @"p4ssw0rd";



Encrypting


NSString *encryptedData = [AESCrypt encrypt:message password:password];



Decrypting


NSString *message = [AESCrypt decrypt:encryptedData password:password];



Hope this will help.





No I have to integrate Cryptlib library for encryption.bcz my back end team use same library for encryption and decryption.it works well in android also.only problem in iOS. I stuck here since 2 week.plzzz help me
– shirish
Aug 9 at 9:04





@shirish would you please provide the code you have tried?
– Faysal Ahmed
Aug 9 at 9:09





NSString str = @"Gitesh"; NSData *strdata00 = [[StringEncryption alloc]encrypt:[str dataUsingEncoding:NSUTF8StringEncoding] key:@"ItDept#!106" iv:@"EdpDept#!106"]; NSString *usernameencrypted_str = [strdata00 base64EncodedString]; NSData *strdata01 = [[StringEncryption alloc]decrypt:[usernameencrypted_str dataUsingEncoding:NSUTF8StringEncoding] key:@"ItDept#!106" iv:@"EdpDept#!106"]; NSString *decryptstr = [strdata01 base64EncodedStringWithOptions:0]; **** not able to do decrypt the same encrypted string** NSLog(@"decryptstr = %@",decryptstr);
– shirish
Aug 9 at 9:15





What kind to an app you tried to build. If its a Messaging type app then you will use AES encryption and decryption. AES perfectly worked for all platform.
– Faysal Ahmed
Aug 9 at 9:48





Its Goverment app used for Bill payment purpose.need to use a library which is compatible with android , .net & ios
– shirish
Aug 9 at 9:52



I got the solution for you. You can use Cross-Platform-AES instead of Cross-platform-AES-encryption.



Both are working same but Cross-Platform-AES have some extra processing that helps to solve this issue and the implementation is so simple.


NSString *str = @"Gitesh";
NSString *enString = [[CryptLib alloc] encryptPlainText:str key:@"ItDept#!106" iv:@"EdpDept#!106"];
// Encrypted text: ThPGsOjk05yuKxaD7EfU8w==
NSString *drString = [[CryptLib alloc] decryptCipherText:enString key:@"ItDept#!106" iv:@"EdpDept#!106"];
NSLog(@"decryptstr = %@",drString); //Gitesh



The small change needs to change in CryptLib.m class decryptCipherText:(NSString *)ciperText key:(NSString *)key iv:(NSString *)iv method


CryptLib.m


decryptCipherText:(NSString *)ciperText key:(NSString *)key iv:(NSString *)iv



use this:


return [[NSString alloc] initWithData:[[CryptLib alloc] decrypt:[[NSData alloc] initWithBase64EncodedString:ciperText options:NSDataBase64DecodingIgnoreUnknownCharacters] key:[[CryptLib alloc] sha256:key length:32] iv:iv] encoding:NSUTF8StringEncoding];



instead of :


return [[NSString alloc] initWithData:[[CryptLib alloc] decrypt:[[NSData alloc] initWithBase64EncodedString:ciperText options:NSDataBase64DecodingIgnoreUnknownCharacters] key:[[CryptLib alloc] sha256:key length:32] iv:[[CryptLib alloc] generateRandomIV16]] encoding:NSUTF8StringEncoding];





Sir its working but my backend team don't convert the key They take Key as string.so when I hit the api using above code getting no response
– shirish
Aug 9 at 12:48






please try to use the same library on your server. It would be simple change I think.
– Faysal Ahmed
Aug 9 at 12:50





Can u share ur mail id.
– shirish
Aug 10 at 3:34





This answer is solved your problem as you said in the first comment. So you should accept this answer.
– Faysal Ahmed
Aug 10 at 4:13





Plz check ur mail id
– shirish
Aug 10 at 8:59






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard