4

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.

insys
  • 1,288
  • 13
  • 26
Sudha Tiwari
  • 2,499
  • 26
  • 50

2 Answers2

2

I have solved my issue by replacing this line

plainTextBufferSize = [EncryptData length];  

into

plainTextBufferSize = [EncryptData length]+1;
Sudha Tiwari
  • 2,499
  • 26
  • 50
1

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.

0xKayvan
  • 368
  • 1
  • 4
  • 18
  • 1
    no luck! :( but I soved out the problem by changing this line plainTextBufferSize = [EncryptData length]; into plainTextBufferSize = [EncryptData length]+1; And thanks a lot!! – Sudha Tiwari Feb 04 '14 at 12:51
  • You're welcome. (I didn't see that line of the code! `plainTextBufferSize = [EncryptData length];` :D) I knew the problem was with `plainTextBufferSize`, but as I didn't notice that line, my first answer was totally wrong, as any value you assign to `plainTextBufferSize` would be replaced with `[EncryptData length]`. I edited my answer to be no longer misleading. – 0xKayvan Feb 04 '14 at 13:10
  • and as a suggestion: answer your own question and accept the answer for others who may have this problem. ;) – 0xKayvan Feb 04 '14 at 13:13