Implementation of IOSAES Encryption

Source: Internet
Author: User

+ (NSData *) Aes256parmencryptwithkey: (NSString *) key encrypttext: (NSData *) text//encryption

{

Char keyptr[kcckeysizeaes256+1];

Bzero (keyptr, sizeof (KEYPTR));

[Key Getcstring:keyptr maxlength:sizeof (KEYPTR) encoding:nsutf8stringencoding];

Nsuinteger datalength = [text length];

size_t buffersize = datalength + kCCBlockSizeAES128;

void *buffer = malloc (buffersize);

size_t numbytesencrypted = 0;

Cccryptorstatus cryptstatus = Cccrypt (Kccencrypt, kCCAlgorithmAES128,

kccoptionpkcs7padding | Kccoptionecbmode,

Keyptr, kCCBlockSizeAES128,

Null

[Text bytes], datalength,

Buffer, buffersize,

&numbytesencrypted);

if (Cryptstatus = = kccsuccess) {

return [NSData Datawithbytesnocopy:buffer length:numbytesencrypted];

}

Free (buffer);

return nil;

}

+ (NSData *) Aes256parmdecryptwithkey: (NSString *) key decrypttext: (NSData *) text//decryption

{

Char keyptr[kcckeysizeaes256+1];

Bzero (keyptr, sizeof (KEYPTR));

[Key Getcstring:keyptr maxlength:sizeof (KEYPTR) encoding:nsutf8stringencoding];

Nsuinteger datalength = [text length];

size_t buffersize = datalength + kCCBlockSizeAES128;

void *buffer = malloc (buffersize);

size_t numbytesdecrypted = 0;

Cccryptorstatus cryptstatus = Cccrypt (Kccdecrypt, kCCAlgorithmAES128,

kccoptionpkcs7padding | Kccoptionecbmode,

Keyptr, kCCBlockSizeAES128,

Null

[Text bytes], datalength,

Buffer, buffersize,

&numbytesdecrypted);

if (Cryptstatus = = kccsuccess) {

return [NSData Datawithbytesnocopy:buffer length:numbytesdecrypted];

}

Free (buffer);

return nil;

}

+ (NSString *) Aes256_encrypt: (NSString *) key encrypttext: (NSString *) text

{

const char *CSTR = [text cstringusingencoding:nsutf8stringencoding];

NSData *data = [NSData datawithbytes:cstr length:text.length];

Encrypt the data

NSData *result = [Lanaes aes256parmencryptwithkey:key encrypttext:data];

Convert to 2 binary string

If (Result && result.length > 0) {

Byte *datas = (byte*) [result bytes];

nsmutablestring *output = [nsmutablestring stringWithCapacity:result.length * 2];

for (int i = 0; i < result.length; i++) {

[Output appendformat:@ "%02x", Datas[i]];

}

return output;

}

return nil;

}

+ (NSString *) Aes256_decrypt: (NSString *) key decrypttext: (NSString *) text

{

Convert to 2 binary data

Nsmutabledata *data = [Nsmutabledata DATAWITHCAPACITY:TEXT.LENGTH/2];

unsigned char whole_byte;

Char byte_chars[3] = {' n ', ' + ', ' + '};

int i;

for (i=0; i < [text length]/2; i++) {

Byte_chars[0] = [text characteratindex:i*2];

BYTE_CHARS[1] = [text characteratindex:i*2+1];

Whole_byte = Strtol (Byte_chars, NULL, 16);

[Data appendbytes:&whole_byte length:1];

}

Decrypt the data

nsdata* result = [Lanaes aes256parmdecryptwithkey:key decrypttext:data];

If (Result && result.length > 0) {

return [[NSString alloc] Initwithdata:result encoding:nsutf8stringencoding];

}

return nil;

}

Implementation of IOSAES Encryption

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.