高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。
1.字符串加密(http://www.seacha.com/tools/aes.html)
原始字符串:SuperDo.Team
加密后字符串:f3de96947b786e45fe338f06e2baeb2a
2.字符串解密(iOS)
[1].对NSData进行拓展
NSData+AES128.h
@interface NSData (AES128)
- (NSData *)AES128_encrypt:(NSString *)key padding:(NSString *)pad;;
- (NSData *)AES128_decrypt:(NSString *)key padding:(NSString *)pad;;
@end
NSData+AES128.m
#import "NSData+AES128.h"
@implementation NSData (AES128)
- (NSData *)AES128_encrypt:(NSString *)key padding:(NSString *)pad; //加密
{
char keyPtr[kCCKeySizeAES128+1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,
kCCOptionPKCS7Padding ,
keyPtr, kCCBlockSizeAES128,
[pad UTF8String],
[self bytes], dataLength,
buffer, bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer);
return nil;
}
- (NSData *)AES128_decrypt:(NSString *)key padding:(NSString *)pad; //解密
{
char keyPtr[kCCKeySizeAES128+1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128,
kCCOptionPKCS7Padding ,
keyPtr, kCCBlockSizeAES128,
[pad UTF8String],
[self bytes], dataLength,
buffer, bufferSize,
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer);
return nil;
}
@end
[2].对NSString进行拓展
NSString+AES128.h
#import
#import
@interface NSString (AES128)
-(NSString *) AES128_decrypt:(NSString *)key padding:(NSString *)pad;
-(NSString *) AES128_encrypt:(NSString *)key padding:(NSString *)pad;;
@end
NSString+AES128.m
#import "NSString+AES128.h"
#import "NSData+AES128.h"
@implementation NSString (AES128)
-(NSString *) AES128_encrypt:(NSString *)key padding:(NSString *)pad;
{
const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:self.length];
//对数据进行加密
NSData *result = [data AES128_encrypt:key padding:pad];
//转换为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 *) AES128_decrypt:(NSString *)key padding:(NSString *)pad;
{
//转换为2进制Data
NSMutableData *data = [NSMutableData dataWithCapacity:self.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:i*2];
byte_chars[1] = [self characterAtIndex:i*2+1];
whole_byte = strtol(byte_chars, NULL, 16);
[data appendBytes:&whole_byte length:1];
}
//对数据进行解密
NSData* result = [data AES128_decrypt:key padding:pad];
if (result && result.length > 0) {
return [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
}
return nil;
}
@end
3.验证
main.m
#import
#import "NSString+AES128.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
NSString *str = [@"f3de96947b786e45fe338f06e2baeb2a" AES128_decrypt:@"123456" padding:@"8888888877777777"];
NSLog(@"%@",str);
}
return 0;
}
验证结果:
2016-01-18 19:27:48.703 AES_128_CBC[30636:2351517] Hello, World!
2016-01-18 19:27:48.704 AES_128_CBC[30636:2351517] SuperDo.Team
Program ended with exit code: 0
完美的搞定AES128_CBC模式加密了!
本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 )
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/5140243.html