Qt-AES/main.cpp

66 lines
2.1 KiB
C++
Raw Normal View History

2017-06-21 16:04:33 -07:00
#include <QCoreApplication>
#include <QByteArray>
#include <QDebug>
#include <QString>
2017-06-21 16:49:00 -07:00
#include <QCryptographicHash>
2017-06-21 16:04:33 -07:00
#include "qaesencryption.h"
2017-06-22 17:01:45 -07:00
QString print(QByteArray in)
{
QString ret="";
for (int i=0; i < in.size();i++) {
QString number = QString::number((quint8)in.at(i), 16);
if (number.size()==1)
number.insert(0, "0");
ret.append(QString("%1").arg(number));
2017-06-22 17:01:45 -07:00
}
return ret;
}
2017-06-21 16:04:33 -07:00
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
2017-06-22 17:01:45 -07:00
const quint8 text[16] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
const quint8 key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
QByteArray hexText, keyHex;
2017-06-21 16:49:00 -07:00
2017-06-22 17:01:45 -07:00
for (int i=0; i<16; i++)
{
hexText.append(text[i]);
keyHex.append(key[i]);
2017-06-22 17:01:45 -07:00
}
2017-06-21 16:49:00 -07:00
2017-06-21 16:04:33 -07:00
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
2017-06-23 15:07:50 -07:00
QByteArray encodedHex = encryption.encode(hexText, keyHex);
QByteArray decodedHex = encryption.decode(encodedHex, keyHex);
2017-06-21 16:49:00 -07:00
qDebug() << "=========================HEX==========================\n";
qDebug() << "Key" << print(keyHex);
qDebug() << "Text" << print(hexText);
qDebug() << "";
qDebug() << "Crypt" << print(encodedHex);
qDebug() << "";
qDebug() << "Decoded text is " << print(decodedHex);
qDebug() << "\n=======================STRING=========================";
QString keyString = "25f9e794323b453885f5181f1b624d0b";
QString plainText = "AES is a subset of the Rijndael cipher developed by two Belgian cryptographers, Vincent Rijmen and Joan Daemen.";
QByteArray encodedString = encryption.encode(plainText.toLocal8Bit(), keyString.toLocal8Bit());
QByteArray decodedString = encryption.decode(encodedString, keyString.toLocal8Bit());
qDebug() << "Key" << keyString;
qDebug() << "Text" << plainText;
2017-06-22 17:01:45 -07:00
qDebug() << "";
qDebug() << "Crypt HEX" << print(encodedString);
2017-06-22 17:01:45 -07:00
qDebug() << "";
qDebug() << "Decoded string is " << QString::fromLocal8Bit(decodedString);
2017-06-21 16:04:33 -07:00
return 0;
}