This article reprinted to http://blog.csdn.net/wildfireli/article/details/23191983
(AES, MD5, Base64) Category: IPhone 2014-04-08 16:30 187 People Read reviews (0) Favorites Report directory (?) [+] 1, AES encryption nsdata+aes.h File////NSDATA-AES.H//Smile////Created by Gary on 12-11-24. Copyright (c) 2012 BOX. All rights reserved. #import @class NSString; @interface NSData (encryption)-(NSData *) Aes256encryptwithkey: (NSString *) key; Encryption-(NSData *) Aes256decryptwithkey: (NSString *) key; Decrypt @end NSDATA+AES.M File////NSDATA-AES.H//Smile////Created by Gary on 12-11-24. Copyright (c) 2012 BOX. All rights reserved. #import "Nsdata+aes.h" #import @implementation NSData (encryption)-(NSData *) Aes256encryptwithkey: (NSString *) key {/ /encrypt 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, Kccalgorithmaeskccoptionpkcs7padding | Kccoptionecbmode, Keyptr, kCCBlockSizeAES128, NULL, [self bytes], datalength, buffer, buffersize, & numbytesencrypted); if (Cryptstatus = = kccsuccess) {return [NSData datawithbytesnocopy:buffer length:numbytesencrypted];} free (buffer); return nil; }-(NSData *) Aes256decryptwithkey: (NSString *) key {//decrypt 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); if (Cryptstatus = = kccsuccess) {return [NSData datawithbytesnocopy:buffer length:numbytesdecrypted];} free (buffer); return nil; } @end here AES isiOS is added to the NSData in the form of storage, if you want to store in nsstring form, then the NSData base64 bit encoding. 2, BASE64 code First download GTMBase64 file, in the project to add three files GTMDefines.h GTMBase64.h gtmbase64.m You can find these three files here http://code.google.com/p/ Google-toolbox-for-mac/source/browse/trunk/foundation/?r=87 You can also find these 3 files in the demo below, the demo will fully implement the 3 kinds of coding methods commonly used in the article. I'll just wrap it up here:. h file #pragma mark-base64 + (nsstring*) encodebase64string: (NSString *) input; + (nsstring*) decodebase64string: (NSString *) input; + (nsstring*) Encodebase64data: (NSData *) data; + (nsstring*) Decodebase64data: (NSData *) data;. m file #pragma mark-base64 + (nsstring*) encodebase64string: (NSString *) INP UT {nsdata *data = [input datausingencoding:nsutf8stringencoding allowlossyconversion:yes]; data = [GTMBase64 encodedata :d ATA]; NSString *base64string = [[[NSString Alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease]; return base64string; } + (nsstring*) decodebase64string: (NSString *) input {nsdata *data = [input datausingencoding:nsutf8stringencoding allow Lossyconversion:yes]; data = [GTMBAse64 Decodedata:data]; NSString *base64string = [[[NSString Alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease]; return base64string; } + (nsstring*) Encodebase64data: (NSData *) data {data = [GTMBase64 encodedata:data]; NSString *base64string = [[[NSString Alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease]; return base64string; } + (nsstring*) Decodebase64data: (NSData *) data {data = [GTMBase64 decodedata:data]; NSString *base64string = [[[NSString Alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease]; return base64string; } 3, MD5 encrypted nsstring+md5.h File////Nsstring+md5encrypt.h//Smile////Created by Gary on 12-11-24. Copyright (c) 2012 BOX. All rights reserved. #import @interface NSString (MD5)-(NSString *) Md5encrypt; @end nsstring+md5.m File////Nsstring+md5encrypt.h//Smile////Created by Gary on 12-11-24. Copyright (c) 2012 BOX. All rights reserved. #import "Nsstring+md5.h" @implementation nsstring (MD5)-(Nsstring *) md5encrypt {const char *original_str = [self utf8string]; unsigned char result[cc_md5_digest_length]; CC_MD5 (Original_str, strlen (ORIGINAL_STR), result); nsmutablestring *hash = [nsmutablestring string]; for (int i = 0; i < i++) [hash appendformat:@ "X", Result[i]]; return [hash lowercasestring]; } @end
Common encryption methods----iOS development