PDA

View Full Version : Decrypting DES string in Qt using OpenSSL



Coder5546
17th March 2014, 10:26
Hi, I have a problem I trying to rewrite the function from C # to Qt/C++, but my code does not work properly.

Old C# code (oryginal)



DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
MemoryStream stream = new MemoryStream(Convert.FromBase64String(inputString) );
CryptoStream stream2 = new CryptoStream(stream, dESCryptoServiceProvider.CreateDecryptor(Encoding. ASCII.GetBytes("29A15767"),
Encoding.UTF32.GetBytes("428848EDEBA04AF4C4D04F5ADCF0305A")), CryptoStreamMode.Read);
StreamReader streamReader = new StreamReader(stream2);
string text = streamReader.ReadToEnd();


My code:



DES_cblock key;
DES_cblock iv;
memcpy(key, "29A15767", 8);
memcpy(iv, "428848EDEBA04AF4C4D04F5ADCF0305A", 32);

QString stringToDecrypt .....
QByteArray encrypted = stringToDecrypt.toLatin1();
encrypted = QByteArray::fromBase64(encrypted);

unsigned char decrypted[encrypted.size()];
DES_key_schedule schedule;
DES_set_odd_parity(&key);
DES_set_key_checked(&key, &schedule);
DES_ncbc_encrypt((unsigned char *)encrypted.constData(), (unsigned char *)decrypted, encrypted.size(), &schedule, &iv, DES_DECRYPT);

QString text = QByteArray::fromRawData((char *)decrypted, encrypted.size());

Any suggestions ? :)
Regards

anda_skoa
17th March 2014, 13:16
Does not work properly is a rather poor description. What does it do vs what do you expect it to do?

Cheers,

ChrisW67
17th March 2014, 23:19
The IV for DES is 8 bytes, not 32, and DES_cblock is also 8 bytes long so something is badly wrong there (and likely to crash your program intermittently).

http://stackoverflow.com/questions/7862671/decrypting-des-with-openssl

Coder5546
18th March 2014, 09:41
Thank you very much for your reply. I'm sorry, I should write more informations.
The main problem is that the string is not well decoded, it's bad ...
Yes, I know that the key should have a length of 8 bytes, but in C# is 32 and works perfectly.

Do you have any ideas how to apply 32 byte key ?

ChrisW67
18th March 2014, 10:49
The main problem is that the string is not well decoded, it's bad ...
That's good. It would be very bad if an incorrectly keyed decryptor produced useful output.

Do you have any ideas how to apply 32 byte key ?
No. A DES key is 56bits, typically expressed as 8 bytes with a parity bit in each.
Data Encryption Standard (http://www.wikipedia.org/wiki/Data_Encryption_Standard)
The initialisation vector (IV) for DES CBC is exactly 64-bits because that is the DES encryption block size.
Cipher Block Chaining (http://www.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher-block_chaining_.28CBC.29)

You need to work out exactly what bytes Encoding.ASCII.GetBytes("29A15767") and Encoding.UTF32.GetBytes("428848EDEBA04AF4C4D04F5ADCF0305A") return and work out how to mimic that. My best guess based on a quick scan of the C# docs is:


// Encoding.ASCII.GetBytes("29A15767")
// '2' '9' 'A' '1' '5' '7' '6' '7'
DES_cblock key = { 0x32, 0x39, 0x41, 0x31, 0x35, 0x37, 0x36, 0x37 };

// Encoding.UTF32.GetBytes("428848EDEBA04AF4C4D04F5ADCF0305A")
// Only the first two letters required to give 8 byte IV
// '4' '2'
DES_cblock iv = { 0x34, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00 };