Implementation of AES encryption and decryption algorithm for iOS development objective-c

Source: Internet
Author: User
Tags decrypt

Advanced Encryption Standard (encryption Standard,aes), also known as Rijndael encryption method. The following implementation code adds a category for NSData and NSString, respectively. Can be called directly when used.

It should be noted that AES is not a hash algorithm, encrypted and decrypted results, and not necessarily the same as the original, please pay attention to the results of the calculation. such as decoding the length of the original text, formatting rules. ng instances

Original: 170987350 Password: 170

The specific implementation code for the AES encryption and decryption algorithm for OBJECTIVE-C is as follows: 1. Expand NSData, add AES256 encryption method

//Nsdata+aes256.h//#import <Foundation/Foundation.h>#import <CommonCrypto/CommonDigest.h>#import <CommonCrypto/CommonCryptor.h> @Interface NSData (AES256)-(NSData *) Aes256_encrypt: (NSString *) key;-(NSData *) Aes256_decrypt: (NSString *) key; @end//Nsdata+aes256.m//#import "Nsdata+aes256.h" @implementation NSData (AES256)-(NSData *) Aes256_encrypt: (NSString *) keyEncryption Char keyptr[kcckeysizeaes256+1];Bzero (Keyptr,sizeof(keyptr));[Key getcstring:keyptr MaxLength:sizeof(KEYPTR) encoding:nsutf8stringencoding];Nsuinteger datalength = [self length];size_t buffersize = datalength + kCCBlockSizeAES128; void *buffer = malloc (buffersize);size_t numbytesencrypted =0;Cccryptorstatus cryptstatus = Cccrypt (Kccencrypt, kCCAlgorithmAES128,         kccoptionpkcs7padding | Kccoptionecbmode,         Keyptr, kCCBlockSizeAES128,         Null         [Self bytes], datalength,         Buffer, buffersize,         &numbytesencrypted); if (Cryptstatus = = kccsuccess) {  return [NSData Datawithbytesnocopy:buffer length:numbytesencrypted];}Free (buffer); return nil;} -(NSData *) Aes256_decrypt: (NSString *) keyDecrypt Char keyptr[kcckeysizeaes256+1];Bzero (Keyptr,sizeof(keyptr));[Key getcstring:keyptr MaxLength:sizeof(KEYPTR) encoding:nsutf8stringencoding];Nsuinteger datalength = [self length];size_t buffersize = datalength + kCCBlockSizeAES128; void *buffer = malloc (buffersize);size_t numbytesdecrypted =0;Cccryptorstatus cryptstatus = Cccrypt (Kccdecrypt, kCCAlgorithmAES128,         kccoptionpkcs7padding | Kccoptionecbmode,         Keyptr, kCCBlockSizeAES128,         Null          [Self bytes], datalength,           buffer, Buffersize,          &numbytesdecrypted); Span class= "indent" > if (cryptstatus = kccsuccess) {  return [nsdata datawithbytesnocopy:buffer length:numbytesdecrypted]; Span class= "indent" >} free (buffer);  return nil;} @end                 

2. Expand NSString, add AES256 encryption method, need to import nsdata+aes256.h

 //NSString +aes256.h// #Import <foundation/foundation.h>#Import <commoncrypto/commondigest.h>#Import <CommonCrypto/CommonCryptor.h> #Import"Nsdata+aes256.h" @interface nsstring (AES256)-(NSString *) Aes256_encrypt: (NSString *) key;-(NSString *) aes256_ Decrypt: (NSString *) key; @end//NSString +aes256.h@implementation NSString (AES256)-(NSString *) Aes256_encrypt: (NSString *) key{ Constchar *cstr = [self cstringusingencoding:nsutf8stringencoding];NSData *data = [NSData datawithbytes:cstr length:self.length]; Encrypt the dataNSData *result = [data Aes256_encrypt:key]; Convert to 2 binary string If (Result && result.length >0) { Byte *datas = (byte*) [result bytes]; nsmutablestring *output = [nsmutablestring stringWithCapacity:result.length *2];  Forint i =0; i < result.length; i++) {  [Output appendformat:@"%02x", Datas[i]]; }  return output;} return nil;} -(NSString *) Aes256_decrypt: (NSString *) key{ Convert to 2 binary dataNsmutabledata *data = [Nsmutabledata dataWithCapacity:self.length/2];UnsignedChar Whole_byte; Char byte_chars[3] = {' A ',' A ',' N '}; int i; for (i=0; i < [self length]/2; i++) { byte_chars[0] = [self characteratindex:i*2];  Byte_chars[1] = [self characteratindex:i*2+1];   Whole_byte = Strtol (Byte_chars, NULL, 16);   [data appendbytes:&whole_byte Length:1];  //decrypt data  nsdata* result = [Data aes256_ Decrypt:key];  if (result && result.length > 0) {  return [[NSString alloc] Initwithdata:result encoding: Nsutf8stringencoding]autorelease]; } return nil;} @end                 

Implementation of AES encryption and decryption algorithm for iOS development objective-c

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.