1.创建一个NSString的Category,我起名叫AES
首先你要知道AES有多种加密模式,就是ECB, CBC ,CFB ,OFB这四种,至于选择哪种你需要跟服务端人员协商好,要一致,这里我们主要介绍CBC模式
.h文件
import <Foundation/Foundation.h>
@interface NSString (AES)
/**< 加密方法 */
- (NSString*)aci_encryptWithAES;
/**< 解密方法 */
- (NSString*)aci_decryptWithAES;
@end
3
.m文件
import "NSString+AES.h"
import <CommonCrypto/CommonDigest.h>
import <CommonCrypto/CommonCryptor.h>
// key跟后台协商一个即可,保持一致
static NSString *const PSW_AES_KEY = @"这里填写客户端跟后台商量的key";
// 这里的偏移量也需要跟后台一致,一般跟key一样就行
static NSString *const AES_IV_PARAMETER = @"偏移量";
@implementation NSString (AES)
(NSString*)aci_encryptWithAES {
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
NSData *AESData = [self AES128operation:kCCEncrypt
data:data
key:PSW_AES_KEY
iv:AES_IV_PARAMETER];
NSString *baseStr_GTM = [self encodeBase64Data:AESData];
return baseStr_GTM;
}-
(NSString*)aci_decryptWithAES {
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
NSData *baseData_GTM = [self decodeBase64Data:data];
NSData *baseData = [[NSData alloc]initWithBase64EncodedString:self options:0];NSData *AESData_GTM = [self AES128operation:kCCDecrypt
data:baseData_GTM
key:PSW_AES_KEY
iv:AES_IV_PARAMETER];
NSData *AESData = [self AES128operation:kCCDecrypt
data:baseData
key:PSW_AES_KEY
iv:AES_IV_PARAMETER];NSString *decStr_GTM = [[NSString alloc] initWithData:AESData_GTM encoding:NSUTF8StringEncoding];
LYLog(@"decStr_GTM : %@",decStr_GTM);
NSString *decStr = [[NSString alloc] initWithData:AESData encoding:NSUTF8StringEncoding];return decStr;
}
/**
- AES加解密算法
- @param operation kCCEncrypt(加密)kCCDecrypt(解密)
- @param data 待操作Data数据
- @param key key
- @param iv 向量
*/
-
(NSData *)AES128operation:(CCOperation)operation data:(NSData *)data key:(NSString *)key iv:(NSString *)iv {
char keyPtr[kCCKeySizeAES128 + 1]; //kCCKeySizeAES128是加密位数 可以替换成256位的
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
// IV
char ivPtr[kCCBlockSizeAES128 + 1];
bzero(ivPtr, sizeof(ivPtr));
[iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];size_t bufferSize = [data length] + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;// 设置加密参数
/**
这里设置的参数ios默认为CBC加密方式,如果需要其他加密方式如ECB,在kCCOptionPKCS7Padding这个参数后边加上kCCOptionECBMode,即kCCOptionPKCS7Padding | kCCOptionECBMode,但是记得修改上边的偏移量,因为只有CBC模式有偏移量之说*/
CCCryptorStatus cryptorStatus = CCCrypt(operation, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES128,
ivPtr,
[data bytes], [data length],
buffer, bufferSize,
&numBytesEncrypted);if(cryptorStatus == kCCSuccess) {
NSLog(@"Success");
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];} else {
NSLog(@"Error");
}free(buffer);
return nil;
}
// 这里附上GTMBase64编码的代码,可以手动写一个分类,也可以直接cocopods下载,pod 'GTMBase64'。
/**< GTMBase64编码 */
- (NSString*)encodeBase64Data:(NSData *)data {
data = [GTMBase64 encodeData:data];
NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return base64String;
}
/**< GTMBase64解码 */
- (NSData*)decodeBase64Data:(NSData *)data {
data = [GTMBase64 decodeData:data];
return data;
}