iOS DES ECB模式对称加密解密

来源:互联网 发布:ftp服务使用端口 编辑:程序博客网 时间:2024/04/29 14:56

最近忙于android和iOS的项目,写完了android的DES 的ECB模式加密解密(相关连接:http://blog.csdn.net/vipa1888/article/details/8086037),又回到了Ios上面,因为本人也是最近今年开始研究ios的,所以Ios上面好多东西都不懂,进过了半年的研究,终于吧ios的DES 的ECB模式对称加密解密搞定了,本人遇到的问题很严重的问题,网上写的好多16进制数转化位字节都有问题的,经过本人研究发现他们有一个地方写错了,导致解密后的NSString 位null,我的代码已经修复了这个问题,下面贴出源代码供大家参考:


首先贴出加密类的头文件:

[plain] view plaincopyprint?
  1. //  
  2. //  DesUtil.h  
  3. //  Author:spring sky  
  4. //  QQ:840950105  
  5. //  Email:vipa1888@163.com  
  6. //  
  7.   
  8. #import <Foundation/Foundation.h>  
  9.   
  10. @interface DesUtil : NSObject  
  11. /**  
  12.  DES加密  
  13.  */  
  14. +(NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key;  
  15.   
  16. /**  
  17.  DES解密  
  18.  */  
  19. +(NSString *) decryptUseDES:(NSString *)plainText key:(NSString *)key;  
  20.   
  21.   
  22.   
  23. @end  

加密类的实现类:

[plain] view plaincopyprint?
  1. //  
  2. //  DesUtil.m  
  3. //  Author:spring sky  
  4. //  QQ:840950105  
  5. //  Email:vipa1888@163.com  
  6. //  
  7.   
  8. #import "DesUtil.h"  
  9. #import <CommonCrypto/CommonCryptor.h>  
  10. #import "ConverUtil.h"  
  11. @implementation DesUtil  
  12.   
  13.   
  14. static Byte iv[] = {1,2,3,4,5,6,7,8};  
  15. /*  
  16.  DES加密  
  17.  */  
  18. +(NSString *) encryptUseDES:(NSString *)clearText key:(NSString *)key  
  19. {  
  20.     NSString *ciphertext = nil;  
  21.     NSData *textData = [clearText dataUsingEncoding:NSUTF8StringEncoding];  
  22.     NSUInteger dataLength = [clearText length];  
  23.     unsigned char buffer[1024];  
  24.     memset(buffer, 0, sizeof(char));  
  25.     size_t numBytesEncrypted = 0;  
  26.       
  27.       
  28.     CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,  
  29.                                           kCCOptionECBMode,  
  30.                                           [key UTF8String], kCCKeySizeDES,  
  31.                                           iv,  
  32.                                           [textData bytes]  , dataLength,  
  33.                                           buffer, 1024,  
  34.                                           &numBytesEncrypted);  
  35.     if (cryptStatus == kCCSuccess) {  
  36.         NSLog(@"DES加密成功");  
  37.         NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];  
  38.         Byte* bb = (Byte*)[data bytes];  
  39.         ciphertext = [ConverUtil parseByteArray2HexString:bb];  
  40.     }else{  
  41.         NSLog(@"DES加密失败");  
  42.     }  
  43.     return ciphertext;  
  44. }  
  45.   
  46. /**  
  47.  DES解密  
  48.  */  
  49. +(NSString *) decryptUseDES:(NSString *)plainText key:(NSString *)key  
  50. {  
  51.     NSString *cleartext = nil;  
  52.     NSData *textData = [ConverUtil parseHexToByteArray:plainText];  
  53.     NSUInteger dataLength = [textData length];  
  54.     unsigned char buffer[1024];  
  55.     memset(buffer, 0, sizeof(char));  
  56.     size_t numBytesEncrypted = 0;  
  57.       
  58.       
  59.     CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES,  
  60.                                           kCCOptionECBMode,  
  61.                                           [key UTF8String], kCCKeySizeDES,  
  62.                                           iv,  
  63.                                           [textData bytes]  , dataLength,  
  64.                                           buffer, 1024,  
  65.                                           &numBytesEncrypted);  
  66.     if (cryptStatus == kCCSuccess) {  
  67.         NSLog(@"DES解密成功");  
  68.   
  69.         NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];  
  70.         cleartext = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  
  71.     }else{  
  72.         NSLog(@"DES解密失败");  
  73.     }  
  74.     return cleartext;  
  75. }  
  76.   
  77.   
  78.   
  79. @end  



下面是转码类,主要是16进制和字节的转化 
头文件:

[plain] view plaincopyprint?
  1. //  
  2. //  ConverUtil.h  
  3. //  Author:spring sky  
  4. //  QQ:840950105  
  5. //  Email:vipa1888@163.com  
  6. //  
  7.   
  8. #import <Foundation/Foundation.h>  
  9.   
  10. @interface ConverUtil : NSObject  
  11.   
  12. /**  
  13.  64编码  
  14.  */  
  15. +(NSString *)base64Encoding:(NSData*) text;  
  16.   
  17. /**  
  18.  字节转化为16进制数  
  19.  */  
  20. +(NSString *) parseByte2HexString:(Byte *) bytes;  
  21.   
  22. /**  
  23.  字节数组转化16进制数  
  24.  */  
  25. +(NSString *) parseByteArray2HexString:(Byte[]) bytes;  
  26.   
  27. /*  
  28.  将16进制数据转化成NSData 数组  
  29.  */  
  30. +(NSData*) parseHexToByteArray:(NSString*) hexString;  
  31.   
  32. @end  

实现类:

[plain] view plaincopyprint?
  1. //  
  2. //  ConverUtil.m  
  3. //  Author:spring sky  
  4. //  QQ:840950105  
  5. //  Email:vipa1888@163.com  
  6. //  
  7.   
  8. #import "ConverUtil.h"  
  9.   
  10. @implementation ConverUtil  
  11. static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";  
  12.   
  13. /**  
  14.  64编码  
  15.  */  
  16. +(NSString *)base64Encoding:(NSData*) text  
  17. {  
  18.     if (text.length == 0)  
  19.         return @"";  
  20.       
  21.     char *characters = malloc(text.length*3/2);  
  22.       
  23.     if (characters == NULL)  
  24.         return @"";  
  25.       
  26.     int end = text.length - 3;  
  27.     int index = 0;  
  28.     int charCount = 0;  
  29.     int n = 0;  
  30.       
  31.     while (index <= end) {  
  32.         int d = (((int)(((char *)[text bytes])[index]) & 0x0ff) << 16)  
  33.         | (((int)(((char *)[text bytes])[index + 1]) & 0x0ff) << 8)  
  34.         | ((int)(((char *)[text bytes])[index + 2]) & 0x0ff);  
  35.           
  36.         characters[charCount++] = encodingTable[(d >> 18) & 63];  
  37.         characters[charCount++] = encodingTable[(d >> 12) & 63];  
  38.         characters[charCount++] = encodingTable[(d >> 6) & 63];  
  39.         characters[charCount++] = encodingTable[d & 63];  
  40.           
  41.         index += 3;  
  42.           
  43.         if(n++ >= 14)  
  44.         {  
  45.             n = 0;  
  46.             characters[charCount++] = ' ';  
  47.         }  
  48.     }  
  49.       
  50.     if(index == text.length - 2)  
  51.     {  
  52.         int d = (((int)(((char *)[text bytes])[index]) & 0x0ff) << 16)  
  53.         | (((int)(((char *)[text bytes])[index + 1]) & 255) << 8);  
  54.         characters[charCount++] = encodingTable[(d >> 18) & 63];  
  55.         characters[charCount++] = encodingTable[(d >> 12) & 63];  
  56.         characters[charCount++] = encodingTable[(d >> 6) & 63];  
  57.         characters[charCount++] = '=';  
  58.     }  
  59.     else if(index == text.length - 1)  
  60.     {  
  61.         int d = ((int)(((char *)[text bytes])[index]) & 0x0ff) << 16;  
  62.         characters[charCount++] = encodingTable[(d >> 18) & 63];  
  63.         characters[charCount++] = encodingTable[(d >> 12) & 63];  
  64.         characters[charCount++] = '=';  
  65.         characters[charCount++] = '=';  
  66.     }  
  67.     NSString * rtnStr = [[NSString alloc] initWithBytesNoCopy:characters length:charCount encoding:NSUTF8StringEncoding freeWhenDone:YES];  
  68.     return rtnStr;  
  69. }  
  70.   
  71. /**  
  72.  字节转化为16进制数  
  73.  */  
  74. +(NSString *) parseByte2HexString:(Byte *) bytes  
  75. {  
  76.     NSMutableString *hexStr = [[NSMutableString alloc]init];  
  77.     int i = 0;  
  78.     if(bytes)  
  79.     {  
  80.         while (bytes[i] != '\0')  
  81.         {  
  82.             NSString *hexByte = [NSString stringWithFormat:@"%x",bytes[i] & 0xff];///16进制数  
  83.             if([hexByte length]==1)  
  84.                 [hexStr appendFormat:@"0%@", hexByte];  
  85.             else  
  86.                 [hexStr appendFormat:@"%@", hexByte];  
  87.               
  88.             i++;  
  89.         }  
  90.     }  
  91.     NSLog(@"bytes 的16进制数为:%@",hexStr);  
  92.     return hexStr;  
  93. }  
  94.   
  95.   
  96. /**  
  97.  字节数组转化16进制数  
  98.  */  
  99. +(NSString *) parseByteArray2HexString:(Byte[]) bytes  
  100. {  
  101.     NSMutableString *hexStr = [[NSMutableString alloc]init];  
  102.     int i = 0;  
  103.     if(bytes)  
  104.     {  
  105.         while (bytes[i] != '\0')  
  106.         {  
  107.             NSString *hexByte = [NSString stringWithFormat:@"%x",bytes[i] & 0xff];///16进制数  
  108.             if([hexByte length]==1)  
  109.                 [hexStr appendFormat:@"0%@", hexByte];  
  110.             else  
  111.                 [hexStr appendFormat:@"%@", hexByte];  
  112.               
  113.             i++;  
  114.         }  
  115.     }  
  116.     NSLog(@"bytes 的16进制数为:%@",hexStr);  
  117.     return [hexStr uppercaseString];  
  118. }  
  119.   
  120. /*  
  121.  将16进制数据转化成NSData 数组  
  122.  */  
  123. +(NSData*) parseHexToByteArray:(NSString*) hexString  
  124. {  
  125.     int j=0;  
  126.     Byte bytes[hexString.length];   
  127.     for(int i=0;i<[hexString length];i++)  
  128.     {  
  129.         int int_ch;  /// 两位16进制数转化后的10进制数  
  130.         unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)  
  131.         int int_ch1;  
  132.         if(hex_char1 >= '0' && hex_char1 <='9')  
  133.             int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48  
  134.         else if(hex_char1 >= 'A' && hex_char1 <='F')  
  135.             int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65  
  136.         else  
  137.             int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97  
  138.         i++;  
  139.         unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)  
  140.         int int_ch2;  
  141.         if(hex_char2 >= '0' && hex_char2 <='9')  
  142.             int_ch2 = (hex_char2-48); //// 0 的Ascll - 48  
  143.         else if(hex_char2 >= 'A' && hex_char1 <='F')  
  144.             int_ch2 = hex_char2-55; //// A 的Ascll - 65  
  145.         else  
  146.             int_ch2 = hex_char2-87; //// a 的Ascll - 97  
  147.           
  148.         int_ch = int_ch1+int_ch2;  
  149.         bytes[j] = int_ch;  ///将转化后的数放入Byte数组里  
  150.         j++;  
  151.     }  
  152.   
  153.     NSData *newData = [[NSData alloc] initWithBytes:bytes length:hexString.length/2];  
  154.     NSLog(@"newData=%@",newData);  
  155.     return newData;  
  156. }  
  157.   
  158. @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

0 0
原创粉丝点击