Results 1 to 5 of 5

Thread: Decrypting DES string in Qt using OpenSSL

  1. #1
    Join Date
    Nov 2012
    Location
    Poland/UK
    Posts
    28
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Decrypting DES string in Qt using OpenSSL

    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)

    Qt Code:
    1. DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
    2. MemoryStream stream = new MemoryStream(Convert.FromBase64String(inputString));
    3. CryptoStream stream2 = new CryptoStream(stream, dESCryptoServiceProvider.CreateDecryptor(Encoding.ASCII.GetBytes("29A15767"),
    4. Encoding.UTF32.GetBytes("428848EDEBA04AF4C4D04F5ADCF0305A")), CryptoStreamMode.Read);
    5. StreamReader streamReader = new StreamReader(stream2);
    6. string text = streamReader.ReadToEnd();
    To copy to clipboard, switch view to plain text mode 

    My code:

    Qt Code:
    1. DES_cblock key;
    2. DES_cblock iv;
    3. memcpy(key, "29A15767", 8);
    4. memcpy(iv, "428848EDEBA04AF4C4D04F5ADCF0305A", 32);
    5.  
    6. QString stringToDecrypt .....
    7. QByteArray encrypted = stringToDecrypt.toLatin1();
    8. encrypted = QByteArray::fromBase64(encrypted);
    9.  
    10. unsigned char decrypted[encrypted.size()];
    11. DES_key_schedule schedule;
    12. DES_set_odd_parity(&key);
    13. DES_set_key_checked(&key, &schedule);
    14. DES_ncbc_encrypt((unsigned char *)encrypted.constData(), (unsigned char *)decrypted, encrypted.size(), &schedule, &iv, DES_DECRYPT);
    15.  
    16. QString text = QByteArray::fromRawData((char *)decrypted, encrypted.size());
    To copy to clipboard, switch view to plain text mode 

    Any suggestions ?
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Decrypting DES string in Qt using OpenSSL

    Does not work properly is a rather poor description. What does it do vs what do you expect it to do?

    Cheers,

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Decrypting DES string in Qt using OpenSSL

    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/7...s-with-openssl

  4. #4
    Join Date
    Nov 2012
    Location
    Poland/UK
    Posts
    28
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Decrypting DES string in Qt using OpenSSL

    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 ?

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Decrypting DES string in Qt using OpenSSL

    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
    The initialisation vector (IV) for DES CBC is exactly 64-bits because that is the DES encryption block size.
    Cipher Block Chaining

    You need to work out exactly what bytes Encoding.ASCII.GetBytes("29A15767") and Encoding.UTF32.GetBytes("428848EDEBA04AF4C4D04F5AD CF0305A") return and work out how to mimic that. My best guess based on a quick scan of the C# docs is:
    Qt Code:
    1. // Encoding.ASCII.GetBytes("29A15767")
    2. // '2' '9' 'A' '1' '5' '7' '6' '7'
    3. DES_cblock key = { 0x32, 0x39, 0x41, 0x31, 0x35, 0x37, 0x36, 0x37 };
    4.  
    5. // Encoding.UTF32.GetBytes("428848EDEBA04AF4C4D04F5ADCF0305A")
    6. // Only the first two letters required to give 8 byte IV
    7. // '4' '2'
    8. DES_cblock iv = { 0x34, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00 };
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to ChrisW67 for this useful post:

    Coder5546 (5th June 2014)

Similar Threads

  1. QNetworkAccessManager put openssl
    By MCSpy in forum Qt Programming
    Replies: 2
    Last Post: 24th January 2014, 11:32
  2. Use openssl 1.0.0 with Qt 4.8.3 on Mac
    By sfcheng77 in forum Qt Programming
    Replies: 2
    Last Post: 24th November 2012, 01:26
  3. Qt openssl aes lib
    By zgulser in forum Qt Programming
    Replies: 5
    Last Post: 11th July 2012, 13:23
  4. QT and OpenSSL EVP.h
    By TCB13 in forum Newbie
    Replies: 16
    Last Post: 4th September 2011, 21:04
  5. Encrypting and decrypting with QCA
    By s.g. in forum Qt Programming
    Replies: 1
    Last Post: 6th December 2009, 21:41

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.