0

I have to decrypt a string in Objective C. The encryption scheme is DES/ECB/NoPadding and I am providing this Java code with correct output with following input data = 741DCDDF1C216EEF and key = D9C44F6D2589255E and output should be 34160D6EADAD6D86

public static String decrypt3DES(String Key, String data) throws Exception
{
    Cipher cipher = null;
    byte[] text = null;
    byte[] desKey = null;
    Key keySpec = null;
    try {

        if (Key.length() <= 16) {
            cipher = Cipher.getInstance("DES/ECB/NoPadding");
            desKey = byteConvertor(Key);
            keySpec = new SecretKeySpec(desKey, "DES");
        } else if (Key.length() >= 32) {
            cipher = Cipher.getInstance("DESede/ECB/NoPadding");
            desKey = byteConvertor(Key);
            keySpec = new SecretKeySpec(desKey, "DESede");
        }
        cipher.init(Cipher.DECRYPT_MODE, keySpec);

        text = cipher.doFinal(byteConvertor(data));

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }

    return alpha2Hex(byteArr2String(text));
}

Objective C code :

NSString *token = @"741DCDDF1C216EEF";

NSString *key = @"D9C44F6D2589255E";

const void *vplainText;

size_t plainTextBufferSize;

NSData *EncryptData = [[NSData alloc] initWithBase64EncodedString:token options:0];
plainTextBufferSize = [EncryptData length]+1;
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];

//uint8_t initVect[8];
//bzero(initVect, 8);

ccStatus = CCCrypt(kCCDecrypt,
                   kCCAlgorithmDES,
                   kCCOptionECBMode|0x0000,
                   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);

i am getting correct output in java and not in objective c.please provide solution for objective c code

StackUser
  • 31
  • 1
  • 5
  • I have to admit at first glance, it doesn't look like you're actually doing TRIPLE DES decryption on the mac side. When you call CCCrypt, you use kCCAlgorithmDES, and kCCKeySizeDES, which to me look like SINGLE DES!!! – trumpetlicks Jul 31 '14 at 12:35
  • Also, you're not converting the `myData` to hexadecimal, you're just putting it in a string. That doesn't provide the same exact result. – B.R.W. Jul 31 '14 at 12:39
  • Thanks for ur reply ..i tried with kCCAlgorithm3DES, and kCCKeySize3DES, but not getting the exact output – StackUser Jul 31 '14 at 13:07

0 Answers0