代码:
+ (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 *)encryptAESCBC:(NSString *)inputString
{
NSMutableData *inputData = [NSMutableData dataWithData: [inputString dataUsingEncoding: NSUTF8StringEncoding]];
NSData *outData = [self CBCWithOperation: kCCEncrypt andIv: 你的偏移量 andKey:加密密钥 andInput: inputData];
outData = [outData base64EncodedDataWithOptions: NSDataBase64EncodingEndLineWithLineFeed];
return [[NSString alloc] initWithData: outData encoding: NSUTF8StringEncoding] ?: @"";
}
调用:[控制器 encryptAESCBC:字符串];