iOS开发:用DES对字符串加解密
参考http://www.cnblogs.com/janken/archive/2012/04/05/2432930.html,做了个小修改,实现PHP,JAVA,Objective-c加解密结果相同。
原先Android版的客户端与服务端(PHP)通讯部分内容用DES加密,加密方法:http://www.pocketdigi.com/20121112/940.html
直接拷贝原文的源码,发现结果不同,原因在于,我在java和php里用的IvParameterSpec是动态的,就是key转成byte[],但原文是静态写死的。
上修改后的源码:
先是Base64:
Base64.h:
- #import <Foundation/Foundation.h>
- @interface Base64 : NSObject
- +(int)char2Int:(char)c;
- +(NSData *)decode:(NSString *)data;
- +(NSString *)encode:(NSData *)data;
- @end
Base64.m
- #import "Base64.h"
- static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- @interface Base64()
- +(int)char2Int:(char)c;
- @end
- @implementation Base64
- +(NSString *)encode:(NSData *)data
- {
- if (data.length == 0)
- return nil;
- char *characters = malloc(data.length * 3 / 2);
- if (characters == NULL)
- return nil;
- int end = data.length - 3;
- int index = 0;
- int charCount = 0;
- int n = 0;
- while (index <= end) {
- int d = (((int)(((char *)[data bytes])[index]) & 0x0ff) << 16)
- | (((int)(((char *)[data bytes])[index + 1]) & 0x0ff) << 8)
- | ((int)(((char *)[data bytes])[index + 2]) & 0x0ff);
- characters[charCount++] = encodingTable[(d >> 18) & 63];
- characters[charCount++] = encodingTable[(d >> 12) & 63];
- characters[charCount++] = encodingTable[(d >> 6) & 63];
- characters[charCount++] = encodingTable[d & 63];
- index += 3;
- if(n++ >= 14)
- {
- n = 0;
- characters[charCount++] = ' ';
- }
- }
- if(index == data.length - 2)
- {
- int d = (((int)(((char *)[data bytes])[index]) & 0x0ff) << 16)
- | (((int)(((char *)[data bytes])[index + 1]) & 255) << 8);
- characters[charCount++] = encodingTable[(d >> 18) & 63];
- characters[charCount++] = encodingTable[(d >> 12) & 63];
- characters[charCount++] = encodingTable[(d >> 6) & 63];
- characters[charCount++] = '=';
- }
- else if(index == data.length - 1)
- {
- int d = ((int)(((char *)[data bytes])[index]) & 0x0ff) << 16;
- characters[charCount++] = encodingTable[(d >> 18) & 63];
- characters[charCount++] = encodingTable[(d >> 12) & 63];
- characters[charCount++] = '=';
- characters[charCount++] = '=';
- }
- NSString * rtnStr = [[NSString alloc] initWithBytesNoCopy:characters length:charCount encoding:NSUTF8StringEncoding freeWhenDone:YES];
- return rtnStr;
- }
- +(NSData *)decode:(NSString *)data
- {
- if(data == nil || data.length <= 0) {
- return nil;
- }
- NSMutableData *rtnData = [[NSMutableData alloc]init];
- int slen = data.length;
- int index = 0;
- while (true) {
- while (index < slen && [data characterAtIndex:index] <= ' ') {
- index++;
- }
- if (index >= slen || index + 3 >= slen) {
- break;
- }
- int byte = ([self char2Int:[data characterAtIndex:index]] << 18) + ([self char2Int:[data characterAtIndex:index + 1]] << 12) + ([self char2Int:[data characterAtIndex:index + 2]] << 6) + [self char2Int:[data characterAtIndex:index + 3]];
- Byte temp1 = (byte >> 16) & 255;
- [rtnData appendBytes:&temp1 length:1];
- if([data characterAtIndex:index + 2] == '=') {
- break;
- }
- Byte temp2 = (byte >> 8) & 255;
- [rtnData appendBytes:&temp2 length:1];
- if([data characterAtIndex:index + 3] == '=') {
- break;
- }
- Byte temp3 = byte & 255;
- [rtnData appendBytes:&temp3 length:1];
- index += 4;
- }
- return rtnData;
- }
- +(int)char2Int:(char)c
- {
- if (c >= 'A' && c <= 'Z') {
- return c - 65;
- } else if (c >= 'a' && c <= 'z') {
- return c - 97 + 26;
- } else if (c >= '0' && c <= '9') {
- return c - 48 + 26 + 26;
- } else {
- switch(c) {
- case '+':
- return 62;
- case '/':
- return 63;
- case '=':
- return 0;
- default:
- return -1;
- }
- }
- }
- @end
DESUtils.h:
- #import <Foundation/Foundation.h>
- #import <CommonCrypto/CommonCrypto.h>
- #import "Base64.h"
- @interface DESUtils : NSObject
- +(NSString *)decryptUseDES:(NSString *)cipherText key:(NSString *)key;
- +(NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key;
- @end
DESUtils.m:
- #import "DESUtils.h"
- @implementation DESUtils
- +(NSString *)decryptUseDES:(NSString *)cipherText key:(NSString *)key
- {
- NSString *plaintext = nil;
- NSData *cipherdata = [Base64 decode:cipherText];
- unsigned char buffer[1024];
- memset(buffer, 0, sizeof(char));
- size_t numBytesDecrypted = 0;
- CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES,
- kCCOptionPKCS7Padding,
- [key UTF8String], kCCKeySizeDES,
- (Byte *)[[key dataUsingEncoding:NSUTF8StringEncoding] bytes],
- [cipherdata bytes], [cipherdata length],
- buffer, 1024,
- &numBytesDecrypted);
- if(cryptStatus == kCCSuccess) {
- NSData *plaindata = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
- plaintext = [[NSString alloc]initWithData:plaindata encoding:NSUTF8StringEncoding];
- }
- return plaintext;
- }
- +(NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key
- {
- NSString *ciphertext = nil;
- NSData *textData = [plainText dataUsingEncoding:NSUTF8StringEncoding];
- NSUInteger dataLength = [textData length];
- unsigned char buffer[1024];
- memset(buffer, 0, sizeof(char));
- size_t numBytesEncrypted = 0;
- CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
- kCCOptionPKCS7Padding,
- [key UTF8String], kCCKeySizeDES,
- (Byte *)[[key dataUsingEncoding:NSUTF8StringEncoding] bytes],
- [textData bytes], dataLength,
- buffer, 1024,
- &numBytesEncrypted);
- if (cryptStatus == kCCSuccess) {
- NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
- ciphertext = [Base64 encode:data];
- }
- return ciphertext;
- }
- @end
© 2014, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记
iOS开发:用DES对字符串加解密的更多相关文章
- 李洪强iOS开发Swift篇—03_字符串和数据类型
李洪强iOS开发Swift篇—03_字符串和数据类型 一.字符串 字符串是String类型的数据,用双引号""包住文字内容 let website = "http:// ...
- android中使用jni对字符串加解密实现分析
android中使用jni对字符串加解密实现分析 近期项目有个需求.就是要对用户的敏感信息进行加密处理,比方用户的账户password,手机号等私密信息.在java中,就对字符串的加解密我们能够使用A ...
- iOS开发-通过正则表达式判断字符串是否为纯阿拉伯数字
iOS开发-通过正则表达式判断字符串是否为纯阿拉伯数字 简述:NSString * regex_0 = @"\\d{1,}"; /*允许首位为0*/ NSString * re ...
- DES跨(C# Android IOS)三个平台通用的加解密方法
#region 跨平台加解密(c# 安卓 IOS) // public static string sKey = "12345678"; ...
- iOS - (base64对字符串加解密)
今天公司让做支付系统,为了安全起见,需要对一些数据进行加密,然而我首想到的就是 base64 ,严格来说这不是一种加密方式,这只是将原有的一些字符串或者其它的一些文本进行一个转化而已,就是转化成数字, ...
- DES,AeS加解密,MD5,SHA加密
1.DES一共就有4个参数参与运作:明文.密文.密钥.向量.其中这4者的关系可以理解为: 密文=明文+密钥+向量: 明文=密文-密钥-向量: 为什么要向量这个参数呢?因为如果有一篇文章,有几个词重复, ...
- 在ios开发中有多少常用的加密解密方式(备用)
最常用的是MD5和base64编码,还有DES 3DES AES加密 ios怎么实现RAS加密解密 最近几天折腾了一下如何在iOS上使用RSA来加密.iOS上并没有直接的RSA加密API.但是iOS提 ...
- [IOS 开发] NSDateFormatter的格式字符串 -- 《整理的笔记》
在ios开发中, OBjective-C中的NSDate是一个挺讨厌的类型, 自己找不到转换成字符串的类型,还得带一个NSDateFormatter的类型. 官方文档上对NSDateFormatter ...
- C#常用字符串加解密方法封装
C#中常用的字符串加密.解密方法封装,包含只加密但不解密的方法.收藏起来备用. //方法一 //须添加对System.Web的引用 //using System.Web.Security; /// & ...
随机推荐
- Codeforces 671D. Roads in Yusland(树形DP+线段树)
调了半天居然还能是线段树写错了,药丸 这题大概是类似一个树形DP的东西.设$dp[i]$为修完i这棵子树的最小代价,假设当前点为$x$,但是转移的时候我们不知道子节点到底有没有一条越过$x$的路.如果 ...
- vim切换显示器乱行问题解决
http://note.youdao.com/noteshare?id=ccdad950ca154a6b1597cbe2ede07b81
- linux shell 通配符
http://note.youdao.com/noteshare?id=4b6bc019e055c897c6dfb81fe2c17756
- 轮廓问题/Outline Problem-->改进的算法及时间复杂度分析
前面写过一篇关于轮廓算法的文章,是把合并建筑和合并轮廓是分开对待的,并且为了使轮廓合并的时候算法简单,对x坐标使用了double类型,然后对整形的x坐标数据进行合并.这样做是为了使得需找拐点的算法容易 ...
- LeakCanary原理分析
参考文档 http://blog.csdn.net/wyfei021/article/details/46506521http://vjson.com/wordpress/leakcanary%e6% ...
- CentOS7.2下安装mongoDB3.2.8
最近在又在倒腾MongoDB,把安装配置的相关命令贴出来 1.下载 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70- ...
- Oracle中查询当前数据库中的所有表空间和对应的数据文件语句命令
Oracle中查询当前数据库中的所有表空间和对应的数据文件语句命令 ------------------------------------------------------------------ ...
- CF839 C 树形DP 期望
给一颗树,求从根出发路径长度的期望是多少. 树形DP 要想清楚期望的计算 /** @Date : 2017-08-12 23:09:41 * @FileName: C.cpp * @Platform: ...
- 那些让 Web 开发者们深感意外的事情
作为 Web 开发者,对自己的行业前景,人人都有自己的看法,然而,任何行业都有出人意料的地方.著名的 Web 开发设计博客 Nope.com 曾向他们的读者做了一个调查,请他们列举 Web 开发领域那 ...
- 【BZOJ】1690: [Usaco2007 Dec]奶牛的旅行
[算法]01分数规划-最优比率环 [题意]给定有向图,点有收益,边有代价,重复经过的话收益不叠加而代价叠加,求从任意点开始最后回归该点的(收益/代价)最大. [题解] 和普通的分数规划不同,这里的方案 ...