Reputation: 2461
I am using DES decryption with ECB mode. I am using the following code for decryption :
NSString *token = @"kRAz86UoZd5tFKf0xv8TKg==";
NSString *key = @"meristem";
const void *vplainText;
size_t plainTextBufferSize;
NSData *EncryptData = [[NSData alloc] initWithBase64EncodedString:token options:0];
plainTextBufferSize = [EncryptData length];
vplainText = [EncryptData bytes];
//plainTextBufferSize = [token length];
//vplainText = (const void *) [token UTF8String];
CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes;
bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x0, bufferPtrSize);
NSString *initVec = @"init Vec";
const void *vkey = (const void *) [key UTF8String];
const void *vinitVec;
vinitVec = (const void *) [initVec UTF8String];
ccStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmDES,
kCCOptionPKCS7Padding | kCCOptionECBMode,
vkey, //"123456789012345678901234", //key
kCCKeySizeDES,
NULL,// vinitVec, //"init Vec", //iv,
vplainText, //"Your Name", //plainText,
plainTextBufferSize,
(void *)bufferPtr,
bufferPtrSize,
&movedBytes);
NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
NSString *decodedString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];
NSLog(@"dis is data %@",decodedString);
Here, you can see that my encrypted string is kRAz86UoZd5tFKf0xv8TKg== and its result is vishal thakur. But by using the above code for decryption, I am getting only vishal t. I cant understand why am not getting the full string. Please can anyone tell me what I am doing wrong.
Upvotes: 4
Views: 681
Reputation: 376
Edited:
I think it has something to do with plainTextBufferSize
, and this cause the problem as you call it in your CCCrypt
function.
Try changing its value before CCCrypt
function.
Upvotes: 1
Reputation: 2461
I have solved my issue by replacing this line
plainTextBufferSize = [EncryptData length];
into
plainTextBufferSize = [EncryptData length]+1;
Upvotes: 2