I am very new to QT and also with Encryption stuffs. Just a week I started fighting to learn encryption stuffs but I am really stuck and don't know what to do.

I just really need to decrypt a downloaded blob. using ECB and also there's another I need with CBC

Here's the working PHP functions I want to convert to QT

Qt Code:
  1. function decryptECB($data)
  2. {
  3. $BLOB_ENCRYPTION_KEY = 'M02cnQ51Ji97vwT4';
  4. return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $BLOB_ENCRYPTION_KEY, pad($data), MCRYPT_MODE_ECB);
  5. }
  6.  
  7. function encryptECB($data)
  8. {
  9. $BLOB_ENCRYPTION_KEY = 'M02cnQ51Ji97vwT4';
  10. return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $BLOB_ENCRYPTION_KEY, pad($data), MCRYPT_MODE_ECB);
  11. }
  12.  
  13. function pad($data, $blocksize = 16)
  14. {
  15. $pad = $blocksize - (strlen($data) % $blocksize);
  16. return $data . str_repeat(chr($pad), $pad);
  17. }
  18.  
  19. // FOR STORIES
  20. function decryptCBC($data, $key, $iv)
  21. {
  22. // Decode the key and IV.
  23. $iv = base64_decode($iv);
  24. $key = base64_decode($key);
  25.  
  26. // Decrypt the data.
  27. $data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv);
  28. $padding = ord($data[strlen($data) - 1]);
  29.  
  30. return substr($data, 0, -$padding);
  31. }
To copy to clipboard, switch view to plain text mode 

I am programming this to the BlackBerry10 Platform

Do you have any suggested libs that has the similar functions as with PHP?

I tried AESCryptoDemo https://github.com/blackberry/Cascad.../AESCryptoDemo and I think it uses CBC which I saw in it's AESParams.cpp file. but no example for ECB

This is how I load downloaded the file in QT

Qt Code:
  1. QString filename = "data/files/blobs/storyphoto.jpg";
  2.  
  3. QFile* file = new QFile(filename);
  4.  
  5. if(file->open(QIODevice::ReadOnly))
  6. {
  7. qDebug() << "BYTES: " + QString::fromAscii(file->readAll()) + ", SIZE: " + file->size();
  8.  
  9. QString decryptedBlob = decrypt(file->readAll());
  10.  
  11. qDebug() << "DECRYPTED BLOB: " + toHex(file->readAll());
  12.  
  13. QFile* newFile = new QFile("data/files/blobs/NEW.jpg");
  14.  
  15. if (!newFile->open(QIODevice::WriteOnly))
  16. {
  17. qDebug() << "PROBLEM OPENING FILE: " + filename;
  18. }
  19. else
  20. {
  21. newFile->write(decryptedBlob.toAscii());
  22. }
  23.  
  24. newFile->close();
  25. }
  26. else
  27. {
  28. qDebug() << "CANT OPEN: " + filename;
  29. }
  30.  
  31. file->close();
  32. I have also modified some AESCryptoDemo functions - Just to return values
  33.  
  34. QString ApplicationUI::encrypt(QString data)
  35. {
  36. QString encryptedData = "";
  37.  
  38. QByteArray in(data.toUtf8());
  39. pad(in);
  40. QByteArray out(in.length(), 0);
  41.  
  42. if (crypt(true, in, out))
  43. {
  44. encryptedData = toHex(out);
  45. }
  46.  
  47. return encryptedData;
  48. }
  49.  
  50. QString ApplicationUI::decrypt(QString data)
  51. {
  52. QString decryptedBlob = "";
  53.  
  54.  
  55. if (!fromHex(data, in))
  56. {
  57. qDebug() << "Cipher text is not valid hex";
  58. return "";
  59. }
  60.  
  61. QByteArray out(in.length(), 0);
  62.  
  63. if (crypt(false, in, out))
  64. {
  65. if (removePadding(out))
  66. {
  67. decryptedBlob = QString::fromUtf8(out.constData(), out.length());
  68. }
  69. }
  70.  
  71. return decryptedBlob;
  72. }
  73.  
  74. QString ApplicationUI::toHex(const QByteArray & in)
  75. {
  76. static char hexChars[] = "0123456789abcdef";
  77.  
  78. const char * c = in.constData();
  79. QString toReturn;
  80.  
  81. for (int i = 0; i < in.length(); ++i)
  82. {
  83. toReturn += hexChars[(c[i] >> 4) & 0xf];
  84. toReturn += hexChars[(c[i]) & 0xf];
  85. }
  86.  
  87. return toReturn;
  88. }
To copy to clipboard, switch view to plain text mode 

Here I am trying to encrypt and decrypt a simple string..

I also don't think if I need to do something with the char "\" because when I qDebug it it is forgotten.


Qt Code:
  1. QString key = "i17ZJekcUf3J\/P\/EFk3fmXHSBnxfMvf6BFuN0kbI57Q=";
  2. QString iv = "wmUlWE+EmUMguG\/ealoMcg==";
  3.  
  4. QString hexKey = toHex(key.toAscii());
  5. QString hexIV = toHex(iv.toAscii());
  6.  
  7. setKey(hexKey);
  8. setIV(hexIV);
  9.  
  10. qDebug() << "HEX KEY: " + hexKey + ", ORIG KEY: " + key;
  11. qDebug() << "HEX IV: " + hexIV + ", ORIG IV: " + iv;
  12.  
  13. QString encrypted = encrypt("data");
  14. QString decrypted = decrypt(encrypted);
  15.  
  16. qDebug() << "ENCRYPTED " + encrypted + ", DECRYPTED: " + decrypted;
To copy to clipboard, switch view to plain text mode 

running the above code I get

"HEX KEY: 6931375a4a656b635566334a2f502f45466b33666d58485342 6e78664d7666364246754e306b62493537513d, ORIG KEY: i17ZJekcUf3J/P/EFk3fmXHSBnxfMvf6BFuN0kbI57Q="

"HEX IV: 776d556c57452b456d554d6775472f65616c6f4d63673d3d, ORIG IV: wmUlWE+EmUMguG/ealoMcg=="

FAILED "AESKey" AESKey 57616 "SB_ERR_BAD_KEY_LEN (57616)"

"Could not create a key. SB_ERR_BAD_KEY_LEN (57616)"

Cipher text is not valid hex
"ENCRYPTED , DECRYPTED: "


Thanks so much! Hope someone can help.