• EncryptTool


    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



  • 相关阅读:
    SQL舊面版與標准版不能互導數據
    今天總算可以繼續寫日記了
    MDAC2.7没有安装,控件不能运行
    希望5月份网络能开通
    SQLITE入门逐步讲解SQLITE命令行(一)
    C#使用SQLite数据库的代码示例
    红外遥控 Arduino 实例
    SQLite数据库是中小站点CMS的最佳选择
    防止 SQLite 数据库被下载的方法
    关于Linux下的Grep命令简介
  • 原文地址:https://www.cnblogs.com/java20130723/p/3212285.html
Copyright © 2020-2023  润新知