标签:cccrypt length copy using set nsdata color dex 解密
#import <Foundation/Foundation.h>
//3DES加密
@interface JKEncrypt : NSObject
-(NSString *)doEncrypt:(NSString *)plainText;
-(NSString*)doDecEncrypt:(NSString *)encryptText;
@end
//
// main.m
// 3DES研究
//
// Created by apple on 15/10/22.
// Copyright ? 2015年 apple. All rights reserved.
//
#import "JKEncrypt.h"
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonCryptor.h>
#import <Security/Security.h>
//密匙 key
#define gkey @"123456788765432112345678" //24位的密钥
//偏移量
#define gIv @"jukai"
@implementation JKEncrypt
-(NSString *)doEncrypt:plainText{
//把string 转NSData
NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding];
//length
size_t plainTextBufferSize = [data length];
const void *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);
const void *vkey = (const void *) [gkey UTF8String];
//偏移量
const void *vinitVec = (const void *) [gIv UTF8String];
//配置CCCrypt
ccStatus = CCCrypt(kCCEncrypt,
kCCAlgorithm3DES, //3DES
kCCOptionECBMode|kCCOptionPKCS7Padding, //设置模式
vkey, //key
kCCKeySize3DES,
// vinitVec, //偏移量,这里不用,设置为nil;不用的话,必须为nil,不可以为@“” 解密算法里也应该为nil,也不可以为@“”
nil, //偏移量,这里不用,设置为nil;不用的话,必须为nil,不可以为@“”
vplainText,
plainTextBufferSize,
(void *)bufferPtr,
bufferPtrSize,
&movedBytes);
NSData *myData = [NSData dataWithBytes:(const char *)bufferPtr length:(NSUInteger)movedBytes];
NSUInteger len = [myData length];
char * chars = (char *)[myData bytes];
NSMutableString * hexString = [[NSMutableString alloc] init];
for(NSUInteger i = 0; i < len; i++ )
[hexString appendString:[NSString stringWithFormat:@"%0.2hhx", chars[i]]];
return hexString;
}
-(NSString*)doDecEncrypt:(NSString *)hexString{
//十六进制转NSData
long len = [hexString length] / 2;
unsigned char *buf = malloc(len);
unsigned char *whole_byte = buf;
char byte_chars[3] = {‘\0‘,‘\0‘,‘\0‘};
int i;
for (i=0; i < [hexString length] / 2; i++) {
byte_chars[0] = [hexString characterAtIndex:i*2];
byte_chars[1] = [hexString characterAtIndex:i*2+1];
*whole_byte = strtol(byte_chars, NULL, 16);
whole_byte++;
}
NSData *encryptData = [NSData dataWithBytes:buf length:len];
size_t plainTextBufferSize = [encryptData length];
const void *vplainText = [encryptData 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);
const void *vkey = (const void *) [gkey UTF8String];
const void *vinitVec = (const void *) [gIv UTF8String];
ccStatus = CCCrypt(kCCDecrypt,
kCCAlgorithm3DES,
kCCOptionPKCS7Padding|kCCOptionECBMode,
vkey,
kCCKeySize3DES,
nil,
vplainText,
plainTextBufferSize,
(void *)bufferPtr,
bufferPtrSize,
&movedBytes);
NSString *result = [[NSString alloc] initWithData:[NSData dataWithBytes:(const void *)bufferPtr
length:(NSUInteger)movedBytes] encoding:NSUTF8StringEncoding];
return result;
}
@end
标签:cccrypt length copy using set nsdata color dex 解密
原文地址:http://www.cnblogs.com/daxueshan/p/6296818.html