PDA

View Full Version : TripleDESCryptoServiceProvider in C++ using OpenSSl



Coder5546
11th December 2014, 15:12
Hello, I have a problem with TripleDESCryptoServiceProvider function. I need it to ensure backward compatibility in our app.
The general problem is that 3DES decoder doesn't work properly. If You have any suggestions how to fix my problem please answer

Best regards.

C# code:


CryptoStream cryptoStream = new CryptoStream(memoryStream, new TripleDESCryptoServiceProvider
{
Key = passwordDeriveBytes.GetBytes(24),
IV = passwordDeriveBytes.GetBytes(8)
}.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader streamReader = new StreamReader(cryptoStream);
string result = streamReader.ReadToEnd();
memoryStream.Close();
cryptoStream.Close();
return result


QT/C++ Code:



byte *key = passwordDeriveBytes.GetBytes(24); // returns same value as c# function
byte *iv = passwordDeriveBytes.GetBytes(8); // returns same value as c# function

DES_key_schedule ks1, ks2;
QByteArray code1 = QByteArray::fromHex(code.toUtf8());
code1 = QByteArray::fromBase64(code1);

/*
QString string1 = "";
for(int i = 0; i < 24; i++){
string1.append(QString::number(key[i], 16) + "-");
}
QString string2 = "";
for(int i = 0; i < 8; i++){
string2.append(QString::number(iv[i], 16) + "-");
}
*/

//////////////////////////// something is wrong /////////////////////////////////////////////////
byte *output = new byte[code1.size()];

DES_set_key((C_Block *)key, &ks1);
DES_set_key((C_Block *)iv, &ks2);

DES_ecb2_encrypt((C_Block *)code1.constData(),(C_Block *)output, &ks1, &ks2, DES_DECRYPT);
QString rval = QByteArray::fromRawData((char *)output, code1.size());
return rval;

Coder5546
12th December 2014, 16:30
Come on..., no one wants to help me? :D

wysota
12th December 2014, 18:34
What is the question?

Coder5546
12th December 2014, 18:47
Sorry, I should precise my problem.
Why my C++ code doesn't work properly ? Is any difference between OpenSSL and Microsoft 3DES ?
I did something wrong ? I don't have enough experience with C#

I think that something is wrong in this code:

byte *output = new byte[code1.size()];
DES_set_key((C_Block *)key, &ks1);
DES_set_key((C_Block *)iv, &ks2);

DES_ecb2_encrypt((C_Block *)code1.constData(),(C_Block *)output, &ks1, &ks2, DES_DECRYPT);
QString rval = QByteArray::fromRawData((char *)output, code1.size());
return rval;


Just I want to rewrite this code from c# to c++/qt using OpenSSL



CryptoStream cryptoStream = new CryptoStream(memoryStream, new TripleDESCryptoServiceProvider
{
Key = passwordDeriveBytes.GetBytes(24),
IV = passwordDeriveBytes.GetBytes(8)
}.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader streamReader = new StreamReader(cryptoStream);
string result = streamReader.ReadToEnd();
memoryStream.Close();
cryptoStream.Close();
return result


My C++ passwordDeriveBytes.GetBytes working well :)
This page may be usefull: http://msdn.microsoft.com/en-us/library/system.security.cryptography.tripledescryptoservic eprovider%28v=vs.110%29.aspx

Thank You for any help

rmp
20th August 2015, 15:22
Hi,
Were you able to solve this finally? I am facing the same issue and am stuck.