1MD5
創建MD5類
#import@interface CJMD5 : NSObject +(NSString *)md5HexDigest:(NSString *)input; @end
#import CJMD5.h #import@implementation CJMD5 +(NSString *)md5HexDigest:(NSString *)input{ const char* str = [input UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5(str, strlen(str), result); NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH]; for(int i = 0; i MD5是不可逆的只有加密沒有解密使用方式如下
NSString *userName = @cerastes; NSString *password = @hello Word; // MD5加密 NSString *md5 = [CJMD5 md5HexDigest:password]; NSLog(@%@,md5);
2AES加密
AES加密開源代碼git下載地址
https://github.com/Gurpartap/AESCrypt-ObjC
使用方法
// AES加密 NSString *encryptedData = [AESCrypt encrypt:userName password:password];//加密 NSString *message = [AESCrypt decrypt:encryptedData password:password]; //解密 NSLog(@加密結果 = %@,encryptedData); NSLog(@解密結果 = %@,message);
3 BASE64加密
下載地址 https://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/?r=87
添加如下方法
.h
+ (NSString*)encodeBase64String:(NSString *)input; + (NSString*)decodeBase64String:(NSString *)input; + (NSString*)encodeBase64Data:(NSData *)data; + (NSString*)decodeBase64Data:(NSData *)data;
.m
+ (NSString*)encodeBase64String:(NSString * )input { NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; data = [GTMBase64 encodeData:data]; NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; return base64String; } + (NSString*)decodeBase64String:(NSString * )input { NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; data = [GTMBase64 decodeData:data]; NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; return base64String; } + (NSString*)encodeBase64Data:(NSData *)data { data = [GTMBase64 encodeData:data]; NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; return base64String; } + (NSString*)decodeBase64Data:(NSData *)data { data = [GTMBase64 decodeData:data]; NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; return base64String; }
使用方法
// BASE64加密 NSString *baseEncodeString = [GTMBase64 encodeBase64String:password]; NSString *baseDecodeString = [GTMBase64 decodeBase64String:baseEncodeString]; NSLog(@baseEncodeString = %@,baseEncodeString); NSLog(@baseDecodeString = %@,baseDecodeString);