参考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, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接!
如何联系我:【万里虎】www.bravetiger.cn 【QQ】3396726884 (咨询问题100元起,帮助解决问题500元起) 【博客】http://www.cnblogs.com/kenshinobiy/