/**初始化向量*/
#define gIv@"0000000000000000"
这个要和后台或者硬件确定好否则会结果不一样
因为3DES的Key长度是24 被加密的的8的倍数 所以我在方法里面直接把key 补齐了
//加密方法
+ (NSData*)encrypt:(NSData*)plainText andkey:(NSData*)gkey {
NSMutableData*keydata = [[NSMutableDataalloc]init];
if(gkey.length<24)
{
NSMutableData*dddd = [[NSMutableDataalloc]initWithData:gkey];
[ddddappendData:gkey];
[ddddappendData:gkey];
keydata = [ddddsubdataWithRange:NSMakeRange(0,24)];
dddd =nil;
}
else
{
keydata = gkey;
}
size_tplainTextBufferSize = [plainTextlength];
constvoid*vplainText = (constvoid*)[plainTextbytes];
CCCryptorStatusccStatus;
uint8_t*bufferPtr =NULL;
size_tbufferPtrSize =0;
size_tmovedBytes =0;
bufferPtrSize = (plainTextBufferSize +kCCBlockSize3DES) & ~(kCCBlockSize3DES-1);
bufferPtr =malloc( bufferPtrSize *sizeof(uint8_t));
memset((void*)bufferPtr,0x0, bufferPtrSize);
// const void *vkey = (const void *) [gkey UTF8String];
//NSData * Keydata = [ccc dataWithHexString:gkey];
constvoid*keyvoid = (constvoid*)[keydatabytes];
constvoid*vinitVec = (constvoid*) [gIvUTF8String];
ccStatus =CCCrypt(kCCEncrypt,
kCCAlgorithm3DES,
kCCOptionPKCS7Padding|kCCOptionECBMode,
keyvoid,
kCCKeySize3DES,
vinitVec,
vplainText,
plainTextBufferSize,
(void*)bufferPtr,
bufferPtrSize,
&movedBytes);
NSMutableData*myData = [NSDatadataWithBytes:(constvoid*)bufferPtrlength:(NSUInteger)movedBytes];
NSMutableData*resultdata = [myDatasubdataWithRange:NSMakeRange(0, plainText.length)];
// [NSThread sleepForTimeInterval:0.01f];
returnresultdata;
}
//解密方法
+ (NSData*)decrypt:(NSData*)encryptText andkey:(NSData*)gkey
{
// [NSThread sleepForTimeInterval:0.02f];
NSMutableData*keydata = [[NSMutableDataalloc]init];
if(gkey.length<24)
{
NSMutableData*dddd = [[NSMutableDataalloc]initWithData:gkey];
[ddddappendData:gkey];
[ddddappendData:gkey];
keydata = [ddddsubdataWithRange:NSMakeRange(0,24)];
}
else
{
keydata = gkey;
}
//NSData *encryptData = [GTMBase64 decodeData:[encryptText dataUsingEncoding:NSUTF8StringEncoding]];
size_tplainTextBufferSize = [encryptTextlength];
constvoid*vplainText = [encryptTextbytes];
CCCryptorStatusccStatus;
uint8_t*bufferPtr =NULL;
size_tbufferPtrSize =0;
size_tmovedBytes =0;
bufferPtrSize = (plainTextBufferSize +kCCBlockSize3DES) & ~(kCCBlockSize3DES-1);
bufferPtr =malloc( bufferPtrSize *sizeof(uint8_t));
memset((void*)bufferPtr,0x0, bufferPtrSize);
constvoid*keyvoid = (constvoid*)[keydatabytes];
constvoid*vinitVec = (constvoid*) [gIvUTF8String];
ccStatus =CCCrypt(kCCDecrypt,
kCCAlgorithm3DES,
kCCOptionECBMode,
keyvoid,
kCCKeySize3DES,
vinitVec,
vplainText,
plainTextBufferSize,
(void*)bufferPtr,
bufferPtrSize,
&movedBytes);
NSData*myData = [NSDatadataWithBytes:(constvoid*)bufferPtrlength:(NSUInteger)movedBytes];
// [NSThread sleepForTimeInterval:0.01f];
returnmyData;
}