2

I am working on 3DES encryption and decryption. I have done encryption successfully. For decryption, I am using this code but not getting exact result. My encryption string is "CHc3TsfJgYs=" and key is "meristem". Please tell me what I am doing wrong?

 NSString *token = @"CHc3TsfJgYs=";
    NSString *key = @"meristem";

    const void *vplainText;
    size_t plainTextBufferSize;

    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);
    // memset((void *) iv, 0x0, (size_t) sizeof(iv));


    NSString *initVec = @"init Vec";
    const void *vkey = (const void *) [key UTF8String];
    const void *vinitVec = (const void *) [initVec UTF8String];

    ccStatus = CCCrypt(kCCDecrypt,
                       kCCAlgorithm3DES,
                       kCCOptionPKCS7Padding | kCCOptionECBMode,
                       vkey, //"123456789012345678901234", //key
                       kCCKeySizeDES,
                       NULL,// vinitVec, //"init Vec", //iv,
                       vplainText, //"Your Name", //plainText,
                       plainTextBufferSize,
                       (void *)bufferPtr,
                       bufferPtrSize,
                       &movedBytes);

    NSString *result;
    NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
    result = [myData base64Encoding];
    // result = [myDat];
    NSLog(@"dis is data %@",result);
Sudha Tiwari
  • 2,499
  • 26
  • 50
  • What are you getting as the output? Are there any warnings or errors? It would help if you showed your encryption code too. – occulus Jan 30 '14 at 14:54
  • Visit this link, You might be get Proper solution [Encryption and Decryption][1] [1]: http://stackoverflow.com/questions/1400246/aes-encryption-for-an-nsstring-on-the-iphone – Dipak Narigara Mar 31 '14 at 06:30
  • In which form are you getting the data from API. I am able to encrypt successfully but while decrypting the data from server I am getting . I can see data is there but unable to decrypt in iOS. – Imran Jul 01 '19 at 13:17

1 Answers1

1
ccStatus = CCCrypt(kCCDecrypt,
                   kCCAlgorithm3DES,
                   kCCOptionPKCS7Padding | kCCOptionECBMode,
                   vkey, //"123456789012345678901234", //key
                   kCCKeySize3DES,
                   NULL,// vinitVec, //"init Vec", //iv,
                   vplainText, //"Your Name", //plainText,
                   plainTextBufferSize,
                   (void *)bufferPtr,
                   bufferPtrSize,
                   &movedBytes);

you need change kCCKeySizeDES -> kCCKeySize3DES

benka
  • 4,732
  • 35
  • 47
  • 58
Joshua
  • 11
  • 1