Encryption and decryption of IOS AES

Source: Internet
Author: User

The main is to get the original encryption after the word a, MD5 can not get the original, only to use aes+base64

To use GTMBase64 This can be downloaded on their own,

See the other 2 classes of files mainly.

#import <Foundation/Foundation.h> @class nsstring, @interface NSData (Nsdata_aes)-(NSData *) Aes128encryptwithkey: (NSString *) key;   Encryption-(NSData *) Aes128decryptwithkey: (NSString *) key;   Decryption @end

#import "Nsdata+nsdata_aes.h" #import <CommonCrypto/CommonCryptor.h> #define GIV @ "xxxxxxxxxxx"// The ability to define 16-bit @implementation NSData (NSDATA_AES)//(Key and IV vectors here is 16-bit) here is the CBC encryption mode, security higher-(NSData *) Aes128encryptwithkey: (    NSString *) key//encryption {char keyptr[kcckeysizeaes128+1];    Bzero (keyptr, sizeof (KEYPTR));        [Key Getcstring:keyptr maxlength:sizeof (KEYPTR) encoding:nsutf8stringencoding];    Char ivptr[kcckeysizeaes128+1];    memset (ivptr, 0, sizeof (IVPTR));        [GIv getcstring:ivptr maxlength:sizeof (ivptr) 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, Keyptr, KccblocksizeaeS128, ivptr, [self bytes],                                          datalength, Buffer,    BufferSize, &numbytesencrypted);    if (Cryptstatus = = kccsuccess) {return [NSData datawithbytesnocopy:buffer length:numbytesencrypted];    } free (buffer); return nil;}    -(NSData *) Aes128decryptwithkey: (NSString *) key//decryption {char keyptr[kcckeysizeaes128+1];    Bzero (keyptr, sizeof (KEYPTR));        [Key Getcstring:keyptr maxlength:sizeof (KEYPTR) encoding:nsutf8stringencoding];    Char ivptr[kcckeysizeaes128+1];    memset (ivptr, 0, sizeof (IVPTR));        [GIv getcstring:ivptr maxlength:sizeof (ivptr) 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, Keyptr,                                          kCCBlockSizeAES128, Ivptr, [Self bytes], datalength, buffer    , buffersize, &numbytesdecrypted);    if (Cryptstatus = = kccsuccess) {return [NSData datawithbytesnocopy:buffer length:numbytesdecrypted];    } free (buffer); return nil;} @end


#import <Foundation/Foundation.h> @interface securityutil:nsobject #pragma mark-base64+ (nsstring*) Encodebase64string: (NSString *) input;+ (nsstring*) decodebase64string: (NSString *) input;+ (NSString*) Encodebase64data: (NSData *) data;+ (nsstring*) Decodebase64data: (NSData *) data; #pragma mark-aes encryption// Turn string into data+ (nsstring*) with password Encryptaesdata: (nsstring*) string App_key: (nsstring*) key;// Turn data with password to string+ (nsstring*) Decryptaesdata: (nsdata*) Data app_key: (nsstring*) key; @end

#import "SecurityUtil.h" #import "GTMBase64.h" #import "nsdata+aes.h" @implementation securityutil#pragma mark-base64+ (nsstring*) Encodebase64string: (NSString *) input {nsdata *data = [input datausingencoding:nsutf8stringencoding AllowL     Ossyconversion:yes];     data = [GTMBase64 encodedata:data]; NSString *base64string = [[[NSString Alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease]; return base64string;} + (nsstring*) decodebase64string: (NSString *) input {nsdata *data = [input datausingencoding:nsutf8stringencoding allo     Wlossyconversion: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;} #pragma mark-aes encryption//convert string to data+ with password (nsstring*) Encryptaesdata: (nsstring*) string App_key: (nsstring*) key{/    /convert NSString to nsdata nsdata *data = [string datausingencoding:nsutf8stringencoding];    Use password to encrypt the nsdata nsdata *encrypteddata = [data Aes128encryptwithkey:key];        NSLog (@ "Encrypted string:%@", [EncryptedData base64encoding]); return [EncryptedData base64encoding];} #pragma mark-aes decryption//Transfer data with password to string+ (nsstring*) Decryptaesdata: (nsdata*) Data app_key: (nsstring*) key{//    Use password to decrypt data nsdata *decrydata = [data Aes128decryptwithkey:key]; Convert the password nsdata to nsstring nsstring *str = [[NSString alloc] initwithdata:decrydata encoding:    Nsutf8stringencoding];    NSLog (@ "decrypted string:%@", str); return [str autorelease];} @end


Detailed usage such as the following:

Encryption:

[securityutilencryptaesdata: @ "XXXXXX" app_key:key]; Here is the ability to set a 16-bit string

Decrypt:

nsdata *encryptdata1 = [GTMBase64decodestring:[securityutil Encryptaesdata: @ "XXXXXX" app_key:key]]; // prior to decryption GTMBase64 Coding

nsstring * string1 = [securityutildecryptaesdata: EncryptData1app_key: KEY];


Encryption and decryption of IOS AES

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.