You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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.
I am trying to compile this pkg in IOS it doesn't compile . But if I add
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
The text was updated successfully, but these errors were encountered: