Original: http://www.lidaren.com/archives/1470
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 Instance Original: 170987350 password:objective-c The specific implementation code of AES encryption and decryption algorithm is as follows: 1. Expand NSData, add AES256 encryption method
1 //2 //nsdata+aes256.h3 //4 5 #import<Foundation/Foundation.h>6 #import<CommonCrypto/CommonDigest.h>7 #import<CommonCrypto/CommonCryptor.h>8 9 @interfaceNSData (AES256)Ten-(NSData *) Aes256_encrypt: (NSString *) key; One-(NSData *) Aes256_decrypt: (NSString *) key; A @end - - the // - //NSDATA+AES256.M - // - #import "nsdata+aes256.h" + - @implementationNSData (AES256) + A-(NSData *) Aes256_encrypt: (NSString *) key//Encrypt at { - Charkeyptr[kcckeysizeaes256+1]; -Bzero (Keyptr,sizeof(keyptr)); -[Key getcstring:keyptr MaxLength:sizeof(KEYPTR) encoding:nsutf8stringencoding]; -Nsuinteger datalength =[self length]; -size_t buffersize = datalength +kCCBlockSizeAES128; in void*buffer =malloc (buffersize); -size_t numbytesencrypted =0; toCccryptorstatus Cryptstatus =Cccrypt (Kccencrypt, kCCAlgorithmAES128, +kccoptionpkcs7padding |Kccoptionecbmode, - keyptr, kCCBlockSizeAES128, the NULL, * [Self bytes], datalength, $ buffer, buffersize,Panax Notoginseng&numbytesencrypted); - if(Cryptstatus = =kccsuccess) { the return[NSData Datawithbytesnocopy:buffer length:numbytesencrypted]; + } A Free (buffer); the returnNil; + } - $ $-(NSData *) Aes256_decrypt: (NSString *) key//decryption - { - Charkeyptr[kcckeysizeaes256+1]; theBzero (Keyptr,sizeof(keyptr)); -[Key getcstring:keyptr MaxLength:sizeof(KEYPTR) encoding:nsutf8stringencoding];WuyiNsuinteger datalength =[self length]; thesize_t buffersize = datalength +kCCBlockSizeAES128; - void*buffer =malloc (buffersize); Wusize_t numbytesdecrypted =0; -Cccryptorstatus Cryptstatus =Cccrypt (Kccdecrypt, kCCAlgorithmAES128, Aboutkccoptionpkcs7padding |Kccoptionecbmode, $ keyptr, kCCBlockSizeAES128, - NULL, - [Self bytes], datalength, - buffer, buffersize, A&numbytesdecrypted); + if(Cryptstatus = =kccsuccess) { the return[NSData Datawithbytesnocopy:buffer length:numbytesdecrypted]; - $ } the Free (buffer); the returnNil; the } the @end - 2Expand NSString, add AES256 encryption method, need to import nsdata+AES256.h in the the // About //NSString +aes256.h the // the the #import<Foundation/Foundation.h> + #import<CommonCrypto/CommonDigest.h> - #import<CommonCrypto/CommonCryptor.h> the Bayi #import "nsdata+aes256.h" the the @interfacensstring (AES256) - --(NSString *) Aes256_encrypt: (NSString *) key; the-(NSString *) Aes256_decrypt: (NSString *) key; the the @end the - the // the //NSString +aes256.h the //94 the @implementationnsstring (AES256) the the-(NSString *) Aes256_encrypt: (NSString *) Key98 { About Const Char*cstr =[self cstringusingencoding:nsutf8stringencoding]; -NSData *data =[NSData datawithbytes:cstr length:self.length];101 //Encrypt the data102NSData *result =[Data Aes256_encrypt:key];103 104 //Convert to 2 binary string the if(Result && result.length >0) {106 107Byte *datas = (byte*) [result bytes];108nsmutablestring *output = [nsmutablestring stringWithCapacity:result.length *2];109 for(inti =0; i < result.length; i++){ the[Output AppendFormat:@"%02x", Datas[i]];111 } the returnoutput;113 } the returnNil; the } the 117-(NSString *) Aes256_decrypt: (NSString *) Key118 { 119 //Convert to 2 binary data -Nsmutabledata *data = [Nsmutabledata dataWithCapacity:self.length/2];121UnsignedCharWhole_byte;122 Charbyte_chars[3] = {' /',' /',' /'};123 inti;124 for(i=0; i < [self length]/2; i++) { thebyte_chars[0] = [Self characteratindex:i*2];126byte_chars[1] = [Self characteratindex:i*2+1];127Whole_byte = Strtol (Byte_chars, NULL, -); -[Data appendbytes:&whole_byte Length:1];129 } the 131 //Decrypt the data thensdata* result =[Data Aes256_decrypt:key];133 if(Result && result.length >0) {134 return[[NSString alloc] Initwithdata:result encoding:nsutf8stringencoding];135 }136 returnNil;137 }138 @end
Implementation of AES encryption and decryption algorithm for iOS development objective-c