一.AES+128+CBC 加密
128加密算法长度
CBC加密方式
1.继承NSData写一个扩展。如下:有相关注释哦~
- (NSData *)AES256EncryptWithKey:(NSString *)key//加密
{
//AES的密钥长度256字节
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
char ivPtr[kCCKeySizeAES256+1];
memset(ivPtr, 0, sizeof(ivPtr));
[APP_PUBLIC_IV getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
//密文的长度
NSUInteger dataLength = [self length];
//密文长度+补位长度
size_t bufferSize = dataLength + kCCKeySizeAES256;
//为加密结果开辟空间
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
/* kCCDecrypt:加密/解密
* kCCAlgorithmAES128:加密方式
* kCCOptionPKCS7Padding | kCCOptionECBMode:工作模式
* keyPtr:UTF-8格式的key
* kCCKeySizeAES256:按32位长度解密
* iv:私钥
* [self bayes]:密文
* ...
*/
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr,
kCCKeySizeAES256,
ivPtr,
[self bytes],
dataLength,
buffer,
bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer);
return nil;
}
- (NSData *)AES256DecryptWithKey:(NSString *)key//解密
{
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
char ivPtr[kCCKeySizeAES256+1];
memset(ivPtr, 0, sizeof(ivPtr));
[APP_PUBLIC_IV getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCKeySizeAES256;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr,
kCCKeySizeAES256,
ivPtr,
[self bytes],
dataLength,
buffer,
bufferSize,
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer);
return nil;
}
2.使用方法
NSString *test = @"abcdefghijklmnopqrstuvwxyz";
NSString *aesData = [SecurityUtil encryptAESData:test];
NSLog(@"AES加密:%@", aesData);
NSString *enstr = [SecurityUtil decryptAESData:aesData];
NSLog(@"AES解密:%@",enstr);
二.AES+256+CFB 加密
#pragma mark - CFB加密
+ (NSString *)encryptAESCBC:(NSString *)inputString
{
NSMutableData *inputData = [NSMutableData dataWithData: [inputString dataUsingEncoding: NSUTF8StringEncoding]];
NSData *outData = [self CBCWithOperation:kCCEncrypt andIv:APP_PUBLIC_IV andKey:APP_PUBLIC_KEY andInput: inputData];
outData = [outData base64EncodedDataWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
return [[NSString alloc] initWithData:outData encoding: NSUTF8StringEncoding] ?: @"";
}
#pragma mark - CFB解密
+ (NSString *)decryptAESCBC:(NSString *)outputString
{
NSData *data = [GTMBase64 decodeString:outputString];
NSData *outData = [self CBCWithOperation:kCCDecrypt andIv:APP_PUBLIC_IV andKey:APP_PUBLIC_KEY andInput:data];
//将解了密码的nsdata转化为nsstring
NSString *result = [[NSString alloc] initWithData:outData encoding:NSUTF8StringEncoding];
return result;
}
核心代码如下:
+ (NSData *)CBCWithOperation:(CCOperation)operation andIv:(NSString *)ivString andKey:(NSString *)keyString andInput:(NSData *)inputData
{
const char *iv = [[ivString dataUsingEncoding: NSUTF8StringEncoding] bytes]; const char *key = [[keyString dataUsingEncoding: NSUTF8StringEncoding] bytes];
CCCryptorRef cryptor;
CCCryptorCreateWithMode(operation, kCCModeCFB, kCCAlgorithmAES, ccNoPadding, iv, key, [keyString length], NULL, 0, 0, 0, &cryptor);
NSUInteger inputLength = inputData.length;
char *outData = malloc(inputLength);
memset(outData, 0, inputLength);
size_t outLength = 0;
CCCryptorUpdate(cryptor, inputData.bytes, inputLength, outData, inputLength, &outLength);
NSData *data = [NSData dataWithBytes: outData length: outLength];
CCCryptorRelease(cryptor);
free(outData);
return data;
}
使用方法:
NSString *str = [NSData encryptAESCBC:test];
NSLog(@"AES cfb加密:%@", str);
NSString *str1 = [NSData decryptAESCBC:str];
NSLog(@"AES cfb解密:%@",str1);
有什么地方看不懂的可以私聊哦~ QQ:154235507
最近比较忙,没有时间上传代码到github上,有需要源文件的也可以私聊哦~
之后有时间的话,会上传demo。