AES加解密
//
// AESEncryptAndDecrypt.h
// NSData扩展方法,用于处理aes加解密
//
// Created by Vie on 16/4/7.
// Copyright © 2016年 Vie. All rights reserved.
//
#import <Foundation/Foundation.h>
@class NSString;
@interface NSData (AES)
- (NSData *)AES256EncryptWithKey:(NSData *)key; //加密
- (NSData *)AES256DecryptWithKey:(NSData *)key; //解密
@end
//
// AESEncryptAndDecrypt.m
// NSData扩展方法,用于处理aes加解密
//
// Created by Vie on 16/4/7.
// Copyright © 2016年 Vie. All rights reserved.
//
#import "AESEncryptAndDecrypt.h"
#import <CommonCrypto/CommonCryptor.h>
//static char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@implementation NSData (AES)
- (NSData *)AES256EncryptWithKey:(NSData *)key //加密
{
//AES256加密,密钥应该是32位的
const void * keyPtr2 = [key bytes];
char (*keyPtr)[32] = keyPtr2;
//对于块加密算法,输出大小总是等于或小于输入大小加上一个块的大小
//所以在下边需要再加上一个块的大小
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,
kCCOptionPKCS7Padding/*这里就是刚才说到的PKCS7Padding填充了*/| kCCOptionECBMode,
[key bytes], kCCKeySizeAES256,
NULL,/* 初始化向量(可选) */
[self bytes], dataLength,/*输入*/
buffer, bufferSize,/* 输出 */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer);//释放buffer
return nil;
}
- (NSData *)AES256DecryptWithKey:(NSData *)key //解密
{
//同理,解密中,密钥也是32位的
const void * keyPtr2 = [key bytes];
char (*keyPtr)[32] = keyPtr2;
//对于块加密算法,输出大小总是等于或小于输入大小加上一个块的大小
//所以在下边需要再加上一个块的大小
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128,
kCCOptionPKCS7Padding/*这里就是刚才说到的PKCS7Padding填充了*/| kCCOptionECBMode,
keyPtr, kCCKeySizeAES256,
NULL,/* 初始化向量(可选) */
[self bytes], dataLength,/* 输入 */
buffer, bufferSize,/* 输出 */
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer);
return nil;
}
@end
//处理base64编码的密钥加密,原文转换为base64字符串;返回aes加密后的base64编码字符串
-(NSString *)encryptBase64Key:(NSString *)key originalText:(NSString *)text{
//base64字符串utf8格式转nsdata
NSData *dataUtf8Key=[[NSData alloc] initWithBase64EncodedString:[NSString stringWithUTF8String:[key UTF8String]] options:NSDataBase64DecodingIgnoreUnknownCharacters];
//原文转data
NSData *textData = [text dataUsingEncoding:NSUTF8StringEncoding];
//aes加密
NSData *cipherData = [textData AES256EncryptWithKey:dataUtf8Key];
//密文转为base64字符串
NSString *cipherBase64Str=[cipherData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSLog(@"密文:%@",cipherBase64Str);
return cipherBase64Str;
}
//处理base64编码的密钥,密文解密;返回原文
-(NSString *)decryptBase64Key:(NSString *)key cipherText:(NSString *)text{
//密钥base64处理
//base64字符串utf8格式转nsdata
NSData *dataUtf8Key=[[NSData alloc] initWithBase64EncodedString:[NSString stringWithUTF8String:[key UTF8String]] options:NSDataBase64DecodingIgnoreUnknownCharacters];
//密文解base64处理
NSData *dataUtf8Data=[[NSData alloc] initWithBase64EncodedString:[NSString stringWithUTF8String:[text UTF8String]] options:NSDataBase64DecodingIgnoreUnknownCharacters];
//解密原文
NSData *decryptData=[dataUtf8Data AES256DecryptWithKey:dataUtf8Key];
NSString *originalText= [[NSString alloc] initWithData:decryptData encoding:NSUTF8StringEncoding];
NSLog(@"原文:%@",originalText);
return originalText;
}
//测试加密
[self encryptBase64Key:@"74DhItW47MitqIBGOf8aosUllBdhlUxB7es64TKCsgU=" originalText:@"监察局时间长"];
//测试解密
[self decryptBase64Key:@"74DhItW47MitqIBGOf8aosUllBdhlUxB7es64TKCsgU=" cipherText:@"bWZMFt1I2H7wy25D9V3Ll26IdbOs+/NOlzaEqcUZ9L8="];