[HELP] AES CBC & ECB Encryption & Decryption
-
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
@
function decryptECB($data)
{
$BLOB_ENCRYPTION_KEY = 'M02cnQ51Ji97vwT4';
return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $BLOB_ENCRYPTION_KEY, pad($data), MCRYPT_MODE_ECB);
}function encryptECB($data)
{
$BLOB_ENCRYPTION_KEY = 'M02cnQ51Ji97vwT4';
return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $BLOB_ENCRYPTION_KEY, pad($data), MCRYPT_MODE_ECB);
}function pad($data, $blocksize = 16)
{
$pad = $blocksize - (strlen($data) % $blocksize);
return $data . str_repeat(chr($pad), $pad);
}// FOR STORIES
function decryptCBC($data, $key, $iv)
{
// Decode the key and IV.
$iv = base64_decode($iv);
$key = base64_decode($key);// Decrypt the data.
$data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv);
$padding = ord($data[strlen($data) - 1]);return substr($data, 0, -$padding);
}@
I am programming this to the BlackBerry10 Platform
Do you have any suggested libs that has the similar functions as with PHP?
I tried AESCryptoDemmo 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
@
QString filename = "data/files/blobs/storyphoto.jpg";
QFile* file = new QFile(filename);
if(file->open(QIODevice::ReadOnly))
{
qDebug() << "BYTES: " + QString::fromAscii(file->readAll()) + ", SIZE: " + file->size();QString decryptedBlob = decrypt(file->readAll());
qDebug() << "DECRYPTED BLOB: " + toHex(file->readAll());
QFile* newFile = new QFile("data/files/blobs/NEW.jpg");
if (!newFile->open(QIODevice::WriteOnly))
{
qDebug() << "PROBLEM OPENING FILE: " + filename;
}
else
{
newFile->write(decryptedBlob.toAscii());
}newFile->close();
}
else
{
qDebug() << "CANT OPEN: " + filename;
}file->close();
I have also modified some AESCryptoDemo functions - Just to return valuesQString ApplicationUI::encrypt(QString data)
{
QString encryptedData = "";QByteArray in(data.toUtf8());
pad(in);
QByteArray out(in.length(), 0);if (crypt(true, in, out))
{
encryptedData = toHex(out);
}return encryptedData;
}QString ApplicationUI::decrypt(QString data)
{
QString decryptedBlob = "";QByteArray in;
if (!fromHex(data, in))
{
qDebug() << "Cipher text is not valid hex";
return "";
}QByteArray out(in.length(), 0);
if (crypt(false, in, out))
{
if (removePadding(out))
{
decryptedBlob = QString::fromUtf8(out.constData(), out.length());
}
}return decryptedBlob;
}QString ApplicationUI::toHex(const QByteArray & in)
{
static char hexChars[] = "0123456789abcdef";const char * c = in.constData();
QString toReturn;for (int i = 0; i < in.length(); ++i)
{
toReturn += hexChars[(c[i] >> 4) & 0xf];
toReturn += hexChars[(c[i]) & 0xf];
}return toReturn;
}@
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.
@
QString key = "i17ZJekcUf3J/P/EFk3fmXHSBnxfMvf6BFuN0kbI57Q=";
QString iv = "wmUlWE+EmUMguG/ealoMcg==";QString hexKey = toHex(key.toAscii());
QString hexIV = toHex(iv.toAscii());setKey(hexKey);
setIV(hexIV);qDebug() << "HEX KEY: " + hexKey + ", ORIG KEY: " + key;
qDebug() << "HEX IV: " + hexIV + ", ORIG IV: " + iv;QString encrypted = encrypt("data");
QString decrypted = decrypt(encrypted);qDebug() << "ENCRYPTED " + encrypted + ", DECRYPTED: " + decrypted;
@
running the above code I get
"HEX KEY: 6931375a4a656b635566334a2f502f45466b33666d584853426e78664d7666364246754e306b62493537513d, 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.