Question

I'm writing a new version of program in C++ using Qt library, and want to maintain compatibility with the old version written in C#.

How can I convert this code to C++ / Qt?

DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
emoryStream stream = new  MemoryStream(Convert.FromBase64String(p0));
CryptoStream stream2 = new CryptoStream(stream,     dESCryptoServiceProvider.CreateDecryptor(Encoding.ASCII.GetBytes("B5B126C5"), 
Encoding.UTF32.GetBytes("0907427F93EC3A3FCFDFEBE3CB55011")), CryptoStreamMode.Read);
StreamReader streamReader = new StreamReader(stream2);
String text = streamReader.ReadToEnd();

My actually Qt/C++ code:

        QByteArray encrypted = code.toLatin1();
        encrypted = QByteArray::fromBase64(encrypted);

     /////////////////////////////////////////////////////////////////////////////////////
        DES_cblock key = { 0x9, 0x7, 0x42, 0x7, 0xf9, 0x3e, 0xC3, 0xa3, 0xfC, 0xfd, 0xfe, 0xbe, 0x3c, 0xb5, 0x50, 0x85 };
                    //this key is too long...

    ///////////////////////////////////////////////////////////////////////////////////////
        DES_cblock iv = { 0xB5, 0xb1, 0x26, 0xc11 };

        DES_key_schedule schedule;
        unsigned char decrypted[encrypted.size()];

        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);
        unsigned int data_size = 0;

        QString text = QByteArray::fromRawData((char * )decrypted, data_size);

When I try to build receive an error:

C:\Project1_Qt\trunk\Core\OldHashDecoder.cpp:1383: error: too many initializers for 'DES_cblock {aka unsigned char [8]}'

Please help Best regards

Was it helpful?

Solution

You have several issues here ongoing.

  • It seems that you have a long key as you noted yourself in the comment. You will need to make that shorter.

  • DES keys are of fixed length as rightfully noted in the comment.

  • You cannot fix 0xc11 in one byte.

As for the C# code, you pasted, I am not sure. I am not good at that language, but perhaps it may have truncated the length for you silently.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top