使用3DES+Base64来加密传输iOS应用数据

本文转载至 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:%@\n%@ --- %@ --- %@",encryptDefaultKey.encryptKey, 
                            orig, encryptString, decryptString);


//custom key
ER3DESEncrypt *encryptCustomKey = [[ER3DESEncrypt alloc] initWithKey:key];

encryptString = [encryptCustomKey encryptString:orig];
decryptString = [encryptCustomKey decryptString:encryptString];
NSLog(@"key:%@\n%@ --- %@ --- %@",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:%@\n%@ --- %@ --- %@", key, orig, encryptString, decryptString);
  • Server (php)

    ^_^ , open Crypt3Des.php ...

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。