//// AESEncrypt.h// AESDemo//// Created by zhangshuai on 17/4/8.// Copyright © 2017年 zhangshuai. All rights reserved.//#import@interface AESEncrypt : NSObject
+(NSData *)AES256ParmEncryptWithKey:(NSString *)key Encrypttext:(NSData *)text; //加密
+(NSData *)AES256ParmDecryptWithKey:(NSString *)key Decrypttext:(NSData *)text; //解密
+(NSString *) aes256_encrypt:(NSString *)key Encrypttext:(NSString *)text;
+(NSString *) aes256_decrypt:(NSString *)key Decrypttext:(NSString *)text;
@end
/*********************************************************/
//// AESEncrypt.m// AESDemo//// Created by zhangshuai on 17/4/8.// Copyright © 2017年 zhangshuai. All rights reserved.//#import "AESEncrypt.h"#import@implementation AESEncrypt
+(NSData *)AES256ParmEncryptWithKey:(NSString *)key Encrypttext:(NSData *)text //加密
{
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 //解密
{
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];
//对数据进行加密
NSData *result = [AESEncrypt AES256ParmEncryptWithKey:key Encrypttext:data];
//转换为2进制字符串
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
{
//转换为2进制Data
NSMutableData *data = [NSMutableData dataWithCapacity:text.length / 2];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
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];
}
//对数据进行解密
NSData* result = [AESEncrypt AES256ParmDecryptWithKey:key Decrypttext:data];
if (result && result.length > 0) {
return [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
}
return nil;
}
@end
/*********************************************************/
// ViewController.m
// AESDemo
//
// Created by zhangshuai on 17/4/8.
// Copyright © 2017年 zhangshuai. All rights reserved.
//
#import "ViewController.h"
#import "AESEncrypt.h"
#define Key @"passwordKey"
#define passeordString @"zhangshuai"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/***********************第一种***********************************/
NSData *tempData = [passeordString dataUsingEncoding:NSUTF8StringEncoding];
NSData *beforeData = [AESEncrypt AES256ParmEncryptWithKey:Key Encrypttext:tempData];
NSData *afterData = [AESEncrypt AES256ParmDecryptWithKey:Key Decrypttext:beforeData];
NSString *afterStr = [[NSString alloc]initWithData:afterData encoding:NSUTF8StringEncoding];;
NSLog(@"字符串的加密前的data%@",beforeData);
NSLog(@"字符串的加密后的data%@",afterData);
NSLog(@"解密后的字符串%@",afterStr);
/************************第二种********************************/
NSLog(@"********************************************************");
NSString *cc =[AESEncrypt aes256_encrypt:Key Encrypttext:passeordString];
NSString *bb = [AESEncrypt aes256_decrypt:Key Decrypttext:cc];
NSLog(@"加密后的字符串 %@",cc);
NSLog(@"解密后的字符串 %@",bb);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end