+ (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