在线校验网站:https://the-x.cn/cryptography/Aes.aspx
AES有ECB和CBC模式,目前采用的是AES128-ECB-NOPadding模式
直接上代码:
#import <UIKit/UIKit.h>
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonCryptor.h>
/**
* AES工具类
*/
NS_ASSUME_NONNULL_BEGIN
@interface AESUtil : NSObject
/**
* AES加密
*/
+ (NSString *)AES128Encrypt:(NSString *)data key:(NSString *)key;
/**
* AES解密
*/
+ (NSString *)AES128Decrypt:(NSString *)data key:(NSString *)key;
@end
NS_ASSUME_NONNULL_END
#import <Foundation/Foundation.h>
#import "AESUtil.h"
@interface AESUtil()
@end
@implementation AESUtil
//解密
+ (NSString *)AES128Decrypt:(NSString *)str key:(NSString *)keyStr{
NSData *data = [[NSData alloc] initWithBase64EncodedString:str options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSData *key = [keyStr utf8Data];
char keyPtr[kCCKeySizeAES128+1];
bzero(keyPtr, sizeof(keyPtr));
//[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
[key getBytes:keyPtr length:kCCKeySizeAES128+1];
NSUInteger dataLength = [data length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr, kCCBlockSizeAES128,
NULL,
[data bytes], dataLength,
buffer, bufferSize,
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
NSString *utfStr = [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
return utfStr;
}
free(buffer);
return nil;
}
//加密
+ (NSString *)AES128Encrypt:(NSString *)str key:(NSString *)keyStr {
NSData *data = [str utf8Data];
NSData *key = [keyStr utf8Data];
char keyPtr[kCCKeySizeAES128+1];
bzero(keyPtr, sizeof(keyPtr));
//[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
[key getBytes:keyPtr length:kCCKeySizeAES128+1];
NSUInteger dataLength = [data length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr, kCCBlockSizeAES128,
NULL,
[data bytes], dataLength,
buffer, bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
return [resultData base64EncodedStringWithOptions:0];
}
free(buffer);
return nil;
}
@end
- (NSData *)utf8Data{
return [self dataUsingEncoding:NSUTF8StringEncoding];
}
256位的参考:
https://dandelioncloud.cn/article/details/1534494198132928514
//
// LanAES.m
// AESSample
//
// Created by Things on 2017/8/17.
// Copyright © 2017年 Xanthuim. All rights reserved.
//
#import "AESUtil.h"
@implementation AESUtil
//解密
+ (NSData *) AES256_Decrypt:(NSData *)data withKey:(NSData *)key{
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr));
//[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
[key getBytes:keyPtr length:kCCKeySizeAES128+1];
NSUInteger dataLength = [data length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr, kCCBlockSizeAES128,
NULL,
[data bytes], dataLength,
buffer, bufferSize,
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer);
return nil;
}
//加密
+ (NSData *) AES256_Encrypt:(NSData *)data withKey:(NSData *)key {
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr));
//[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
[key getBytes:keyPtr length:kCCKeySizeAES128+1];
NSUInteger dataLength = [data length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr, kCCBlockSizeAES128,
NULL,
[data bytes], dataLength,
buffer, bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer);
return nil;
}
@end