import"NSData+AES256.h"
#import 《Foundation/Foundation.h》
#import 《CommonCrypto/CommonDigest.h》
#import 《CommonCrypto/CommonCryptor.h》
@interfaceNSData (AES256)
- (NSData*)aes256_encrypt:(NSString*)key;
- (NSData*)aes256_decrypt:(NSString*)key;
@end
import"NSData+AES256.m"
#import"NSData+AES256.h"
@implementationNSData (AES256)
- (NSData*)aes256_encrypt:(NSString*)key//加密
{
charkeyPtr[kCCKeySizeAES256+1];
bzero(keyPtr,sizeof(keyPtr));
[keygetCString:keyPtrmaxLength:sizeof(keyPtr)encoding:NSUTF8StringEncoding];
NSUIntegerdataLength = [selflength];
size_tbufferSize = dataLength +kCCBlockSizeAES128;
void*buffer =malloc(bufferSize);
size_tnumBytesEncrypted =0;
CCCryptorStatuscryptStatus =CCCrypt(kCCEncrypt,kCCAlgorithmAES128,
kCCOptionPKCS7Padding|kCCOptionECBMode,
keyPtr,kCCBlockSizeAES128,NULL,[selfbytes], dataLength,buffer, bufferSize,&numBytesEncrypted);
if(cryptStatus ==kCCSuccess)
{
return[NSDatadataWithBytesNoCopy:bufferlength:numBytesEncrypted];
}
free(buffer);
returnnil;
}
- (NSData*)aes256_decrypt:(NSString*)key//解密
{
charkeyPtr[kCCKeySizeAES256+1];
bzero(keyPtr,sizeof(keyPtr));
[keygetCString:keyPtrmaxLength:sizeof(keyPtr)encoding:NSUTF8StringEncoding];
NSUIntegerdataLength = [selflength];
size_tbufferSize = dataLength +kCCBlockSizeAES128;
void*buffer =malloc(bufferSize);
size_tnumBytesDecrypted =0;
CCCryptorStatuscryptStatus =CCCrypt(kCCDecrypt,kCCAlgorithmAES128,
kCCOptionPKCS7Padding|kCCOptionECBMode,keyPtr,kCCBlockSizeAES128,NULL,[selfbytes], dataLength,buffer, bufferSize,&numBytesDecrypted);
if(cryptStatus ==kCCSuccess)
{
return[NSDatadataWithBytesNoCopy:bufferlength:numBytesDecrypted];
}
free(buffer);
returnnil;
}
@end
import"NSString+AES256.h"
#import 《Foundation/Foundation.h》
#import"NSData+AES256.h"
@interfaceNSString (AES256)
- (NSData*)aes256_encrypt:(NSString*)key;
- (NSData*)aes256_decrypt:(NSString*)key;
@end
import"NSString+AES256.m"
#import"NSString+AES256.h"
#import 《Security/Security.h》
@implementationNSString (AES256)
-(NSString*) aes256_encrypt:(NSString*)key
{
constchar*cstr = [selfcStringUsingEncoding:NSUTF8StringEncoding];
NSData*data = [NSDatadataWithBytes:cstrlength:self.length];
//对数据进行加密
NSData*result = [dataaes256_encrypt:key];
//转换为2进制字符串
if(result && result.length>0) {
Byte*datas = (Byte*)[resultbytes];
NSMutableString*output = [NSMutableStringstringWithCapacity:result.length*2];
for(inti =0; i < result.length; i++){
[outputappendFormat:@"%02x", datas[i]];
}
returnoutput;
}
returnnil;
}
-(NSString*) aes256_decrypt:(NSString*)key
{
//转换为2进制Data
NSMutableData*data = [NSMutableDatadataWithCapacity:self.length/2];
unsignedcharwhole_byte;
charbyte_chars[3] = {'\0','\0','\0'};
inti;
for(i=0; i < [selflength] /2; i++) {
byte_chars[0] = [selfcharacterAtIndex:i*2];
byte_chars[1] = [selfcharacterAtIndex:i*2+1];
whole_byte =strtol(byte_chars,NULL,16);
[dataappendBytes:&whole_bytelength:1];
}
//对数据进行解密
NSData* result = [dataaes256_decrypt:key];
if(result && result.length>0) {
return[[NSStringalloc]initWithData:resultencoding:NSUTF8StringEncoding];
}
returnnil;
}
@end