iOS开发:用DES对字符串加解密

时间:2020-12-07 09:14:52

参考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. @interface Base64 : NSObject
  3. +(int)char2Int:(char)c;
  4. +(NSData *)decode:(NSString *)data;
  5. +(NSString *)encode:(NSData *)data;
  6. @end

Base64.m

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

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

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