#include #include #include #include #include #include "qaesencryption.h" //const uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; /*inline int getAlignedSize(int currSize, int alignment) { int padding = (alignment - currSize % alignment) % alignment; return currSize + padding; }*/ /*QString encodeText(const QString rawText, const QString key); QString decodeText(const QString hexEncodedText, const QString key); */ int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QByteArray srcString = "Hello World"; QString key = "1234"; QCryptographicHash hash(QCryptographicHash::Md5); hash.addData(key.toUtf8()); QByteArray keyData = hash.result(); QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB); QByteArray encodedString = encryption.encode(srcString, keyData); QByteArray decodedString = encryption.decode(encodedString, keyData); qDebug() << "Plain Text" << srcString; //qDebug() << "Crypt" << QString(encodedString); qDebug() << "Decoded string is " << QString(decodedString); return 0; } /* QString encodeText(const QString rawText, const QString key) { QCryptographicHash hash(QCryptographicHash::Md5); hash.addData(key.toUtf8()); QByteArray keyData = hash.result(); const ushort *rawData = rawText.utf16(); void *rawDataVoid = (void*)rawData; const char *rawDataChar = static_cast(rawDataVoid); QByteArray inputData; // ushort is 2*uint8_t + 1 byte for '\0' inputData.append(rawDataChar, rawText.size() * 2 + 1); const int length = inputData.size(); int encryptionLength = getAlignedSize(length, 16); QByteArray encodingBuffer(encryptionLength, 0); inputData.resize(encryptionLength); AES_CBC_encrypt_buffer((uint8_t*)encodingBuffer.data(), (uint8_t*)inputData.data(), encryptionLength, (const uint8_t*)keyData.data(), iv); QByteArray data(encodingBuffer.data(), encryptionLength); QString hex = QString::fromLatin1(data.toHex()); return hex; } QString decodeText(const QString hexEncodedText, const QString key) { QCryptographicHash hash(QCryptographicHash::Md5); hash.addData(key.toUtf8()); QByteArray keyData = hash.result(); const int length = hexEncodedText.size(); int encryptionLength = getAlignedSize(length, 16); QByteArray encodingBuffer(encryptionLength, 0); QByteArray encodedText = QByteArray::fromHex(hexEncodedText.toLatin1()); encodedText.resize(encryptionLength); AES_CBC_decrypt_buffer((uint8_t*)encodingBuffer.data(), (uint8_t*)encodedText.data(), encryptionLength, (const uint8_t*)keyData.data(), iv); void *data = encodingBuffer.data(); const ushort *decodedData = static_cast(data); QString result = QString::fromUtf16(decodedData); return result; }*/