adding unit testing class

This commit is contained in:
Matteo Brichese 2017-07-07 10:03:16 -07:00
parent 1ab589a62e
commit 67f06e475b
4 changed files with 241 additions and 231 deletions

202
aestest.cpp Normal file
View File

@ -0,0 +1,202 @@
#include "aestest.h"
#include <QByteArray>
#include <QCryptographicHash>
#include "qaesencryption.h"
void AesTest::ECB128Crypt()
{
QByteArray hexText, keyHex, outputHex;
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
const quint8 key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
const quint8 text[16] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
const quint8 output[16] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97};
for (int i=0; i<16; i++){
keyHex.append(key[i]);
hexText.append(text[i]);
outputHex.append(output[i]);
}
QCOMPARE(encryption.encode(hexText, keyHex), outputHex);
}
void AesTest::ECB128Decrypt()
{
QByteArray hexText, keyHex, outputHex;
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
const quint8 key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
const quint8 text[16] = {0x43, 0xb1, 0xcd, 0x7f, 0x59, 0x8e, 0xce, 0x23, 0x88, 0x1b, 0x00, 0xe3, 0xed, 0x03, 0x06, 0x88};
const quint8 output[16] = {0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef};
for (int i=0; i<16; i++){
keyHex.append(key[i]);
hexText.append(text[i]);
outputHex.append(output[i]);
}
QCOMPARE(encryption.decode(hexText, keyHex), outputHex);
}
void AesTest::ECB192Crypt()
{
QByteArray hexText, keyHex, outputHex;
QAESEncryption encryption(QAESEncryption::AES_192, QAESEncryption::ECB);
uint8_t key[24] = { 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5, 0x62, 0xf8,
0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b};
uint8_t text[16] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
uint8_t output[16] = { 0xbd, 0x33, 0x4f, 0x1d, 0x6e, 0x45, 0xf2, 0x5f, 0xf7, 0x12, 0xa2, 0x14, 0x57, 0x1f, 0xa5, 0xcc };
for (int i=0; i<16 ; i++){
hexText.append(text[i]);
outputHex.append(output[i]);
}
for (int i=0; i<24 ; i++)
keyHex.append(key[i]);
QCOMPARE(encryption.encode(hexText, keyHex), outputHex);
}
void AesTest::ECB192Decrypt()
{
QByteArray hexText, keyHex, outputHex;
QAESEncryption encryption(QAESEncryption::AES_192, QAESEncryption::ECB);
uint8_t key[24] = { 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5, 0x62, 0xf8,
0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b};
uint8_t text[16] = { 0xbd, 0x33, 0x4f, 0x1d, 0x6e, 0x45, 0xf2, 0x5f, 0xf7, 0x12, 0xa2, 0x14, 0x57, 0x1f, 0xa5, 0xcc };
uint8_t output[16] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
for (int i=0; i<16 ; i++){
hexText.append(text[i]);
outputHex.append(output[i]);
}
for (int i=0; i<24 ; i++)
keyHex.append(key[i]);
QCOMPARE(encryption.decode(hexText, keyHex), outputHex);
}
void AesTest::ECB256Crypt()
{
QByteArray hexText, keyHex, outputHex;
QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 };
uint8_t text[16] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
uint8_t output[16] = { 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c, 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8 };
for (int i=0; i<16 ; i++){
hexText.append(text[i]);
outputHex.append(output[i]);
}
for (int i=0; i<32 ; i++)
keyHex.append(key[i]);
QCOMPARE(encryption.encode(hexText, keyHex), outputHex);
}
void AesTest::ECB256Decrypt()
{
QByteArray hexText, keyHex, outputHex;
QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 };
uint8_t output[16] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
uint8_t text[16] = { 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c, 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8 };
for (int i=0; i<16 ; i++){
hexText.append(text[i]);
outputHex.append(output[i]);
}
for (int i=0; i<32 ; i++)
keyHex.append(key[i]);
QCOMPARE(encryption.decode(hexText, keyHex), outputHex);
}
void AesTest::ECB256String()
{
QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
QString inputStr("The Advanced Encryption Standard (AES), also known by its original name Rijndael "
"is a specification for the encryption of electronic data established by the U.S. "
"National Institute of Standards and Technology (NIST) in 2001");
QString key("123456789123");
QByteArray hashKey = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Sha256);
QByteArray encodeText = encryption.encode(inputStr.toLocal8Bit(), hashKey);
QCOMPARE(QString(encryption.decode(encodeText, hashKey)), inputStr);
}
void AesTest::CBC128Crypt()
{
QByteArray hexText, keyHex, outputHex, ivHex;
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::CBC);
const quint8 key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
const quint8 iv[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
const quint8 text[64] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10};
const quint8 output[64] = {0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7};
for (int i=0; i<64; i++){
if (i<16) {
keyHex.append(key[i]);
ivHex.append(iv[i]);
}
hexText.append(text[i]);
outputHex.append(output[i]);
}
QCOMPARE(encryption.encode(hexText, keyHex, ivHex), outputHex);
}
void AesTest::CBC128Decrypt()
{
QByteArray hexText, keyHex, outputHex, ivHex;
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::CBC);
const quint8 key[16] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
const quint8 iv[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
const quint8 text[64] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
const quint8 output[64] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
for (int i=0; i<64; i++){
if (i<16) {
keyHex.append(key[i]);
ivHex.append(iv[i]);
}
hexText.append(text[i]);
outputHex.append(output[i]);
}
QCOMPARE(encryption.decode(hexText, keyHex, ivHex), outputHex);
}

30
aestest.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef AESTEST_H
#define AESTEST_H
#include <QObject>
#include <QTest>
class AesTest : public QObject
{
Q_OBJECT
private slots:
void initTestCase() {}
void ECB128Crypt();
void ECB128Decrypt();
void ECB192Crypt();
void ECB192Decrypt();
void ECB256Crypt();
void ECB256Decrypt();
void ECB256String();
void CBC128Crypt();
void CBC128Decrypt();
void cleanupTestCase(){}
};
#endif // AESTEST_H

232
main.cpp
View File

@ -1,237 +1,13 @@
#include <QCoreApplication>
#include <QByteArray>
#include <QDebug>
#include <QString>
#include <QCryptographicHash>
#include "qaesencryption.h"
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));
}
return ret;
}
bool testECB128Decrypt()
{
QByteArray hexText, keyHex, outputHex;
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
const quint8 key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
const quint8 text[16] = {0x43, 0xb1, 0xcd, 0x7f, 0x59, 0x8e, 0xce, 0x23, 0x88, 0x1b, 0x00, 0xe3, 0xed, 0x03, 0x06, 0x88};
const quint8 output[16] = {0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef};
for (int i=0; i<16; i++)
{
keyHex.append(key[i]);
hexText.append(text[i]);
outputHex.append(output[i]);
}
QByteArray decodedHex = encryption.decode(hexText, keyHex);
return (outputHex == decodedHex);
}
bool testECB128Crypt()
{
QByteArray hexText, keyHex, outputHex;
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
const quint8 key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
const quint8 text[16] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
const quint8 output[16] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97};
for (int i=0; i<16; i++)
{
keyHex.append(key[i]);
hexText.append(text[i]);
outputHex.append(output[i]);
}
QByteArray encodedHex = encryption.encode(hexText, keyHex);
return (outputHex == encodedHex);
}
bool testCBC128Crypt()
{
QByteArray hexText, keyHex, outputHex, ivHex;
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::CBC);
const quint8 key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
const quint8 iv[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
const quint8 text[64] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10};
const quint8 output[64] = {0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7};
for (int i=0; i<64; i++)
{
if (i<16) {
keyHex.append(key[i]);
ivHex.append(iv[i]);
}
hexText.append(text[i]);
outputHex.append(output[i]);
}
QByteArray encodedHex = encryption.encode(hexText, keyHex, ivHex);
return (outputHex == encodedHex);
}
bool testCBC128Decrypt()
{
QByteArray hexText, keyHex, outputHex, ivHex;
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::CBC);
const quint8 key[16] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
const quint8 iv[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
const quint8 text[64] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
const quint8 output[64] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
for (int i=0; i<64; i++)
{
if (i<16) {
keyHex.append(key[i]);
ivHex.append(iv[i]);
}
hexText.append(text[i]);
outputHex.append(output[i]);
}
QByteArray decodeHex = encryption.decode(hexText, keyHex, ivHex);
return (outputHex == decodeHex);
}
bool testECB256Crypt()
{
QByteArray hexText, keyHex, outputHex;
QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 };
uint8_t text[16] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
uint8_t output[16] = { 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c, 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8 };
for (int i=0; i<16 ; i++)
{
hexText.append(text[i]);
outputHex.append(output[i]);
}
for (int i=0; i<32 ; i++)
keyHex.append(key[i]);
QByteArray encodedHex = encryption.encode(hexText, keyHex);
encodedHex.truncate(16);
return (outputHex == encodedHex);
}
bool testECB256Decrypt()
{
QByteArray hexText, keyHex, outputHex;
QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 };
uint8_t output[16] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
uint8_t text[16] = { 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c, 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8 };
for (int i=0; i<16 ; i++)
{
hexText.append(text[i]);
outputHex.append(output[i]);
}
for (int i=0; i<32 ; i++)
keyHex.append(key[i]);
QByteArray decodeHex = encryption.decode(hexText, keyHex);
decodeHex.truncate(16);
return (outputHex == decodeHex);
}
bool testECB128String()
{
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
QString inputStr("The Advanced Encryption Standard (AES), also known by its original name Rijndael "
"is a specification for the encryption of electronic data established by the U.S. "
"National Institute of Standards and Technology (NIST) in 2001");
QString key("123456789");
QByteArray hashKey = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Md5);
QByteArray encodeText = encryption.encode(inputStr.toLocal8Bit(), hashKey);
QByteArray decodeText = encryption.decode(encodeText, hashKey);
return (QString(decodeText) == inputStr);
}
bool testECB256String()
{
QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
QString inputStr("The Advanced Encryption Standard (AES), also known by its original name Rijndael "
"is a specification for the encryption of electronic data established by the U.S. "
"National Institute of Standards and Technology (NIST) in 2001");
QString key("123456789");
QByteArray hashKey = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Sha256);
QByteArray encodeText = encryption.encode(inputStr.toLocal8Bit(), hashKey);
QByteArray decodeText = encryption.decode(encodeText, hashKey);
return (QString(decodeText) == inputStr);
}
#include <QTest>
#include "aestest.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "Testing in progress...";
Q_ASSERT(testECB128Crypt());
Q_ASSERT(testECB128Decrypt());
Q_ASSERT(testECB256Crypt());
Q_ASSERT(testECB256Decrypt());
Q_ASSERT(testCBC128Crypt());
Q_ASSERT(testCBC128Decrypt());
Q_ASSERT(testECB128String());
Q_ASSERT(testECB256String());
qDebug() << "Testing with instance member DONE";
AesTest test1;
QTest::qExec(&test1);
return 0;
}

View File

@ -1,4 +1,4 @@
QT += core
QT += core testlib
QT -= gui
CONFIG += c++11
@ -10,7 +10,8 @@ CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
qaesencryption.cpp
qaesencryption.cpp \
aestest.cpp
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
@ -24,5 +25,6 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
HEADERS += \
qaesencryption.h
qaesencryption.h \
aestest.h