Home:ALL Converter>AES Encryption / Decryption Xcode

AES Encryption / Decryption Xcode

Ask Time:2016-11-03T21:08:41         Author:9to5ios

Json Formatter

I am facing error in AES encrypt and decrypt

I am using these CODE

And using in my viewcontroller like this // read show error comment in code Please fix how to use it?

#import "CryptLib.h"
#import "NSData+Base64.h"


-(NSData *)encryption:(NSString*)myString
{



     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.

     key = [[CryptLib alloc] sha256:key length:32]; //this is very important, 32 bytes = 256 bit
    NSString * iv =   [[[[CryptLib alloc] generateRandomIV:11]  base64EncodingWithLineLength:0] substringToIndex:16]; //Here we are generating random initialization vector (iv). Length of this vector = 16 bytes = 128 bits  //SHOWS ERROR HERE

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

     NSLog(@"encrypted data:: %@",[encryptedData  base64EncodingWithLineLength:0]); //print the encrypted text  //SHOWS ERROR HERE
    // Encryption = [plainText + secretKey + randomIV] = Cyphertext

    return encryptedData;
}

Author:9to5ios,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40402331/aes-encryption-decryption-xcode
yy