AES:高级加密标准(Advanced Encryption Standard)。在密码学中又称Rijndael加密法,是美国联邦*采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一(百度可以查到)。
加密步骤:字符串经过AES加密得到NSData类型,然后在对加密后的NSData类型进行Base64转码,得出最终的字符串。
解密步骤:对要解密的字符串进行Base64解码,然后进行解密,得出原字符串。
首先创建一个NSData+JKEncrypt类别,写完的.h文件如下:
#import <Foundation/Foundation.h>
@interface NSData (JKEncrypt)
/**
* 利用AES加密数据
*
* @param key key
* @param iv iv description
*
* @return data
*/
- (NSData *)jk_encryptedWithAESUsingKey:(NSString*)key andIV:(NSData*)iv;
/**
* @brief 利用AES解密据
*
* @param key key
* @param iv iv
*
* @return 解密后数据
*/
- (NSData *)jk_decryptedWithAESUsingKey:(NSString*)key andIV:(NSData*)iv;
/**
* 利用3DES加密数据
*
* @param key key
* @param iv iv description
*
* @return data
*/
- (NSData *)jk_encryptedWith3DESUsingKey:(NSString*)key andIV:(NSData*)iv;
/**
* @brief 利用3DES解密数据
*
* @param key key
* @param iv iv
*
* @return 解密后数据
*/
- (NSData *)jk_decryptedWith3DESUsingKey:(NSString*)key andIV:(NSData*)iv;
.m文件导入相应的头文件并且实现这对应的方法
#import "NSData+JKEncrypt.h"
#import <CommonCrypto/CommonCryptor.h>
@implementation NSData (JKEncrypt)
/**
* 利用AES加密数据
*
* @param key key
* @param iv iv description
*
* @return data
*/
- (NSData*)jk_encryptedWithAESUsingKey:(NSString*)key andIV:(NSData*)iv {
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
size_t dataMoved;
NSMutableData *encryptedData = [NSMutableData dataWithLength:self.length + kCCBlockSizeAES128];
CCCryptorStatus status = CCCrypt(kCCEncrypt, // kCCEncrypt or kCCDecrypt
kCCAlgorithmAES128,
kCCOptionPKCS7Padding, // Padding option for CBC Mode
keyData.bytes,
keyData.length,
iv.bytes,
self.bytes,
self.length,
encryptedData.mutableBytes, // encrypted data out
encryptedData.length,
&dataMoved); // total data moved
if (status == kCCSuccess) {
encryptedData.length = dataMoved;
return encryptedData;
}
return nil;
}
/**
* @brief 利用AES解密据
*
* @param key key
* @param iv iv
*
* @return 解密后数据
*/
- (NSData*)jk_decryptedWithAESUsingKey:(NSString*)key andIV:(NSData*)iv {
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
size_t dataMoved;
NSMutableData *decryptedData = [NSMutableData dataWithLength:self.length + kCCBlockSizeAES128];
CCCryptorStatus result = CCCrypt(kCCDecrypt, // kCCEncrypt or kCCDecrypt
kCCAlgorithmAES128,
kCCOptionPKCS7Padding, // Padding option for CBC Mode
keyData.bytes,
keyData.length,
iv.bytes,
self.bytes,
self.length,
decryptedData.mutableBytes, // encrypted data out
decryptedData.length,
&dataMoved); // total data moved
if (result == kCCSuccess) {
decryptedData.length = dataMoved;
return decryptedData;
}
return nil;
}
/**
* 利用3DES加密数据
*
* @param key key
* @param iv iv description
*
* @return data
*/
- (NSData*)jk_encryptedWith3DESUsingKey:(NSString*)key andIV:(NSData*)iv {
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
size_t dataMoved;
NSMutableData *encryptedData = [NSMutableData dataWithLength:self.length + kCCBlockSize3DES];
CCCryptorStatus result = CCCrypt(kCCEncrypt, // kCCEncrypt or kCCDecrypt
kCCAlgorithm3DES,
kCCOptionPKCS7Padding, // Padding option for CBC Mode
keyData.bytes,
keyData.length,
iv.bytes,
self.bytes,
self.length,
encryptedData.mutableBytes, // encrypted data out
encryptedData.length,
&dataMoved); // total data moved
if (result == kCCSuccess) {
encryptedData.length = dataMoved;
return encryptedData;
}
return nil;
}
/**
* @brief 利用3DES解密数据
*
* @param key key
* @param iv iv
*
* @return 解密后数据
*/
- (NSData*)jk_decryptedWith3DESUsingKey:(NSString*)key andIV:(NSData*)iv {
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
size_t dataMoved;
NSMutableData *decryptedData = [NSMutableData dataWithLength:self.length + kCCBlockSize3DES];
CCCryptorStatus result = CCCrypt(kCCDecrypt, // kCCEncrypt or kCCDecrypt
kCCAlgorithm3DES,
kCCOptionPKCS7Padding, // Padding option for CBC Mode
keyData.bytes,
keyData.length,
iv.bytes,
self.bytes,
self.length,
decryptedData.mutableBytes, // encrypted data out
decryptedData.length,
&dataMoved); // total data moved
if (result == kCCSuccess) {
decryptedData.length = dataMoved;
return decryptedData;
}
return nil;
}