AES(The Advanced Encryption Standard)
AES是美国国家标准与技术研究所用于加密电子数据的规范。它被预期能成为人们公认的加密包括金融、电信和政府数字信息的方法
AES 是一个迭代的、对称密钥分组的密码,它可以使用128、192 和 256 位密钥,并且用 128 位(16字节)分组加密和解密数据。与公共密钥密码使用密钥对不同,对称密钥密码使用相同的密钥加密和解密数据。通过分组密码返回的加密数据 的位数与输入数据相同。迭代加密使用一个循环结构,在该循环中重复置换(permutations )和替换(substitutions)输入数据。
目前在商业App中使用的最多的是RSA与AES加密方式,两种加密各种所长,所幸目前都有接触过。
iOS中的使用
新建MySecurity类,继承于NSObject
.h文件
<pre><code class='Xcode'>
import <Foundation/Foundation.h>
@interface MySecurity : NSObject
pragma mark -根据密匙初始化
-(instancetype) initWithKey:(NSString *) key;
pragma mark -加密
-(NSString *) AES256EncryptWithString:(NSString *) str;
pragma mark -解密
-(NSString *) AES256DecryptWithString:(NSString *) str;
pragma mark -获取安全密匙
+(NSString*) getSecurityKey;
@end
</code></pre>
.m文件
<pre><code class='Xcode'>
import "MySecurity.h"
import "NSData+AES256.h"
import "NSString+MD5.h"
@interface MySecurity ()
@property(strong,nonatomic) NSString *key;
@end
@implementation MySecurity
pragma mark -获取安全钥匙
+(NSString*) getSecurityKey
{
return @"加密的密匙";
}
pragma mark -更加密码密匙初始化
-(instancetype) initWithKey:(NSString *) key
{
self = [super init];
if (self)
{
self.key = key;
}
return self;
}
pragma mark -加密
-(NSString *) AES256EncryptWithString:(NSString *) str
{
NSData *dt1 = [str dataUsingEncoding:NSUTF8StringEncoding];
NSData *dt2 = [dt1 AES256EncryptWithKey:[self.key MD5]];
NSString *str2 = [dt2 base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
return str2;
}
pragma mark -解密
-(NSString *) AES256DecryptWithString:(NSString *) str
{
NSData *dt3 = [[NSData alloc] initWithBase64EncodedString:str options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSData *dt4 = [dt3 AES256DecryptWithKey:[self.key MD5]];
NSString *str4 = [[NSString alloc] initWithData:dt4 encoding:NSUTF8StringEncoding];
return str4;
}
@end
</code></pre>
需要用到的NSData+AES256分类与NSString+MD5分类
<pre><code class='Xcode'>
//---------------NSData (AES256)头文件------------
import <Foundation/Foundation.h>
@interface NSData (AES256)
/*
加密
(NSString*)key 32位秘钥
返回加密后的 NSData
*/
- (NSData)AES256EncryptWithKey:(NSString)key ;
/*
解密
(NSString*)key 32位秘钥
返回解密后的 NSData
*/
- (NSData)AES256DecryptWithKey:(NSString)key ;
@end
//---------------NSData (AES256)实现文件------------
import "NSData+AES256.h"
import <CommonCrypto/CommonCryptor.h>
@implementation NSData (AES256)
-
(NSData)AES256EncryptWithKey:(NSString)key {
char keyPtr[kCCKeySizeAES256 +1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)[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|kCCOptionECBMode,
keyPtr,
kCCKeySizeAES256,
NULL /* initialization vector (optional) /,
[self bytes],
dataLength, / input /
buffer,
bufferSize, / output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer);
return nil;
}
-
(NSData)AES256DecryptWithKey:(NSString)key {
char keyPtr[kCCKeySizeAES256+1 ]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[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|kCCOptionECBMode,
keyPtr,
kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[self bytes],
dataLength, /* input */
buffer,
bufferSize, /* output */
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer); //free the buffer;
return nil;
}
@end
//---------------NSString (MD5)头文件------------
import <Foundation/Foundation.h>
@interface NSString (MD5)
/*
获取字符串的MD5值
返回32位MD5值
/
-(NSString) MD5;
@end
//---------------NSString (MD5)实现文件------------
import "NSString+MD5.h"
import <CommonCrypto/CommonDigest.h>
@implementation NSString (MD5)
pragma mark -MD5加密
-(NSString*) MD5
{
const char * cStrValue = [self UTF8String];
unsigned char theResult[CC_MD5_DIGEST_LENGTH];
CC_MD5(cStrValue, (CC_LONG)strlen(cStrValue), theResult);
return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
theResult[0], theResult[1], theResult[2], theResult[3],
theResult[4], theResult[5], theResult[6], theResult[7],
theResult[8], theResult[9], theResult[10], theResult[11],
theResult[12], theResult[13], theResult[14], theResult[15]];
}
@end
</code></pre>
使用
<pre><code class='Xcode'>
//初始化对象
MySecurity *security = [[MySecurity alloc] initWithKey:[MySecurity getSecurityKey]];
//加密
NSString * str1 = [security AES256EncryptWithString:@"要加密的内容"];
//解密
NSString * str2 = [security AES256DecryptWithString:@"要解密的内容"];
</code></pre>
总的来说,使用还是很简单的,希望能帮助到大家。
end
网友评论