Archived Forum Post

Index of archived forum posts

Question:

3DES Decrypt with C++Builder

Jan 12 '13 at 14:13

Hello ,

I've already sent email to support, but I post it here, too, for sharing Q&A.

I've just tried Crypt product.

I have to create code to decrypt 3DES string.

I decrypted my encrypted string with OpenSSL command line like followings,

echo -en "xcax99x19x61xd1x21x4f......." | openssl des3 -d -K e3c254b6ec976b4638f216c83de61504166d7.... -iv 4eed0d2f1c....

I have to create code those function in C++Builder and your product.

Now my code is..

const char * ivHex; ivHex = "4eed0d2f1c48...."; crypt.SetEncodedIV(ivHex,"hex");

const char * keyHex; keyHex = "e3c254b6ec976b4638f216c83de61504166d7070ae97...."; crypt.SetEncodedKey(keyHex,"hex");

char encrypted[] = {0xca,0x99,0x19,0x61,0xd1,0x21,0x4f,0x77,0x3e,0x0d,0xd3,0xcc,0x07,0x9f,0x..,0x..}; Is that correct ? And I don't know how to decrypt encrypted string.

Please advice.

Nobuo Miwa


Answer

Please provide the full data so that I can actually run the openssl command to see the results.


Answer

Thanks.

OpenSSL command line is followings..

echo -en "xcax99x19x61xd1x21x4fx77x3ex0dxd3xccx07x9fx56x24" | openssl des3 -d -K e3c254b6ec976b4638f216c83de61504166d7070ae97bfb6 -iv 4eed0d2f1c48e0f1

Answer is "berobero"

Regards


Answer

Thank you for reply.

You need (back slash) with each byte of echo string

"(back slash)xca(back slash)x99(back slash)x19(back slash)x61(back slash)xd1(back slash)x21(back slash)x4f(back slash)x77(back slash)x3e(back slash)x0d(back slash)xd3(back slash)xcc(back slash)x07(back slash)x9f(back slash)x56(back slash)x24"

Somehow back slash has gone with my last post. (back slash)xca means a binary character like 0xca.

Regards, Nobuo Miwa


Answer

Here's sample code that works:

void des3_decrypt(void)
    {
    CkCrypt2 crypt;

crypt.UnlockComponent("test");

crypt.put_CryptAlgorithm("3des");
crypt.put_KeyLength(192);

const char * ivHex; 
ivHex = "4eed0d2f1c48e0f1"; 
crypt.SetEncodedIV(ivHex,"hex");

const char * keyHex; 
keyHex = "e3c254b6ec976b4638f216c83de61504166d7070ae97bfb6";

crypt.SetEncodedKey(keyHex,"hex");

CkByteData bData;
bData.appendEncoded("ca991961d1214f773e0dd3cc079f5624","hex");

printf("%s\n",crypt.decryptString(bData));
}

Answer

Great thanks !

It worked !


Answer

I'm sorry.. Another decryption case..

I put followings in your code..

ivHex = "f80150b898ddc36f";

keyHex = "f59323a693069c4aeecc65cc89322504f698fd00c2c6db3c";

bData.appendEncoded("174b2e13d63703efbfcb4f060ef76f037ca9aa2789613696b30413cb77fb6cdc47352b3e7f62b7c0c278c6348af4ff6cf7e598bde24e92b508b08c5d34ce1d498337c741c1ff961f2c8904fb9c8a145a2e5c156ff991826284b209491fb2dd86","hex");

Then I got 60 bytes length.

But with openssl command followings..

echo -en "\x17\x4b\x2e\x13\xd6\x37\x03\xef\xbf\xcb\x4f\x06\x0e\xf7\x6f\x03\x7c\xa9\xaa\x27\x89\x61\x36\x96\xb3\x04\x13\xcb\x77\xfb\x6c\xdc\x47\x35\x2b\x3e\x7f\x62\xb7\xc0\xc2\x78\xc6\x34\x8a\xf4\xff\x6c\xf7\xe5\x98\xbd\xe2\x4e\x92\xb5\x08\xb0\x8c\x5d\x34\xce\x1d\x49\x83\x37\xc7\x41\xc1\xff\x96\x1f\x2c\x89\x04\xfb\x9c\x8a\x14\x5a\x2e\x5c\x15\x6f\xf9\x91\x82\x62\x84\xb2\x09\x49\x1f\xb2\xdd\x86" | openssl des3 -d -K f59323a693069c4aeecc65cc89322504f698fd00c2c6db3c -iv f80150b898ddc36f > tmp.dat

I got 91 bytes from tmp.dat

Each result byte were little differences from openssl's one.

Please advice to get 91 bytes from your code.


Answer

Hello,

How about above case ?

Please advice, Regards, Nobuo Miwa


Answer

The KeyLength for your new case is 384, not 192. You need to multiply the length of your key string by 8 to get the appropriate key length.


Answer

Thank you for advice.

I set to key length like this..

crypt.put_KeyLength(384);

But I still got 60 bytes of decrypted string length.

Please advice, Nobuo MMiwa


Answer

Here's my VB6 code that is returning 91 characters, it might be of some help:

Sub Test5()
   Dim lo_Crypt As New CHILKATCRYPT2Lib.ChilkatCrypt2

   With lo_Crypt
        .UnlockComponent "UNLOCKCODE"
        .EncodingMode = "hex"
        .CryptAlgorithm = "3des"
        .KeyLength = 384
        .SetEncodedIV "f80150b898ddc36f", "hex"
        .SetEncodedKey "f59323a693069c4aeecc65cc89322504f698fd00c2c6db3c", "hex"

        Debug.Print .DecryptStringENC("174b2e13d63703efbfcb4f060ef76f037ca9aa2789613696b30413cb77fb6cdc47352b3e7f62b7c0c278c6348af4ff6cf7e598bde24e92b508b08c5d34ce1d498337c741c1ff961f2c8904fb9c8a145a2e5c156ff991826284b209491fb2dd86")
    End With
End Sub