在某些考虑数据安全的场景下,我们常常会用到加密解密、编码解码知识。比如把用户密码保存到数据库上,常用的方式是通过MD5或SHA1不可逆算法进行加密后密文保存。
这里主要介绍三种常用的加密算法:
(1)不可逆:MD5、SHA1
(2)可逆:AES256
另外常用的编码方式:
(1)可逆:Base64
main.m
1 // 2 // main.m 3 // OCEncryptionAlgorithm: UTF8String, cStringUsingEncoding, dataUsingEncoding, base64EncodedStringWithOptions, initWithBase64EncodedString, initWithData (加密算法)(扩展知识:Base64编码) 4 // 5 // Created by Kenmu on 15/5/12. 6 // Copyright (c) 2015年 Kenmu. All rights reserved. 7 // 8 9 #import10 #import "KMEncryption.h"11 12 int main(int argc, const char * argv[]) {13 @autoreleasepool {14 //-------------------------MD5加密和SHA1加密15 NSString *strSource = @"Kenmu-啊武";16 NSLog(@"“%@”的MD5加密数据为:“%@”", strSource, [KMEncryption encryptByMD5:strSource]);17 18 NSLog(@"“%@”的SHA1加密数据为:“%@”", strSource, [KMEncryption encryptBySHA1:strSource]);19 20 21 //-------------------------AES256加密解密22 NSString *strKey = @"12345678901234567890123456789012"; //strKey必须是32字节23 NSData *data = [KMEncryption encryptByAES256:strSource withKey:strKey];24 NSLog(@"“%@”的AES256加密数据为:“%@”", strSource, [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]);25 26 strSource = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];27 data = [KMEncryption decryptByAES256:strSource withKey:strKey];28 NSLog(@"“%@”的AES256解密数据为:“%@”", strSource, [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);29 30 //-------------------------Base64编码解码31 strSource = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];32 NSLog(@"“%@”的Base64编码数据为:“%@”", strSource, [KMEncryption encodeBase64String:strSource]);33 34 strSource = [KMEncryption encodeBase64String:strSource];35 NSLog(@"“%@”的Base64解码数据为:“%@”", strSource, [KMEncryption decodeBase64String:strSource]);36 }37 return 0;38 }
KMEncryption.h
1 #import2 3 @interface KMEncryption : NSObject 4 /** 5 * MD5加密 6 * 7 * @param strSource 需要被加密的明文字符串 8 * 9 * @return MD5加密后的密文字符串10 */11 + (NSString *)encryptByMD5:(NSString *)strSource;12 13 /**14 * SHA1加密15 *16 * @param strSource 需要被加密的明文字符串17 *18 * @return SHA1加密后的密文字符串19 */20 + (NSString *)encryptBySHA1:(NSString *)strSource;21 22 /**23 * AES256加密24 *25 * @param strSource 需要被加密的明文字符串26 * @param strKey 用于加密解密的32字节的密钥字符串(如不是32字节,就无法加密)27 *28 * @return AES256加密后的密文字符串29 */30 + (NSData *)encryptByAES256:(NSString *)strSource withKey:(NSString *)strKey;31 32 /**33 * AES256解密34 *35 * @param strSource 需要被解密的密文字符串36 * @param strKey 用于加密解密的32字节的密钥字符串(如不是32字节,就无法解密)37 *38 * @return AES256解密后的明文字符串39 */40 + (NSData *)decryptByAES256:(NSString *)strSource withKey:(NSString *)strKey;41 42 /**43 * Base64编码44 *45 * @param strSource 需要被编码的字符串46 *47 * @return Base64编码后的字符串48 */49 + (NSString *)encodeBase64String:(NSString *)strSource;50 51 /**52 * Base64解码53 *54 * @param strSource 需要被解码的字符串55 *56 * @return Base64解码后的字符串57 */58 + (NSString *)decodeBase64String:(NSString *)strSource;59 60 @end
KMEncryption.m
1 // 2 // KMEncryption.m 3 // OCEncryptionAlgorithm 4 // 5 // Created by Kenmu on 15/5/12. 6 // Copyright (c) 2015年 Kenmu. All rights reserved. 7 // 8 9 #import "KMEncryption.h" 10 #import11 #import 12 13 @implementation KMEncryption 14 15 typedef NS_ENUM(NSUInteger, EncryptType) { 16 MD5, 17 SHA1 18 }; 19 20 #pragma mark - 私有方法 21 /** 22 * MD5或SHA1加密 23 * 24 * @param strSource 需要被加密的明文字符串 25 * @param encryptType 加密类型;例如:MD5、SHA1 26 * 27 * @return 加密后的密文字符串 28 */ 29 + (NSString *)encrypt:(NSString *)strSource encryptType:(EncryptType)encryptType { 30 NSString *strResult = nil; 31 const char *cSource = [strSource UTF8String]; //等同于[strSource cStringUsingEncoding:NSUTF8StringEncoding] 32 CC_LONG cSourceLen = (CC_LONG)strlen(cSource); 33 NSUInteger digestLen = 16; 34 unsigned char arrOutputDigest[digestLen]; 35 36 switch (encryptType) { 37 case MD5: { 38 CC_MD5(cSource, cSourceLen, arrOutputDigest); 39 break; 40 } 41 case SHA1: { 42 CC_SHA1(cSource, cSourceLen, arrOutputDigest); 43 break; 44 } 45 default: { 46 break; 47 } 48 } 49 50 NSMutableString *mStrResult = [[NSMutableString alloc] init]; 51 for (NSUInteger i=0; i 0) { 95 strResult = [self encrypt:strSource encryptType:MD5]; 96 } 97 return strResult; 98 } 99 100 + (NSString *)encryptBySHA1:(NSString *)strSource {101 NSString *strResult = nil;102 if (strSource && strSource.length > 0) {103 strResult = [self encrypt:strSource encryptType:SHA1];104 }105 return strResult;106 }107 108 + (NSData *)encryptByAES256:(NSString *)strSource withKey:(NSString *)strKey {109 NSData *dataResult = nil;110 if (strSource && strSource.length > 0 && strKey && strKey.length == 32) {111 dataResult = [strSource dataUsingEncoding:NSUTF8StringEncoding]; //编码112 dataResult = [self operationByAES256:dataResult113 withKey:strKey114 isEncrypt:YES];115 }116 return dataResult;117 }118 119 + (NSData *)decryptByAES256:(NSString *)strSource withKey:(NSString *)strKey {120 NSData *dataResult = nil;121 if (strSource && strSource.length > 0 && strKey && strKey.length == 32) {122 dataResult = [[NSData alloc] initWithBase64EncodedString:strSource123 options:NSDataBase64DecodingIgnoreUnknownCharacters]; //解码124 dataResult = [self operationByAES256:dataResult125 withKey:strKey126 isEncrypt:NO];127 }128 return dataResult;129 }130 131 + (NSString *)encodeBase64String:(NSString *)strSource {132 NSData *data = [strSource dataUsingEncoding:NSUTF8StringEncoding];133 return [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];134 }135 136 + (NSString *)decodeBase64String:(NSString *)strSource {137 NSData *data = [[NSData alloc] initWithBase64EncodedString:strSource138 options:NSDataBase64DecodingIgnoreUnknownCharacters];139 return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];140 }141 142 @end
结果:
1 2015-05-14 00:21:36.203 OCEncryptionAlgorithm[1010:79295] “Kenmu-啊武”的MD5加密数据为:“d33ee89d8ea08c8d258df20b7836bd02”2 2015-05-14 00:21:36.204 OCEncryptionAlgorithm[1010:79295] “Kenmu-啊武”的SHA1加密数据为:“9bb7a793756842e38a76815bb03dd968”3 2015-05-14 00:21:36.204 OCEncryptionAlgorithm[1010:79295] “Kenmu-啊武”的AES256加密数据为:“1KQQ/bVAkRszh8Ue18VzMQ==”4 2015-05-14 00:21:36.204 OCEncryptionAlgorithm[1010:79295] “1KQQ/bVAkRszh8Ue18VzMQ==”的AES256解密数据为:“Kenmu-啊武”5 2015-05-14 00:21:36.204 OCEncryptionAlgorithm[1010:79295] “Kenmu-啊武”的Base64编码数据为:“S2VubXUt5ZWK5q2m”6 2015-05-14 00:21:36.205 OCEncryptionAlgorithm[1010:79295] “S2VubXUt5ZWK5q2m”的Base64解码数据为:“Kenmu-啊武”