iOS common encryption Methods (AES, MD5, base64)

Source: Internet
Author: User
Tags md5 encryption

1. Code

iOS Common encryption methods (AES, MD5, base64)1, AES encryption NSData+AES.h File//  //Nsdata-aes.h//Smile//  //Created by Zhou Min on 12-11-24. //Copyright (c) 2012 BOX.  All rights reserved. //    #import<Foundation/Foundation.h>@classNSString; @interfaceNSData (encryption)-(NSData *) Aes256encryptwithkey: (NSString *) key;//Encrypt-(NSData *) Aes256decryptwithkey: (NSString *) key;//decryption  @endNSData+aes.m File//  //Nsdata-aes.h//Smile//  //Created by Zhou Min on 12-11-24. //Copyright (c) 2012 BOX.  All rights reserved. //    #import "Nsdata+aes.h"  #import<CommonCrypto/CommonCryptor.h>@implementationNSData (encryption)-(NSData *) Aes256encryptwithkey: (NSString *) Key {//Encrypt    Charkeyptr[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); returnNil; }      -(NSData *) Aes256decryptwithkey: (NSString *) Key {//decryption    Charkeyptr[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); returnNil; }    @endin this case, AES is saved in nsdata form after iOS is added, and if you want to store it in nsstring form, the NSData is Base64 bit encoded. 2, BASE64 encoding first download the GTMBase64 file, add three files to your project GTMDEFINES.HGTMBASE64.HGTMBASE64.M here you can find these three file http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/foundation/?r=87You 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. Here's a little bit of encapsulation:. h file#pragmaMark-base64 + (nsstring*) encodebase64string: (NSString *) input; + (nsstring*) decodebase64string: (NSString *) input; + (nsstring*) Encodebase64data: (NSData *) data; + (nsstring*) Decodebase64data: (NSData *) data; . m file#pragmaMark-base64 + (nsstring*) encodebase64string: (NSString *) input {NSData*data =[input datausingencoding:nsutf8stringencoding allowlossyconversion:yes]; Data=[GTMBase64 Encodedata:data]; NSString*base64string =[[[ NSString alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease]; returnbase64string; }    + (nsstring*) decodebase64string: (NSString *) input {NSData*data =[input datausingencoding:nsutf8stringencoding allowlossyconversion:yes]; Data=[GTMBase64 Decodedata:data]; NSString*base64string =[[[ NSString alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease]; returnbase64string; }     + (nsstring*) Encodebase64data: (NSData *) Data {data=[GTMBase64 Encodedata:data]; NSString*base64string =[[[ NSString alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease]; returnbase64string; }    + (nsstring*) Decodebase64data: (NSData *) Data {data=[GTMBase64 Decodedata:data]; NSString*base64string =[[[ NSString alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease]; returnbase64string; }   3, MD5 encryption www.2cto.comNSString+MD5.h File//  //nsstring+md5encrypt.h//Smile//  //Created by Zhou Min on 12-11-24. //Copyright (c) 2012 BOX.  All rights reserved. //    #import<CommonCrypto/CommonDigest.h>@interfacensstring (MD5)-(NSString *) Md5encrypt; @endNSString+md5.m File//  //nsstring+md5encrypt.h//Smile//  //Created by Zhou Min on 12-11-24. //Copyright (c) 2012 BOX.  All rights reserved. //    #import "nsstring+md5.h"    @implementationnsstring (MD5)-(NSString *) Md5encrypt {Const Char*original_str =[self utf8string]; unsignedCharResult[cc_md5_digest_length];      CC_MD5 (Original_str, strlen (ORIGINAL_STR), result); Nsmutablestring*hash = [nsmutablestringstring];  for(inti =0; I < -; i++) [Hash AppendFormat:@"%02x", Result[i]]; return[Hash lowercasestring]; }    @end   

2. Call Method:

#import "SecurityUtil.h"

NSString *teststring = @ "Test";

Method 1:(irreversible)

NSLog (@ "======base64:%@======", [Securityutil encodebase64string:teststring]);

Method 2:(irreversible)

NSLog (@ "======md5:%@=======", [Securityutil encryptmd5string:teststring]);

Method 3: (reversible)

NSData *aesdata = [Securityutil encryptaesdata:teststring];

NSLog (@ "=====aes encryption:%@=========", aesdata);

NSLog (@ "=======aes decryption:%@======", [Securityutil Decryptaesdata:aesdata]);

Method 4: Combining methods

For example, the MD5 results of BASE64 encryption and so on, thereby increasing the difficulty of cracking

Demo contains these 3 kinds of use, in the APPDELEGATE.M file, will not use their own reference

Http://up.2cto.com/2012/1215/20121215123257741.zip

Ps:http://www.2cto.com/kf/201212/176351.html

iOS common encryption Methods (AES, MD5, base64)

Related Article

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.