1: 對稱密碼算法 ---- AES摘要算法
分割線
2:摘要算法
摘要算法竭翠,具有單向不可逆
的基本性質(zhì)俘侠,速度快象缀。
① 消息摘要算法MD5
MD5算法將任意明文(不為空)映射位32位字符串。數(shù)字簽名和復(fù)雜的加密系統(tǒng)中都有使用爷速,單獨使用由于撞庫原因安全性較低央星。
新建NSString的分類,添加加密所需的頭文件
#import <CommonCrypto/CommonDigest.h>
NSString的MD5方法:
NSString+MD5.h:
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>
@interface NSString (MD5)
+ (NSString *)md5To32bit:(NSString *)str;
@end
#import "NSString+MD5.h"
@implementation NSString (MD5)
+ (NSString *)md5To32bit:(NSString *)str {
const char *cStr = [str UTF8String];
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, strlen(cStr),digest );
NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[result appendFormat:@"%02x", digest[i]];
return result;
}
- 使用
#import "ViewController.h"
#import "NSString+MD5.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *name = @"蘇慶林";
//加密字符串
NSString *encypt = [NSString md5To32bit:name];
NSLog(@"\n name: %@\n encypt: %@",name,encypt);
// 7f7b7adc32fffe9afcaaefc1d63e8686
}
NSData 的MD5 (參考:md5的坑)
import “NSData+MD5.h”
#import <CommonCrypto/CommonCrypto.h>
@implementation NSData (MD5)
– (NSString *)getMD5Data{
//1: 創(chuàng)建一個MD5對象
CC_MD5_CTX md5;
//2: 初始化MD5
CC_MD5_Init(&md5);
//3: 準(zhǔn)備MD5加密
CC_MD5_Update(&md5, self.bytes, (CC_LONG)self.length);
//4: 準(zhǔn)備一個字符串?dāng)?shù)組, 存儲MD5加密之后的數(shù)據(jù)
unsigned char result[CC_MD5_DIGEST_LENGTH];
//5: 結(jié)束MD5加密
CC_MD5_Final(result, &md5);
NSMutableString *resultString = [NSMutableString string];
//6:從result數(shù)組中獲取最終結(jié)果
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
[resultString appendFormat:@”%02X”, result[i]];
}
return resultString;
}
@end
② 安全散列算法 -- SHA (貌似不再安全)
+ (NSString *)SHA1To32bit:(NSString *)str {
const char *cStr = [str UTF8String];
unsigned char digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1( cStr, strlen(cStr),digest );
NSMutableString *result = [NSMutableString stringWithCapacity: CC_SHA1_DIGEST_LENGTH];
for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
[result appendFormat:@"%02x", digest[i]];
return result;
}```
3、base64 `可逆` `>ios7`
// 加密文本
NSData *sData = [@"你好China" dataUsingEncoding:NSUTF8StringEncoding];
NSString *baseStr = [sData base64EncodedStringWithOptions:0];
// 解密文本
NSData *jimiData = [[NSData alloc]initWithBase64EncodedString:baseStr options:0];
NSString *jiemiStr = [[NSString alloc] initWithData:jimiData encoding:NSUTF8StringEncoding];
NSLog(@"加密:%@, 解密:%@", baseStr, jiemiStr);
![Snip20170426_1.png](http://upload-images.jianshu.io/upload_images/2850902-8a26ce91a26c90b0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
/******************-------******************/
// 圖片加密
NSData *data = [NSData dataWithContentsOfFile:@"/Users/suqinglin/Desktop/timg.jpeg"];
NSData *base64Data = [data base64EncodedDataWithOptions:0];
[base64Data writeToFile:@"/Users/suqinglin/Desktop/jiami" atomically:YES];
// 圖片解密
NSData *baseData = [[NSData alloc] initWithBase64EncodedData:base64Data options:0];
[baseData writeToFile:@"/Users/suqinglin/Desktop/jiami.jpeg" atomically:YES];