//需要导入 #import <CommonCrypto/CommonCryptor.h>
==============MD5加密============
NSString *str = @"加密的内容";
//转换成C语言的字符串
const char *cStr=[str UTF8String];
//MD5加密的结果是128位,需要开辟一个16字节的空间
unsigned char result[16];
//调用加密函数
CC_MD5(cStr, (unsigned int)strlen(cStr), result);
//通过上面这个方法获取的MD5是一个16个字符的数组,需要转换成32位的MD5值
NSMutableString *string = [NSMutableString stringWithCapacity:10];
for (int i=; i<16; i++) {
[string appendString:[NSString stringWithFormat:@"%02x",result[i]]];
}
NSLog(@"MD5加密:%@",string);
===============base64加密解密=============
//ios7 以后提供了base64的转码方式
//加密
NSString *pass=@"加密的内容";
NSData *passData=[pass dataUsingEncoding:NSUTF8StringEncoding];
NSString *result = [passData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
NSLog(@"base64加密:%@",result);
//解密
NSData *decodeData = [[NSData alloc]initWithBase64EncodedString:result options:];
NSString *decodeStr = [[NSString alloc]initWithData:decodeData encoding:NSUTF8StringEncoding];
NSLog(@"base64解密:%@",decodeStr);
================AES加密解密===============
//新建一个NSData类,写入一下两个方法
//加密方法
- (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 = ;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
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 = ;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
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;
在另外的类里面调用上面NSData里面的方法
//==========AES加密解密=============
NSString *key = @"mykey";//钥匙
NSString *secret = @"加密内容";//准备加密的内容
NSData *plain = [secret dataUsingEncoding:NSUTF8StringEncoding];
NSData *ciper = [plain AES256EncryptWithKey:key];
NSLog(@"AES加密%@",ciper);
//解密
plain = [ciper AES256DecryptWithKey:key];
NSLog(@"AES解密:%@",[[NSString alloc]initWithData:plain encoding:NSUTF8StringEncoding]);