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:

  1. #import <Foundation/Foundation.h>
  2.  
  3. @interface Base64 : NSObject
  4. +(int)char2Int:(char)c;
  5. +(NSData *)decode:(NSString *)data;
  6. +(NSString *)encode:(NSData *)data;
  7. @end
  8.  

Base64.m

  1. #import "Base64.h"
  2.  
  3. static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  4.  
  5. @interface Base64()
  6. +(int)char2Int:(char)c;
  7. @end
  8.  
  9. @implementation Base64
  10.  
  11. +(NSString *)encode:(NSData *)data
  12. {
  13. if (data.length == 0)
  14. return nil;
  15. char *characters = malloc(data.length * 3 / 2);
  16. if (characters == NULL)
  17. return nil;
  18. int end = data.length - 3;
  19. int index = 0;
  20. int charCount = 0;
  21. int n = 0;
  22. while (index <= end) {
  23. int d = (((int)(((char *)[data bytes])[index]) & 0x0ff) << 16)
  24. | (((int)(((char *)[data bytes])[index + 1]) & 0x0ff) << 8)
  25. | ((int)(((char *)[data bytes])[index + 2]) & 0x0ff);
  26. characters[charCount++] = encodingTable[(d >> 18) & 63];
  27. characters[charCount++] = encodingTable[(d >> 12) & 63];
  28. characters[charCount++] = encodingTable[(d >> 6) & 63];
  29. characters[charCount++] = encodingTable[d & 63];
  30. index += 3;
  31. if(n++ >= 14)
  32. {
  33. n = 0;
  34. characters[charCount++] = ' ';
  35. }
  36. }
  37. if(index == data.length - 2)
  38. {
  39. int d = (((int)(((char *)[data bytes])[index]) & 0x0ff) << 16)
  40. | (((int)(((char *)[data bytes])[index + 1]) & 255) << 8);
  41. characters[charCount++] = encodingTable[(d >> 18) & 63];
  42. characters[charCount++] = encodingTable[(d >> 12) & 63];
  43. characters[charCount++] = encodingTable[(d >> 6) & 63];
  44. characters[charCount++] = '=';
  45. }
  46. else if(index == data.length - 1)
  47. {
  48. int d = ((int)(((char *)[data bytes])[index]) & 0x0ff) << 16;
  49. characters[charCount++] = encodingTable[(d >> 18) & 63];
  50. characters[charCount++] = encodingTable[(d >> 12) & 63];
  51. characters[charCount++] = '=';
  52. characters[charCount++] = '=';
  53. }
  54. NSString * rtnStr = [[NSString alloc] initWithBytesNoCopy:characters length:charCount encoding:NSUTF8StringEncoding freeWhenDone:YES];
  55. return rtnStr;
  56. }
  57.  
  58. +(NSData *)decode:(NSString *)data
  59. {
  60. if(data == nil || data.length <= 0) {
  61. return nil;
  62. }
  63. NSMutableData *rtnData = [[NSMutableData alloc]init];
  64. int slen = data.length;
  65. int index = 0;
  66. while (true) {
  67. while (index < slen && [data characterAtIndex:index] <= ' ') {
  68. index++;
  69. }
  70. if (index >= slen || index + 3 >= slen) {
  71. break;
  72. }
  73. 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]];
  74. Byte temp1 = (byte >> 16) & 255;
  75. [rtnData appendBytes:&temp1 length:1];
  76. if([data characterAtIndex:index + 2] == '=') {
  77. break;
  78. }
  79. Byte temp2 = (byte >> 8) & 255;
  80. [rtnData appendBytes:&temp2 length:1];
  81. if([data characterAtIndex:index + 3] == '=') {
  82. break;
  83. }
  84. Byte temp3 = byte & 255;
  85. [rtnData appendBytes:&temp3 length:1];
  86. index += 4;
  87. }
  88. return rtnData;
  89. }
  90.  
  91. +(int)char2Int:(char)c
  92. {
  93. if (c >= 'A' && c <= 'Z') {
  94. return c - 65;
  95. } else if (c >= 'a' && c <= 'z') {
  96. return c - 97 + 26;
  97. } else if (c >= '0' && c <= '9') {
  98. return c - 48 + 26 + 26;
  99. } else {
  100. switch(c) {
  101. case '+':
  102. return 62;
  103. case '/':
  104. return 63;
  105. case '=':
  106. return 0;
  107. default:
  108. return -1;
  109. }
  110. }
  111. }
  112.  
  113. @end
  114.  

DESUtils.h:

  1. #import <Foundation/Foundation.h>
  2. #import <CommonCrypto/CommonCrypto.h>
  3. #import "Base64.h"
  4. @interface DESUtils : NSObject
  5. +(NSString *)decryptUseDES:(NSString *)cipherText key:(NSString *)key;
  6. +(NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key;
  7. @end

DESUtils.m:

  1.  
  2. #import "DESUtils.h"
  3.  
  4. @implementation DESUtils
  5. +(NSString *)decryptUseDES:(NSString *)cipherText key:(NSString *)key
  6. {
  7. NSString *plaintext = nil;
  8. NSData *cipherdata = [Base64 decode:cipherText];
  9.  
  10. unsigned char buffer[1024];
  11. memset(buffer, 0, sizeof(char));
  12. size_t numBytesDecrypted = 0;
  13. CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES,
  14. kCCOptionPKCS7Padding,
  15. [key UTF8String], kCCKeySizeDES,
  16. (Byte *)[[key dataUsingEncoding:NSUTF8StringEncoding] bytes],
  17. [cipherdata bytes], [cipherdata length],
  18. buffer, 1024,
  19. &numBytesDecrypted);
  20. if(cryptStatus == kCCSuccess) {
  21. NSData *plaindata = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
  22. plaintext = [[NSString alloc]initWithData:plaindata encoding:NSUTF8StringEncoding];
  23. }
  24. return plaintext;
  25. }
  26. +(NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key
  27. {
  28.  
  29. NSString *ciphertext = nil;
  30. NSData *textData = [plainText dataUsingEncoding:NSUTF8StringEncoding];
  31. NSUInteger dataLength = [textData length];
  32. unsigned char buffer[1024];
  33. memset(buffer, 0, sizeof(char));
  34. size_t numBytesEncrypted = 0;
  35. CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
  36. kCCOptionPKCS7Padding,
  37. [key UTF8String], kCCKeySizeDES,
  38. (Byte *)[[key dataUsingEncoding:NSUTF8StringEncoding] bytes],
  39. [textData bytes], dataLength,
  40. buffer, 1024,
  41. &numBytesEncrypted);
  42. if (cryptStatus == kCCSuccess) {
  43. NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
  44. ciphertext = [Base64 encode:data];
  45. }
  46. return ciphertext;
  47. }
  48. @end
  49.  

© 2014, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记

你可能感兴趣的:(ios开发)