//須要導入 #import <CommonCrypto/CommonCryptor.h>ios
==============MD5加密============數組
NSString *str = @"加密的內容";app
//轉換成C語言的字符串函數
const char *cStr=[str UTF8String];fetch
//MD5加密的結果是128位,須要開闢一個16字節的空間加密
unsigned char result[16];spa
//調用加密函數3d
CC_MD5(cStr, (unsigned int)strlen(cStr), result);code
//經過上面這個方法獲取的MD5是一個16個字符的數組,須要轉換成32位的MD5值orm
NSMutableString *string = [NSMutableString stringWithCapacity:10];
for (int i=0; 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:0];
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 = 0;
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 = 0;
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]);