本文转载至 http://www.erblah.com/post/objective-c/shi-yong-3des-base64lai-jia-mi-chuan-shu-iosying-yong-shu-ju
app与服务端进行数据传输有时需要加密,以免出现某知名新闻客户端密码只是md5一下就往服务器post的窘况...
ios加密的情况有三种:1.https 2.对称加密 3.非对称加密,因为种种原因,在一个应用中用到了3DES对称加密(据说AES更好),感觉挺方便的。
代码做了一些整理,提供了object和NSString+Category两种实现方式,同时附server端的加密和解密的代码(php实现),有需要的同学可以拿去用。
Github地址:ER3DESEncryptAndServer
使用说明
ER3DESEncryptAndServer
useage
- use ER3DESEncrypt
#import "ER3DESEncrypt.h"
...
NSString *orig = @"erblah.com", *encryptString, *decryptString, *key = @"I love you.";
//default key: abcd12345678901234567890
ER3DESEncrypt *encryptDefaultKey = [[ER3DESEncrypt alloc] init];
encryptString = [encryptDefaultKey encryptString:orig];
decryptString = [encryptDefaultKey decryptString:encryptString];
NSLog(@"key:%@
%@ --- %@ --- %@",encryptDefaultKey.encryptKey,
orig, encryptString, decryptString);
//custom key
ER3DESEncrypt *encryptCustomKey = [[ER3DESEncrypt alloc] initWithKey:key];
encryptString = [encryptCustomKey encryptString:orig];
decryptString = [encryptCustomKey decryptString:encryptString];
NSLog(@"key:%@
%@ --- %@ --- %@",encryptDefaultKey.encryptKey,
orig, encryptString, decryptString);
- use Category(NSString+Encrypt3DESandBase64)
#import "NSString+Encrypt3DESandBase64.h"
...
NSString *orig = @"erblah.com", *encryptString, *decryptString, *key = @"I love you.";
encryptString = [orig encryptStringWithKey:key];
decryptString = [encryptString decryptStringWithKey:key];
NSLog(@"key:%@
%@ --- %@ --- %@", key, orig, encryptString, decryptString);
-
Server (php)
^_^ , open Crypt3Des.php ...