不可逆
同样的数据加密结果是?一样的?密码上的?一个忌讳
加点”盐”私钥
// .H // NSString+Password.h // 数据加密 // #import <Foundation/Foundation.h> @interface NSString (Password) /** “加点盐”的过程 */ -- (NSString *)myMD5; /** * 32位MD5加密 * * @return 32位MD5加密结果 */ - (NSString *)MD5; /** * SHA1加密 * * @return SHA1加密结果 */ - (NSString *)SHA1; @end
// .M // NSString+Password.m // 数据加密 // #import "NSString+Password.h" #import <CommonCrypto/CommonDigest.h> /**私钥--令牌的意思(token)*/ static NSString *token = @"fdhsahgahgoh435245dsaiubviusauiash34$%#@$@fdhsaohv^%*%^dsaghf"; @implementation NSString (Password) /** “加点盐”的过程 */ -- (NSString *)myMD5{ NSString *str = [NSString stringWithForamt:@"%@%@", self, token]; return [str MD5]; } #pragma mark 使用MD5加密字符串 - (NSString *)MD5 { const char *cStr = [self UTF8String]; unsigned char digest[CC_MD5_DIGEST_LENGTH]; CC_MD5(cStr, strlen(cStr), digest); NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) { [result appendFormat:@"%02x", digest[i]]; } return result; } #pragma mark 使用SHA1加密字符串 - (NSString *)SHA1 { const char *cStr = [self UTF8String]; NSData *data = [NSData dataWithBytes:cStr length:self.length]; uint8_t digest[CC_SHA1_DIGEST_LENGTH]; CC_SHA1(data.bytes, data.length, digest); NSMutableString *result = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2]; for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) { [result appendFormat:@"%02x", digest[i]]; } return result; } @end
#import "ViewController.h" #import "NSString+Password.h" @interface ViewController () <NSURLConnectionDataDelegate> @property (weak, nonatomic) IBOutlet UITextField *userName; @property (weak, nonatomic) IBOutlet UITextField *userPwd; @property (nonatomic, strong) NSMutableData *data; @property (nonatomic, strong) NSString *loginPwd; @end @implementation ViewController - (NSString *)loginPwd { /*******************************/ return [self.userPwd.text MD5]; //这里调用 //return [self.userPwd.text myMD5]; //私钥进一步加密,“加点盐”后的MD5加密 /*******************************/ } - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)logon { NSURL *url = [NSURL URLWithString:@"http://localhost/login.php"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f]; request.HTTPMethod = @"POST"; NSString *body = [NSString stringWithFormat:@"username=%@&password=%@", self.userName.text, self.loginPwd]; NSLog(@"%@", body); request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; [connection start]; } #pragma mark - 代理方法 #pragma mark 接收到服务器的响应 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { // 准备工作 if (!self.data) { self.data = [NSMutableData data]; } else { self.data = nil; } } #pragma mark 收到数据(有可能是一部分) - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.data appendData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { // 数据后续处理 NSString *result = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding]; NSLog(@"%@", result); } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"%@", error.localizedDescription); } @end
原文:http://my.oschina.net/227/blog/389833