最近忙于android和iOS的项目,写完了android的DES 的ECB模式加密解密(相关连接:http://blog.csdn.net/vipa1888/article/details/8086037),又 回到了Ios上面,因为本人也是最近今年开始研究ios的,所以Ios上面好多东西都不懂,进过了半年的研究,终于吧ios的DES 的ECB模式对称加密解密搞定了,本人遇到的问题很严重的问题,网上写的好多16进制数转化位字节都有问题的,经过本人研究发现他们有一个地方写错了,导 致解密后的NSString 位null,我的代码已经修复了这个问题,下面贴出源代码供大家参考:
首先贴出加密类的头文件:
// // DesUtil.h // Author:spring sky // QQ:840950105 // Email:[email protected] // #import <Foundation/Foundation.h> @interface DesUtil : NSObject /** DES加密 */ +(NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key; /** DES解密 */ +(NSString *) decryptUseDES:(NSString *)plainText key:(NSString *)key; @end
加密类的实现类:
// // DesUtil.m // Author:spring sky // QQ:840950105 // Email:[email protected] // #import "DesUtil.h" #import <CommonCrypto/CommonCryptor.h> #import "ConverUtil.h" @implementation DesUtil static Byte iv[] = {1,2,3,4,5,6,7,8}; /* DES加密 */ +(NSString *) encryptUseDES:(NSString *)clearText key:(NSString *)key { NSString *ciphertext = nil; NSData *textData = [clearText dataUsingEncoding:NSUTF8StringEncoding]; NSUInteger dataLength = [clearText length]; unsigned char buffer[1024]; memset(buffer, 0, sizeof(char)); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionECBMode, [key UTF8String], kCCKeySizeDES, iv, [textData bytes] , dataLength, buffer, 1024, &numBytesEncrypted); if (cryptStatus == kCCSuccess) { NSLog(@"DES加密成功"); NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted]; Byte* bb = (Byte*)[data bytes]; ciphertext = [ConverUtil parseByteArray2HexString:bb]; }else{ NSLog(@"DES加密失败"); } return ciphertext; } /** DES解密 */ +(NSString *) decryptUseDES:(NSString *)plainText key:(NSString *)key { NSString *cleartext = nil; NSData *textData = [ConverUtil parseHexToByteArray:plainText]; NSUInteger dataLength = [textData length]; unsigned char buffer[1024]; memset(buffer, 0, sizeof(char)); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES, kCCOptionECBMode, [key UTF8String], kCCKeySizeDES, iv, [textData bytes] , dataLength, buffer, 1024, &numBytesEncrypted); if (cryptStatus == kCCSuccess) { NSLog(@"DES解密成功"); NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted]; cleartext = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; }else{ NSLog(@"DES解密失败"); } return cleartext; } @end
下面是转码类,主要是16进制和字节的转化
头文件:
// // ConverUtil.h // Author:spring sky // QQ:840950105 // Email:[email protected] // #import <Foundation/Foundation.h> @interface ConverUtil : NSObject /** 64编码 */ +(NSString *)base64Encoding:(NSData*) text; /** 字节转化为16进制数 */ +(NSString *) parseByte2HexString:(Byte *) bytes; /** 字节数组转化16进制数 */ +(NSString *) parseByteArray2HexString:(Byte[]) bytes; /* 将16进制数据转化成NSData 数组 */ +(NSData*) parseHexToByteArray:(NSString*) hexString; @end
实现类:
// // ConverUtil.m // Author:spring sky // QQ:840950105 // Email:[email protected] // #import "ConverUtil.h" @implementation ConverUtil static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; /** 64编码 */ +(NSString *)base64Encoding:(NSData*) text { if (text.length == 0) return @""; char *characters = malloc(text.length*3/2); if (characters == NULL) return @""; int end = text.length - 3; int index = 0; int charCount = 0; int n = 0; while (index <= end) { int d = (((int)(((char *)[text bytes])[index]) & 0x0ff) << 16) | (((int)(((char *)[text bytes])[index + 1]) & 0x0ff) << 8) | ((int)(((char *)[text 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 == text.length - 2) { int d = (((int)(((char *)[text bytes])[index]) & 0x0ff) << 16) | (((int)(((char *)[text 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 == text.length - 1) { int d = ((int)(((char *)[text 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; } /** 字节转化为16进制数 */ +(NSString *) parseByte2HexString:(Byte *) bytes { NSMutableString *hexStr = [[NSMutableString alloc]init]; int i = 0; if(bytes) { while (bytes[i] != ‘\0‘) { NSString *hexByte = [NSString stringWithFormat:@"%x",bytes[i] & 0xff];///16进制数 if([hexByte length]==1) [hexStr appendFormat:@"0%@", hexByte]; else [hexStr appendFormat:@"%@", hexByte]; i++; } } NSLog(@"bytes 的16进制数为:%@",hexStr); return hexStr; } /** 字节数组转化16进制数 */ +(NSString *) parseByteArray2HexString:(Byte[]) bytes { NSMutableString *hexStr = [[NSMutableString alloc]init]; int i = 0; if(bytes) { while (bytes[i] != ‘\0‘) { NSString *hexByte = [NSString stringWithFormat:@"%x",bytes[i] & 0xff];///16进制数 if([hexByte length]==1) [hexStr appendFormat:@"0%@", hexByte]; else [hexStr appendFormat:@"%@", hexByte]; i++; } } NSLog(@"bytes 的16进制数为:%@",hexStr); return [hexStr uppercaseString]; } /* 将16进制数据转化成NSData 数组 */ +(NSData*) parseHexToByteArray:(NSString*) hexString { int j=0; Byte bytes[hexString.length]; for(int i=0;i<[hexString length];i++) { int int_ch; /// 两位16进制数转化后的10进制数 unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16) int int_ch1; if(hex_char1 >= ‘0‘ && hex_char1 <=‘9‘) int_ch1 = (hex_char1-48)*16; //// 0 的Ascll - 48 else if(hex_char1 >= ‘A‘ && hex_char1 <=‘F‘) int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65 else int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97 i++; unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位) int int_ch2; if(hex_char2 >= ‘0‘ && hex_char2 <=‘9‘) int_ch2 = (hex_char2-48); //// 0 的Ascll - 48 else if(hex_char2 >= ‘A‘ && hex_char1 <=‘F‘) int_ch2 = hex_char2-55; //// A 的Ascll - 65 else int_ch2 = hex_char2-87; //// a 的Ascll - 97 int_ch = int_ch1+int_ch2; bytes[j] = int_ch; ///将转化后的数放入Byte数组里 j++; } NSData *newData = [[NSData alloc] initWithBytes:bytes length:hexString.length/2]; NSLog(@"newData=%@",newData); return newData; } @end
测试结果:
2012-10-18 19:58:41.592 [3356:707] 明文 = 1PALMPAY key = 12345678
2012-10-18 19:58:41.599 [3356:707] DES加密成功
2012-10-18 19:58:41.601 [3356:707] bytes 的16进制数为:0be9d717b1478b32
2012-10-18 19:58:41.603 [3356:707] 加密后的数据:0BE9D717B1478B32
2012-10-18 19:58:41.604 [3356:707] newData=<0be9d717 b1478b32>
2012-10-18 19:58:41.607 [3356:707] DES解密成功
2012-10-18 19:58:41.608 [3356:707] 解密后的数据:1PALMPAY
本人的工作内容主要是银行金融方面,所以经常会使用到各种的加密解密,如果有需要帮助的朋友可以联系我,如果我能搞定的,一定尽力而已!
欢迎转载,但是必须保留版权!
原文地址:http://blog.csdn.net/springsky_/article/details/8087165
版权归原作者所有,转载留作学习
【转】 iOS DES ECB模式对称加密解密,布布扣,bubuko.com