objective-c实现authCode 解决php与ios通信加密的问题

来源:互联网 发布:暗黑破坏神2 mac 1.13 编辑:程序博客网 时间:2024/05/24 06:49

原文地址:http://blog.csdn.net/jxncwzb/article/details/9254759


PHP 代码如下:

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. /** 
  3.  * 加密解密 字符串 
  4.  * @param string $string 原字符串 
  5.  * @param string  $auth_key 加密的KEY 
  6.  * @param string $operation  加密解密的开关   ENCODE:加密, DECODE:解密 
  7.  * @return string|mixed 
  8.  */  
  9. function authCode($string$auth_key$operation='ENCODE') {  
  10.     $key = md5($auth_key);  
  11.     $key_length = strlen($key);  
  12.   
  13.     //base64_decode 默认是 iso-8859-1 转 UTF-8 用 utf8_decode(base64_decode($string))  
  14.     $string = $operation == 'DECODE' ? utf8_decode(base64_decode($string)) : substr(md5($string.$key), 0, 8).$string;  
  15.     $string_length = strlen($string);  
  16.   
  17.     $rndkey = $box = array();  
  18.     $result = '';  
  19.   
  20.     for($i = 0; $i <= 255; $i++) {  
  21.         $rndkey[$i] = ord($key[$i % $key_length]);  
  22.         $box[$i] = $i;  
  23.     }   
  24.    
  25.     for($j = $i = 0; $i < 256; $i++) {  
  26.         $j = ($j + $box[$i] + $rndkey[$i]) % 256;  
  27.         $tmp = $box[$i];  
  28.         $box[$i] = $box[$j];  
  29.         $box[$j] = $tmp;  
  30.     }  
  31.   
  32.     for($a = $j = $i = 0; $i < $string_length$i++) {  
  33.         $a = ($a + 1) % 256;  
  34.         $j = ($j + $box[$a]) % 256;  
  35.         $tmp = $box[$a];  
  36.         $box[$a] = $box[$j];  
  37.         $box[$j] = $tmp;  
  38.         $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));  
  39.     }  
  40.   
  41.     if($operation == 'DECODE') {  
  42.         if(substr($result, 0, 8) == substr(md5(substr($result, 8).$key), 0, 8)) {  
  43.             return substr($result, 8);  
  44.         } else {  
  45.             return '';  
  46.         }  
  47.     } else {  
  48.         //base64_encode 默认是 iso-8859-1 转 UTF-8 用 base64_encode(utf8_encode($result))  
  49.         return str_replace('=''',base64_encode(utf8_encode($result)));  
  50.     }  
  51. }  
  52.   
  53. $key = "123";  
  54. $string = "test";  
  55. $a = authCode($string$key);  
  56. var_dump($a);  
  57. var_dump(authCode($a$key,'DECODE'));  
  58. ?>  



objective-c 代码如下:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  Base64.h  
  3. //  Base64  
  4. //  
  5. //  Created by wangzhongbin on 13-7-5.  
  6. //  Copyright (c) 2013年 iOS Group. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @interface NSData (Base64)  
  12.   
  13. + (NSData *)dataWithBase64EncodedString:(NSString *)string;  
  14. - (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth;  
  15. - (NSString *)base64EncodedString;  
  16.   
  17. @end  
  18.   
  19.   
  20. @interface NSString (Base64)  
  21.   
  22. + (NSString *)stringWithBase64EncodedString:(NSString *)string encoding:(NSStringEncoding)encoding;  
  23. + (NSString *)stringWithBase64EncodedString:(NSString *)string;  
  24. - (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth;  
  25. - (NSString *)base64EncodedString:(NSStringEncoding)encoding;  
  26. - (NSString *)base64EncodedString;  
  27. - (NSString *)base64DecodedString:(NSStringEncoding)encoding;  
  28. - (NSString *)base64DecodedString;  
  29. - (NSData *)base64DecodedData;  
  30.   
  31. - (NSString *)authCodeEncoded:(NSString *)key encoding:(NSStringEncoding)encoding;  
  32. - (NSString *)authCodeEncoded:(NSString *)key;  
  33. - (NSString *)authCodeDecoded:(NSString *)key encoding:(NSStringEncoding)encoding;  
  34. - (NSString *)authCodeDecoded:(NSString *)key;  
  35. @end  


[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  Base64.m  
  3. //  Base64  
  4. //  
  5. //  Created by wangzhongbin on 13-7-5.  
  6. //  Copyright (c) 2013年 iOS Group. All rights reserved.  
  7. //  
  8.   
  9. #import "Base64.h"  
  10. #import <CommonCrypto/CommonDigest.h>  
  11.   
  12. #import <Availability.h>  
  13. #if !__has_feature(objc_arc)  
  14. #error This library requires automatic reference counting  
  15. #endif  
  16.   
  17. typedef enum {  
  18.     NSStringAuthCodeEncoded,  
  19.     NSStringAuthCodeDecoded  
  20. } NSStringAuthCode;  
  21.   
  22.   
  23. @implementation NSData (Base64)  
  24.   
  25. + (NSData *)dataWithBase64EncodedString:(NSString *)string  
  26. {  
  27.     const char lookup[] =  
  28.     {  
  29.         99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,   
  30.         99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,   
  31.         99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 62, 99, 99, 99, 63,   
  32.         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 99, 99, 99, 99, 99, 99,   
  33.         99,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,   
  34.         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 99, 99, 99, 99, 99,   
  35.         99, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,   
  36.         41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 99, 99, 99, 99, 99  
  37.     };  
  38.       
  39.     NSData *inputData = [string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
  40.     long long inputLength = [inputData length];  
  41.     const unsigned char *inputBytes = [inputData bytes];  
  42.       
  43.     long long maxOutputLength = (inputLength / 4 + 1) * 3;  
  44.     NSMutableData *outputData = [NSMutableData dataWithLength:maxOutputLength];  
  45.     unsigned char *outputBytes = (unsigned char *)[outputData mutableBytes];  
  46.   
  47.     int accumulator = 0;  
  48.     long long outputLength = 0;  
  49.     unsigned char accumulated[] = {0, 0, 0, 0};  
  50.     for (long long i = 0; i < inputLength; i++)  
  51.     {  
  52.         unsigned char decoded = lookup[inputBytes[i] & 0x7F];  
  53.         if (decoded != 99)  
  54.         {  
  55.             accumulated[accumulator] = decoded;  
  56.             if (accumulator == 3)  
  57.             {  
  58.                 outputBytes[outputLength++] = (accumulated[0] << 2) | (accumulated[1] >> 4);    
  59.                 outputBytes[outputLength++] = (accumulated[1] << 4) | (accumulated[2] >> 2);    
  60.                 outputBytes[outputLength++] = (accumulated[2] << 6) | accumulated[3];  
  61.             }  
  62.             accumulator = (accumulator + 1) % 4;  
  63.         }  
  64.     }  
  65.       
  66.     //handle left-over data  
  67.     if (accumulator > 0) outputBytes[outputLength] = (accumulated[0] << 2) | (accumulated[1] >> 4);  
  68.     if (accumulator > 1) outputBytes[++outputLength] = (accumulated[1] << 4) | (accumulated[2] >> 2);  
  69.     if (accumulator > 2) outputLength++;  
  70.       
  71.     //truncate data to match actual output length  
  72.     outputData.length = outputLength;  
  73.     return outputLength? outputData: nil;  
  74. }  
  75.   
  76. - (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth  
  77. {  
  78.     //ensure wrapWidth is a multiple of 4  
  79.     wrapWidth = (wrapWidth / 4) * 4;  
  80.       
  81.     const char lookup[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";  
  82.       
  83.     long long inputLength = [self length];  
  84.     const unsigned char *inputBytes = [self bytes];  
  85.       
  86.     long long maxOutputLength = (inputLength / 3 + 1) * 4;  
  87.     maxOutputLength += wrapWidth? (maxOutputLength / wrapWidth) * 2: 0;  
  88.     unsigned char *outputBytes = (unsigned char *)malloc(maxOutputLength);  
  89.       
  90.     long long i;  
  91.     long long outputLength = 0;  
  92.     for (i = 0; i < inputLength - 2; i += 3)  
  93.     {  
  94.         outputBytes[outputLength++] = lookup[(inputBytes[i] & 0xFC) >> 2];  
  95.         outputBytes[outputLength++] = lookup[((inputBytes[i] & 0x03) << 4) | ((inputBytes[i + 1] & 0xF0) >> 4)];  
  96.         outputBytes[outputLength++] = lookup[((inputBytes[i + 1] & 0x0F) << 2) | ((inputBytes[i + 2] & 0xC0) >> 6)];  
  97.         outputBytes[outputLength++] = lookup[inputBytes[i + 2] & 0x3F];  
  98.           
  99.         //add line break  
  100.         if (wrapWidth && (outputLength + 2) % (wrapWidth + 2) == 0)  
  101.         {  
  102.             outputBytes[outputLength++] = '\r';  
  103.             outputBytes[outputLength++] = '\n';  
  104.         }  
  105.     }  
  106.       
  107.     //handle left-over data  
  108.     if (i == inputLength - 2)  
  109.     {  
  110.         // = terminator  
  111.         outputBytes[outputLength++] = lookup[(inputBytes[i] & 0xFC) >> 2];  
  112.         outputBytes[outputLength++] = lookup[((inputBytes[i] & 0x03) << 4) | ((inputBytes[i + 1] & 0xF0) >> 4)];  
  113.         outputBytes[outputLength++] = lookup[(inputBytes[i + 1] & 0x0F) << 2];  
  114.         outputBytes[outputLength++] =   '=';  
  115.     }  
  116.     else if (i == inputLength - 1)  
  117.     {  
  118.         // == terminator  
  119.         outputBytes[outputLength++] = lookup[(inputBytes[i] & 0xFC) >> 2];  
  120.         outputBytes[outputLength++] = lookup[(inputBytes[i] & 0x03) << 4];  
  121.         outputBytes[outputLength++] = '=';  
  122.         outputBytes[outputLength++] = '=';  
  123.     }  
  124.       
  125.     if (outputLength >= 4)  
  126.     {  
  127.         //truncate data to match actual output length  
  128.         outputBytes = realloc(outputBytes, outputLength);  
  129.         return [[NSString alloc] initWithBytesNoCopy:outputBytes  
  130.                                               length:outputLength  
  131.                                             encoding:NSASCIIStringEncoding  
  132.                                         freeWhenDone:YES];  
  133.     }  
  134.     else if (outputBytes)  
  135.     {  
  136.         free(outputBytes);  
  137.     }  
  138.     return nil;  
  139. }  
  140.   
  141. - (NSString *)base64EncodedString  
  142. {  
  143.     return [self base64EncodedStringWithWrapWidth:0];  
  144. }  
  145.   
  146. @end  
  147.   
  148.   
  149. @implementation NSString (Base64)  
  150.   
  151. + (NSString *)stringWithBase64EncodedString:(NSString *)string encoding:(NSStringEncoding)encoding  
  152. {  
  153.     NSData *data = [NSData dataWithBase64EncodedString:string];  
  154.     if (data)  
  155.     {  
  156.         return [[self alloc] initWithData:data encoding:encoding];  
  157.     }  
  158.     return nil;  
  159. }  
  160.   
  161. + (NSString *)stringWithBase64EncodedString:(NSString *)string  
  162. {  
  163.     return [self stringWithBase64EncodedString:string encoding:NSUTF8StringEncoding];  
  164. }  
  165.   
  166. - (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth  
  167. {  
  168.     NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];  
  169.     return [data base64EncodedStringWithWrapWidth:wrapWidth];  
  170. }  
  171.   
  172. - (NSString *)base64EncodedString:(NSStringEncoding)encoding  
  173. {  
  174.     NSData *data = [self dataUsingEncoding:encoding allowLossyConversion:YES];  
  175.     return [data base64EncodedString];  
  176. }  
  177.   
  178. - (NSString *)base64EncodedString  
  179. {  
  180.     return [self base64EncodedString:NSUTF8StringEncoding];  
  181. }  
  182.   
  183. - (NSString *)base64DecodedString:(NSStringEncoding)encoding  
  184. {  
  185.     return [NSString stringWithBase64EncodedString:self encoding:encoding];  
  186. }  
  187.   
  188. - (NSString *)base64DecodedString  
  189. {  
  190.     return [NSString stringWithBase64EncodedString:self];  
  191. }  
  192.   
  193. - (NSData *)base64DecodedData  
  194. {  
  195.     return [NSData dataWithBase64EncodedString:self];  
  196. }  
  197.   
  198. - (NSString *)md5 {  
  199.     const char *concat_str = [self UTF8String];  
  200.     unsigned char result[CC_MD5_DIGEST_LENGTH];  
  201.     CC_MD5(concat_str, strlen(concat_str), result);  
  202.     NSMutableString *hash = [NSMutableString string];  
  203.     for (int i = 0; i < 16; i++)  
  204.         [hash appendFormat:@"%02x", result[i]];  
  205.     return [hash lowercaseString];  
  206. }  
  207.   
  208. - (NSString *)authCode:(NSString *)auth_key operation:(NSStringAuthCode)operation encoding:(NSStringEncoding)encoding;  
  209. {  
  210.     NSMutableArray *rndkey = [NSMutableArray array];  
  211.     NSMutableArray *box = [NSMutableArray array];  
  212.     NSMutableString *result = [NSMutableString string];  
  213.       
  214.     NSString *key = [auth_key md5];  
  215.     NSUInteger key_length = key.length;  
  216.     NSString *string = (operation == NSStringAuthCodeDecoded) ? [self base64DecodedString:encoding] : [NSString stringWithFormat:@"%@%@",[[[NSString stringWithFormat:@"%@%@",self,key] md5] substringToIndex:8],self];  
  217.     NSUInteger string_length = string.length;  
  218.       
  219.     for(int i = 0; i <= 255; i++) {  
  220.         [rndkey addObject:[NSNumber numberWithUnsignedShort:[key characterAtIndex:i%key_length]]];  
  221.         [box addObject:[NSNumber numberWithUnsignedShort:i]];  
  222.     }  
  223.       
  224.     int j = 0;  
  225.     for(int i = 0; i < 256; i++) {  
  226.         unsigned short b = [[box objectAtIndex:i] unsignedShortValue];  
  227.         unsigned short r = [[rndkey objectAtIndex:i] unsignedShortValue];  
  228.         j = (j + b + r) % 256;  
  229.         [box replaceObjectAtIndex:i withObject:[box objectAtIndex:j]];  
  230.         [box replaceObjectAtIndex:j withObject:[NSNumber numberWithUnsignedShort:b]];  
  231.     }  
  232.       
  233.     int a = 0;  
  234.     j = 0;  
  235.     for(int i = 0; i < string_length; i++) {  
  236.         a = (a + 1) % 256;  
  237.         unsigned short b = [[box objectAtIndex:a] unsignedShortValue];  
  238.         j = (j + b) % 256;  
  239.         [box replaceObjectAtIndex:a withObject:[box objectAtIndex:j]];  
  240.         [box replaceObjectAtIndex:j withObject:[NSNumber numberWithUnsignedShort:b]];  
  241.           
  242.         unsigned short sc = [string characterAtIndex:i];  
  243.         unsigned short bi = [[box objectAtIndex:(([[box objectAtIndex:a] unsignedShortValue] + [[box objectAtIndex:j] unsignedShortValue]) % 256)] unsignedShortValue];  
  244.         unsigned short k = sc ^ bi;  
  245.         [result appendFormat:@"%C",k];  
  246.     }  
  247.       
  248.     if(operation == NSStringAuthCodeDecoded) {  
  249.         NSString *start = [result substringToIndex:8];  
  250.         NSString *end = [[[NSString stringWithFormat:@"%@%@",[result substringFromIndex:8],key] md5]  substringToIndex:8];  
  251.         if([start isEqualToString:end]){  
  252.             return [result substringFromIndex:8];  
  253.         }  
  254.         else{  
  255.             return nil;  
  256.         }  
  257.     } else {  
  258.         return [[result base64EncodedString:encoding] stringByReplacingOccurrencesOfString:@"=" withString:@""];  
  259.     }  
  260. }  
  261.   
  262. - (NSString *)authCodeEncoded:(NSString *)key encoding:(NSStringEncoding)encoding  
  263. {  
  264.     return [self authCode:key operation:NSStringAuthCodeEncoded encoding:encoding];  
  265. }  
  266.   
  267. - (NSString *)authCodeEncoded:(NSString *)key  
  268. {  
  269.     return [self authCodeEncoded:key encoding:NSUTF8StringEncoding];  
  270. }  
  271.   
  272. - (NSString *)authCodeDecoded:(NSString *)key encoding:(NSStringEncoding)encoding  
  273. {  
  274.     return [self authCode:key operation:NSStringAuthCodeDecoded encoding:encoding];  
  275. }  
  276.   
  277. - (NSString *)authCodeDecoded:(NSString *)key  
  278. {  
  279.     return [self authCodeDecoded:key encoding:NSUTF8StringEncoding];  
  280. }  
  281.   
  282. @end  


[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. NSString *key = @"123";  
  2. NSString *string = @"test";  
  3. NSString *a = [string authCodeEncoded:key];  
  4. NSLog(@"%@",a);  
  5. NSLog(@"%@",[a authCodeDecoded:key]);  


完整代码下载地址:http://download.csdn.net/detail/jxncwzb/8474547

0 0
原创粉丝点击