高級加密標準(Advanced Encryption Standard,AES),又稱 Rijndael 加密法。 以下實現代碼中分別為 NSData 和 NSString 增加了一個 Category。使用時直接調用即可。
需要注意的是,AES 並不能作為 HASH 算法,加密並解密後的結果,並不一定與原文相同,使用時請注意進行結果驗算。例如解密原文的長度,格式規則等。 NG 實例
原文:170987350
密碼:170
Objective-c 的 AES 加密和解密算法的具體實現代碼如下: 1. 拓展 NSData,增加 AES256 加密方法
//
//NSData+AES256.h
//
#import #import #import @interface NSData(AES256)
-(NSData *) aes256_encrypt:(NSString *)key;
-(NSData *) aes256_decrypt:(NSString *)key;
@end
//
//NSData+AES256.m
//
#import "NSData+AES256.h"
@implementation NSData(AES256)
-
(NSData *) aes256_encrypt:(NSString *) key // 加密
{
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString maxLength(keyPtr) encoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr, kCCBlockSizeAES128,
NULL,
[self bytes], dataLength,
buffer, bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy length];
}
free(buffer);
return nil;
} -
(NSData *) aes256_decrypt:(NSString *) key // 解密
{
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString maxLength(keyPtr) encoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr, kCCBlockSizeAES128,
NULL,
[self bytes], dataLength,
buffer, bufferSize,
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy length];}
free(buffer);
return nil;
}
@end
2. 拓展 NSString,增加 AES256 加密方法,需要導入 NSData+AES256.h
//
//NSString +AES256.h
//
#import #import #import #import "NSData+AES256.h"
@interface NSString(AES256)
-(NSString *) aes256_encrypt:(NSString *)key;
-(NSString *) aes256_decrypt:(NSString *)key;
@end
//
//NSString +AES256.h
//
@implementation NSString(AES256)
-(NSString *) aes256_encrypt:(NSString *)key
{
const char *cstr = [self cStringUsingEncoding];
NSData *data = [NSData dataWithBytes length.length];
// 對數據進行加密
NSData *result = [data aes256_encrypt];
//轉換為2進制字符串
if (result && result.length > 0) {
Byte \*datas = (Byte\*)\[result bytes\];
NSMutableString \*output = \[NSMutableString stringWithCapacity:result.length \* 2\];
for(int i = 0; i < result.length; i++){
\[output appendFormat:@"%02x", datas\[i\]\];
}
return output;
}
return nil;
}
-(NSString *) aes256_decrypt:(NSString *)key
{
// 轉換為 2 進制 Data
NSMutableData *data = [NSMutableData dataWithCapacity.length / 2];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [self length] / 2; i++) {
byte_chars[0] = [self characterAtIndex*2];
byte_chars[1] = [self characterAtIndex*2+1];
whole_byte = strtol(byte_chars, NULL, 16);
[data appendBytes:&whole_byte length:1];
}
//對數據進行解密
NSData\* result = \[data aes256\_decrypt:key\];
if (result && result.length > 0) {
return \[\[NSString alloc\] initWithData:result encoding:NSUTF8StringEncoding\]autorelease\];
}
return nil;
}
@end