Last active
August 29, 2015 14:06
-
-
Save qingjoin/9d5d3b7c4d71c1167ac3 to your computer and use it in GitHub Desktop.
iOS 3DES 加密 ECB模式
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Qy3DES.h | |
// GameLive | |
// | |
// Created by qingyun on 12/26/13. | |
// Copyright (c) 2013 qingyun. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import <CommonCrypto/CommonDigest.h> | |
#import <CommonCrypto/CommonCryptor.h> | |
@interface Qy3DES : NSObject | |
//3DES加密 | |
+ (NSString *) encrypt3DesStr:(NSString *) str withKey:(NSString *)key; | |
//3DES解密 | |
+ (NSString *) decrypt3DesStr:(NSString *) str withKey:(NSString *)key; | |
//3DES解密-------把服务器反回的json格式解成可读的格式 | |
+(NSDictionary*)decrypt3DesJson:(NSString*)str withKey:(NSString*)key; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Qy3DES.m | |
// GameLive | |
// | |
// Created by qingyun on 12/26/13. | |
// Copyright (c) 2013 qingyun. All rights reserved. | |
// | |
#import "Qy3DES.h" | |
#import "GLSupprot.h" | |
@implementation Qy3DES | |
static const char _glbase64EncodingTable[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
static const short _glbase64DecodingTable[256] = { | |
-2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -1, -1, -2, -2, | |
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | |
-1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63, | |
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2, | |
-2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, | |
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2, | |
-2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, | |
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2, | |
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | |
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | |
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | |
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | |
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | |
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | |
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | |
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2 | |
}; | |
+(NSDictionary*)decrypt3DesJson:(NSString*)str withKey:(NSString*)key | |
{ | |
if(!str) | |
{ | |
NSLog(@"decrypt3DEStr nil"); | |
return nil; | |
} | |
if(!key) | |
{ | |
NSLog(@"decrypt3DEKey nil"); | |
return nil; | |
} | |
NSString *token = [self decrypt3DesStr:str withKey:key]; | |
NSDictionary *jsonDic = nil; | |
NSError *error = nil; | |
if(token) | |
{ | |
jsonDic = [NSJSONSerialization JSONObjectWithData:[token dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:&error]; | |
} | |
return jsonDic; | |
} | |
+ (NSString *) encrypt3DesStr:(NSString *) str withKey:(NSString *)key | |
{ | |
if(!str) | |
{ | |
NSLog(@"encrypt3DEStr nil"); | |
return nil; | |
} | |
if(!key) | |
{ | |
NSLog(@"encrypt3DEKey nil"); | |
return nil; | |
} | |
return [self TripleDES:str encryptOrDecrypt:kCCEncrypt withKey:key]; | |
} | |
+ (NSString *) decrypt3DesStr:(NSString *) str withKey:(NSString *)key | |
{ | |
if(!str) | |
{ | |
NSLog(@"decrypt3DEStr nil"); | |
return nil; | |
} | |
if(!key) | |
{ | |
NSLog(@"decrypt3DEKey nil"); | |
return nil; | |
} | |
return [self TripleDES:str encryptOrDecrypt:kCCDecrypt withKey:key]; | |
} | |
+(NSString*)TripleDES:(NSString*)plainText encryptOrDecrypt:(CCOperation)encryptOrDecrypt withKey :(NSString*)_key | |
{ | |
const void *vplainText; | |
size_t plainTextBufferSize; | |
if (encryptOrDecrypt == kCCDecrypt)//解密 | |
{ | |
//NSData *EncryptData = [GTMBase64 decodeData:[plainText dataUsingEncoding:NSUTF8StringEncoding]]; | |
NSData *EncryptData = [[Qy3DES decodeBase64WithString:plainText]mutableCopy] ; | |
plainTextBufferSize = [EncryptData length]; | |
vplainText = [EncryptData bytes]; | |
} | |
else //加密 | |
{ | |
NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding]; | |
plainTextBufferSize = [data length]; | |
vplainText = (const void *)[data bytes]; | |
} | |
CCCryptorStatus ccStatus; | |
uint8_t *bufferPtr = NULL; | |
size_t bufferPtrSize = 0; | |
size_t movedBytes = 0; | |
bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1); | |
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t)); | |
memset((void *)bufferPtr, 0x0, bufferPtrSize); | |
// memset((void *) iv, 0x0, (size_t) sizeof(iv)); | |
const void *vkey = (const void *)[_key UTF8String]; | |
// NSString *initVec = @"init Vec"; | |
//const void *vinitVec = (const void *) [initVec UTF8String]; | |
// Byte iv[] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; | |
ccStatus = CCCrypt(encryptOrDecrypt, | |
kCCAlgorithm3DES, | |
kCCOptionPKCS7Padding | kCCOptionECBMode, | |
vkey, | |
kCCKeySize3DES, | |
nil, | |
vplainText, | |
plainTextBufferSize, | |
(void *)bufferPtr, | |
bufferPtrSize, | |
&movedBytes); | |
NSString *result; | |
if (encryptOrDecrypt == kCCDecrypt) | |
{ | |
result = [[NSString alloc] initWithData:[NSData dataWithBytes:(const void *)bufferPtr | |
length:(NSUInteger)movedBytes] | |
encoding:NSUTF8StringEncoding]; | |
} | |
else | |
{ | |
NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes]; | |
result = [Qy3DES encodeBase64WithData:myData]; | |
//result = [[NSString alloc]initWithData:myData encoding:NSUTF8StringEncoding]; | |
} | |
// alloc memory 20140227 内存释放 | |
free(bufferPtr); | |
// alloc memory 20140227 内存释放 | |
return result; | |
} | |
+ (NSString *)encodeBase64WithData:(NSData *)objData | |
{ | |
if ([objData length] == 0) | |
return @""; | |
char *characters = malloc((([objData length] + 2) / 3) * 4); | |
if (characters == NULL) | |
return nil; | |
NSUInteger length = 0; | |
NSUInteger i = 0; | |
while (i < [objData length]) | |
{ | |
char buffer[3] = {0,0,0}; | |
short bufferLength = 0; | |
while (bufferLength < 3 && i < [objData length]) | |
buffer[bufferLength++] = ((char *)[objData bytes])[i++]; | |
// Encode the bytes in the buffer to four characters, including padding "=" characters if necessary. | |
characters[length++] = _glbase64EncodingTable[(buffer[0] & 0xFC) >> 2]; | |
characters[length++] = _glbase64EncodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)]; | |
if (bufferLength > 1) | |
characters[length++] = _glbase64EncodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)]; | |
else characters[length++] = '='; | |
if (bufferLength > 2) | |
characters[length++] = _glbase64EncodingTable[buffer[2] & 0x3F]; | |
else characters[length++] = '='; | |
} | |
return [[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES]; | |
} | |
/* | |
+ (NSString *)encodeBase64WithData:(NSData *)objData { | |
const unsigned char * objRawData = [objData bytes]; | |
char * objPointer; | |
char * strResult; | |
// Get the Raw Data length and ensure we actually have data | |
int intLength = [objData length]; | |
if (intLength == 0) return nil; | |
// Setup the String-based Result placeholder and pointer within that placeholder | |
strResult = (char *)calloc(((intLength + 2) / 3) * 4, sizeof(char)); | |
objPointer = strResult; | |
// Iterate through everything | |
while (intLength > 2) { // keep going until we have less than 24 bits | |
*objPointer++ = _glbase64EncodingTable[objRawData[0] >> 2]; | |
*objPointer++ = _glbase64EncodingTable[((objRawData[0] & 0x03) << 4) + (objRawData[1] >> 4)]; | |
*objPointer++ = _glbase64EncodingTable[((objRawData[1] & 0x0f) << 2) + (objRawData[2] >> 6)]; | |
*objPointer++ = _glbase64EncodingTable[objRawData[2] & 0x3f]; | |
// we just handled 3 octets (24 bits) of data | |
objRawData += 3; | |
intLength -= 3; | |
} | |
// now deal with the tail end of things | |
if (intLength != 0) { | |
*objPointer++ = _glbase64EncodingTable[objRawData[0] >> 2]; | |
if (intLength > 1) { | |
*objPointer++ = _glbase64EncodingTable[((objRawData[0] & 0x03) << 4) + (objRawData[1] >> 4)]; | |
*objPointer++ = _glbase64EncodingTable[(objRawData[1] & 0x0f) << 2]; | |
*objPointer++ = '='; | |
} else { | |
*objPointer++ = _glbase64EncodingTable[(objRawData[0] & 0x03) << 4]; | |
*objPointer++ = '='; | |
*objPointer++ = '='; | |
} | |
} | |
// Terminate the string-based result | |
*objPointer = '\0'; | |
// Return the results as an NSString object | |
return [NSString stringWithCString:strResult encoding:NSASCIIStringEncoding]; | |
} | |
*/ | |
+ (NSData *)decodeBase64WithString:(NSString *)strBase64 { | |
const char * objPointer = [strBase64 cStringUsingEncoding:NSASCIIStringEncoding]; | |
int intLength = strlen(objPointer); | |
int intCurrent; | |
int i = 0, j = 0, k; | |
unsigned char * objResult; | |
objResult = calloc(intLength, sizeof(char)); | |
// Run through the whole string, converting as we go | |
while ( ((intCurrent = *objPointer++) != '\0') && (intLength-- > 0) ) { | |
if (intCurrent == '=') { | |
if (*objPointer != '=' && ((i % 4) == 1)) {// || (intLength > 0)) { | |
// the padding character is invalid at this point -- so this entire string is invalid | |
free(objResult); | |
return nil; | |
} | |
continue; | |
} | |
intCurrent = _glbase64DecodingTable[intCurrent]; | |
if (intCurrent == -1) { | |
// we're at a whitespace -- simply skip over | |
continue; | |
} else if (intCurrent == -2) { | |
// we're at an invalid character | |
free(objResult); | |
return nil; | |
} | |
switch (i % 4) { | |
case 0: | |
objResult[j] = intCurrent << 2; | |
break; | |
case 1: | |
objResult[j++] |= intCurrent >> 4; | |
objResult[j] = (intCurrent & 0x0f) << 4; | |
break; | |
case 2: | |
objResult[j++] |= intCurrent >>2; | |
objResult[j] = (intCurrent & 0x03) << 6; | |
break; | |
case 3: | |
objResult[j++] |= intCurrent; | |
break; | |
} | |
i++; | |
} | |
// mop things up if we ended on a boundary | |
k = j; | |
if (intCurrent == '=') { | |
switch (i % 4) { | |
case 1: | |
// Invalid state | |
free(objResult); | |
return nil; | |
case 2: | |
k++; | |
// flow through | |
case 3: | |
objResult[k] = 0; | |
} | |
} | |
// Cleanup and setup the return NSData | |
NSData * objData = [[NSData alloc] initWithBytes:objResult length:j]; | |
free(objResult); | |
return objData; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment