AES string encryption in Objective-C -


My purpose - requires app text / string encryption (especially).

I know that AES is the safest encryption method for consumer use. I also understand how to convert data to the wire and back ... how to change (only one start).

Many web pages and Q / AES are not clear about encryption, and none of them tell how to use code given code. For example, a webpage might say: "Here's the code ... here's what it does ..." but there is no explanation for how to use it.

I've got this code through lots of research:

  #import "& LT; CommonCrypto / CommonCryptor.h & gt;" @implementation NSMutableData (AES)   

Encryption:

  - (NSMutableData *) EncryptAES: (NSString *) key {char keyPtr [kCCKeySizeAES256 + 1 ]; Berboo (keypets, keyboards); [Important GetCString: keyPtr maxLength: sizeof (keyPtr) encoding: NSUTF16StringEncoding]; Size_t numBytesEncrypted = 0; NSUntiger DataLong = [Self length]; Size_t BufferSecurity = DataLanguage + KCCBlockSizeAES128; Zero * buffer = molok (buffer size); NSMutableData * Output = [[NSData alloc] init]; CCCryptorStatus result = CCCrypt (kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, zero, [self MutableBytes], [Self length], buffer, bufferSize, & amp; numBytesEncrypted); Output = [NSMutableData dataWithBytesNoCopy: buffer length: numBytesEncrypted]; If (results == kCCSuccess) {return output; } Return tap; }   

The decryption:

  - (NSMutableData *) DecryptAES: (NSString *) key andForData: (NSMutableData *) objEncryptedData {four keyPtr [kCCKeySizeAES256 + 1]; Berboo (keypets, keyboards); [Important GetCString: keyPtr maxLength: sizeof (keyPtr) encoding: NSUTF16StringEncoding]; Size_t numBytesEncrypted = 0; NSUntiger DataLong = [Self length]; Size_t BufferSecurity = DataLanguage + KCCBlockSizeAES128; Zero * buffer _ decrypt = molok (buffer size); NSMutableData * output_decrypt = [[NSData alloc] init]; CCCryptorStatus result = CCCrypt (kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, zero, [self mutableBytes], [Self length], buffer_decrypt, bufferSize, & amp; numBytesEncrypted); Output_decrypt = [NSMutableData dataWithBytesNoCopy: buffer_driver length: numBytesEncrypted]; If (result == kCCSuccess) {output output_decrypt; } Return tap; }}   

The code I made that I want to correspond with the above code above:

  - (minus) to encrypt {// Convert NSString for NSDT so that this input can be used to encrypt NSString * input = [Inputbox text]; NSDTA * InputData = [input data using encoding: NSUFF 8 string encoding]; // what to do here}   

How do I use this code, these methods? Where does it go into my implementation file?

After

This line near the top says that you are adding AES functionality to NSMutableData:

  @implementation NSMutableData (AES)   

In Objective-C, this is called a class; Categories allow you to expand the existing class

This code usually goes into a file named NSMutableData-AES.m. Also create a header file, NSMutableData-AES.h. It should include:

  @interface NSMutableData (AES) - (NSMutableData *) EncryptAES: (NSString *) key; @end   

Include in (#import) which is the header in your main file. Call the encryption function in your code:

  NSData * InputData = [input dataUsingEncoding: NSUTF8StringEncoding]; NSDTA * Encrypted Data [[InputData EncryptAES: @ "Manacrption Key"];   

Similarly for decryption.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -