EncryptTool.h
// // Des.h // HungryBear // // Created by Bruce Yang on 12-5-26. // Copyright (c) 2012年 EricGameStudio. All rights reserved. // #import <Foundation/Foundation.h> #import <CommonCrypto/CommonCryptor.h> #import "GTMBase64.h" @interface EncryptTool : NSObject { } +(EncryptTool*) et; +(NSString*) parseByte2HexString:(Byte*)bytes; +(NSString*) parseByteArray2HexString:(Byte[])bytes; -(id) init; -(NSString*) encryptUseDES:(NSString*)plainText key:(NSString*)key; -(NSString*) decryptUseDES:(NSString*)cipherText key:(NSString*)key; @end
EncryptTool.mm
// // Des.mm // HungryBear // // Created by Bruce Yang on 12-5-26. // Copyright (c) 2012年 EricGameStudio. All rights reserved. // #import "EncryptTool.h" @implementation EncryptTool static Byte _iv_[] = {1, 2, 3, 4, 5, 6, 7, 8}; +(EncryptTool*) et { return [[[self alloc] init] autorelease]; } +(NSString*) parseByte2HexString:(Byte*) bytes { NSMutableString *hexStr = [[NSMutableString alloc]init]; int i = 0; if(bytes) { while (bytes[i] != '\0') { NSString *hexByte = [NSString stringWithFormat:@"%x",bytes[i] & 0xff];///16进制数 if([hexByte length]==1) { [hexStr appendFormat:@"0%@", hexByte]; } else { [hexStr appendFormat:@"%@", hexByte]; } i ++; } } NSLog(@"bytes 的16进制数为:%@",hexStr); return hexStr; } +(NSString*) parseByteArray2HexString:(Byte[]) bytes { NSMutableString *hexStr = [[NSMutableString alloc]init]; int i = 0; if(bytes) { while (bytes[i] != '\0') { NSString *hexByte = [NSString stringWithFormat:@"%x",bytes[i] & 0xff];///16进制数 if([hexByte length]==1) { [hexStr appendFormat:@"0%@", hexByte]; } else { [hexStr appendFormat:@"%@", hexByte]; } i ++; } } NSLog(@"bytes 的16进制数为:%@",hexStr); return hexStr; } -(id) init { if((self = [super init])) { } return self; } -(NSString*) encryptUseDES:(NSString*)plainText key:(NSString*)key { const char* textBytes = [plainText UTF8String]; NSUInteger dataLength = [plainText length]; unsigned char buffer[1024]; memset(buffer, 0, sizeof(char)); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionPKCS7Padding, [key UTF8String], kCCKeySizeDES, _iv_, textBytes, dataLength, buffer, 1024, &numBytesEncrypted); NSString* cipherText = nil; if (cryptStatus == kCCSuccess) { NSData* data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted]; cipherText = [GTMBase64 stringByEncodingData:data]; } return cipherText; } -(NSString*) decryptUseDES:(NSString*)cipherText key:(NSString*)key { NSData* cipherData = [GTMBase64 decodeString:cipherText]; // 8kB~ unsigned char buffer[8196]; memset(buffer, 0, sizeof(char)); size_t numBytesDecrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES, kCCOptionPKCS7Padding, [key UTF8String], kCCKeySizeDES, _iv_, [cipherData bytes], [cipherData length], buffer, 8196, &numBytesDecrypted); NSString* plainText = nil; /** * 2012.05.27.09.34,中间遇到一个问题, * 查看了一下返回的状态码,皆是因为 buffer 的尺寸过小所致 * 最开始 buffer 的长度是 1024~ */ // NSLog(@"%d", cryptStatus); if (cryptStatus == kCCSuccess) { NSData* data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted]; plainText = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]; } return plainText; } @end