IOS AES128 Encryption Decryption

Source: Internet
Author: User

There is a project used AES128 encryption, at that time searched for a long time of data, basic can not be used. Here I have written a code that can be used as follows:

PS: There are some flaws in this method, but it's good enough to use it, and it will be nice to have a chance to write ... Forwarding to mark the source OH)

This is. h

1 //2 //aes128cbc_no_padding_unit.h3 // 4 //5 //Created by Apple on 14-5-13.6 //Copyright (c) 2014 akforsure. All rights reserved.7 //8 9 #import<Foundation/Foundation.h>Ten  One @interfaceAes128cbc_no_padding_unit:nsobject A /** - * AES128 Encryption -  * the * @param plaintext original -  * - * @return A cryptographically good string -  */ ++ (NSString *) Aes128encrypt: (NSString *) plaintext; - /** + * AES128 Decryption A  * at * @param encrypttext cipher -  * - * @return Clear Text -  */ -+ (NSString *) Aes128decrypt: (NSString *) Encrypttext; - @end

This is. m

1 //2 //AES128CBC_NO_PADDING_UNIT.M3 // 4 //5 //Created by Apple on 14-5-13.6 //Copyright (c) 2014 akforsure. All rights reserved.7 //8 9 #import "aes128cbc_no_padding_unit.h"Ten #import<CommonCrypto/CommonCryptor.h> One #import "GTMBase64.h" A @implementationAes128cbc_no_padding_unit -+ (NSString *) Aes128encrypt: (NSString *) PlainText - { the     Charkeyptr[kcckeysizeaes128+1]; -memset (Keyptr,0,sizeof(keyptr)); -[Aeskey getcstring:keyptr MaxLength:sizeof(KEYPTR) encoding:nsutf8stringencoding]; -      +     Charivptr[kccblocksizeaes128+1]; -memset (Ivptr,0,sizeof(ivptr)); +[Aesiv getcstring:ivptr MaxLength:sizeof(IVPTR) encoding:nsutf8stringencoding]; A      atnsdata* data =[plaintext datausingencoding:nsutf8stringencoding]; -Nsuinteger datalength =[data length]; -      -     intdiff = kCCKeySizeAES128-(datalength%kCCKeySizeAES128); -     intNewSize =0; -      in     if(diff >0) -     { toNewSize = Datalength +diff; +     } -      the     CharDataptr[newsize]; * memcpy (dataptr, [data bytes], [data length]); $      for(inti =0; I < diff; i++)Panax Notoginseng     { -Dataptr[i + datalength] =0x00; the     } +      Asize_t buffersize = newSize +kCCBlockSizeAES128; the     void*buffer =malloc (buffersize); +memset (Buffer,0, buffersize); -      $size_t numbytescrypted =0; $      -Cccryptorstatus Cryptstatus =Cccrypt (Kccencrypt, - kCCAlgorithmAES128, the                                           0x0000,//The nopadding filling method used here -                                                     //Besides, there are kccoptionpkcs7padding and Kccoptionecbmode .Wuyi Keyptr, the kCCKeySizeAES128, - Ivptr, Wu Dataptr, -                                           sizeof(dataptr), About Buffer, $ buffersize, -&numbytescrypted); -      -     if(Cryptstatus = =kccsuccess) { ANSData *resultdata =[NSData Datawithbytesnocopy:buffer length:numbytescrypted]; +         return[GTMBase64 Stringbyencodingdata:resultdata]; the     } - Free (buffer); $     returnNil; the } the  the+ (NSString *) Aes128decrypt: (NSString *) Encrypttext the { -     Charkeyptr[kcckeysizeaes128 +1]; inmemset (Keyptr,0,sizeof(keyptr)); the[Aeskey getcstring:keyptr MaxLength:sizeof(KEYPTR) encoding:nsutf8stringencoding]; the      About     Charivptr[kccblocksizeaes128 +1]; thememset (Ivptr,0,sizeof(ivptr)); the[Aesiv getcstring:ivptr MaxLength:sizeof(IVPTR) encoding:nsutf8stringencoding]; the      +NSData *data =[GTMBase64 decodedata:[encrypttext datausingencoding:nsutf8stringencoding]; -Nsuinteger datalength =[data length]; thesize_t buffersize = datalength +kCCBlockSizeAES128;Bayi     void*buffer =malloc (buffersize); the      thesize_t numbytescrypted =0; -Cccryptorstatus Cryptstatus =Cccrypt (Kccdecrypt, - kCCAlgorithmAES128, the                                           0x0000, the Keyptr, the kCCBlockSizeAES128, the Ivptr, - [data bytes], the Datalength, the Buffer, the buffersize,94&numbytescrypted); the     if(Cryptstatus = =kccsuccess) { theNSData *resultdata =[NSData Datawithbytesnocopy:buffer length:numbytescrypted]; the         return[[[ NSString alloc] initwithdata:resultdata encoding:nsutf8stringencoding] autorelease];98     } About Free (buffer); -     returnNil;101 }102 103 @end

IOS AES128 Encryption Decryption

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.