mirror of
https://github.com/QuasarApp/Qt-AES.git
synced 2025-04-26 21:54:32 +00:00
commit
b616caf7c1
11
README.md
11
README.md
@ -28,7 +28,12 @@ QAESEncryption::ExpandKey => expandKey(...)
|
||||
```
|
||||
|
||||
#### Padding
|
||||
Please note that as of today all input that does not comes as a muptiple of 16 will be padded with zeros to the closest multiple value.
|
||||
By default the padding method is `ISO`, however, the class supports:
|
||||
```
|
||||
ZERO
|
||||
PKCS7
|
||||
ISO
|
||||
```
|
||||
|
||||
### Example
|
||||
Sample code using a 128bit key in ECB mode
|
||||
@ -59,6 +64,10 @@ Example for 256bit CBC using QString
|
||||
|
||||
QByteArray encodeText = encryption.encode(inputStr.toLocal8Bit(), hashKey, hashIV);
|
||||
QByteArray decodeText = encryption.decode(encodeText, hashKey, hashIV);
|
||||
|
||||
QString decodedString = QString(QAESEncryption::RemovePadding(decodeText));
|
||||
|
||||
//decodedString == inputStr !!
|
||||
```
|
||||
|
||||
### Example via static invocation
|
||||
|
1
main.cpp
1
main.cpp
@ -2,6 +2,7 @@
|
||||
#include <QTest>
|
||||
#include "unit_test/aestest.h"
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication a(argc, argv);
|
||||
|
@ -85,6 +85,7 @@ QByteArray QAESEncryption::getPadding(int currSize, int alignment)
|
||||
{
|
||||
QByteArray ret(0);
|
||||
int size = (alignment - currSize % alignment) % alignment;
|
||||
if (size == 0) return ret;
|
||||
switch(m_padding)
|
||||
{
|
||||
case PADDING::ZERO:
|
||||
@ -158,6 +159,29 @@ QByteArray QAESEncryption::expandKey(const QByteArray &key)
|
||||
return roundKey;
|
||||
}
|
||||
|
||||
QByteArray QAESEncryption::RemovePadding(const QByteArray &rawText, QAESEncryption::PADDING padding)
|
||||
{
|
||||
QByteArray ret(rawText);
|
||||
switch (padding)
|
||||
{
|
||||
case PADDING::ZERO:
|
||||
//Works only if the last byte of the decoded array is not zero
|
||||
while (ret.at(ret.length()-1) == 0x00)
|
||||
ret.remove(ret.length()-1, 1);
|
||||
break;
|
||||
case PADDING::PKCS7:
|
||||
ret.remove(ret.length() - ret.at(ret.length()-1), ret.at(ret.length()-1));
|
||||
break;
|
||||
case PADDING::ISO:
|
||||
ret.truncate(ret.lastIndexOf(0x80));
|
||||
break;
|
||||
default:
|
||||
//do nothing
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// This function adds the round key to state.
|
||||
// The round key is added to the state by an XOR function.
|
||||
void QAESEncryption::addRoundKey(const quint8 round, const QByteArray expKey)
|
||||
|
@ -29,8 +29,9 @@ public:
|
||||
static QByteArray Crypt(QAESEncryption::AES level, QAESEncryption::MODE mode, const QByteArray &rawText, const QByteArray &key, const QByteArray &iv = NULL);
|
||||
static QByteArray Decrypt(QAESEncryption::AES level, QAESEncryption::MODE mode, const QByteArray &rawText, const QByteArray &key, const QByteArray &iv = NULL);
|
||||
static QByteArray ExpandKey(QAESEncryption::AES level, QAESEncryption::MODE mode, const QByteArray &key);
|
||||
static QByteArray RemovePadding(const QByteArray &rawText, QAESEncryption::PADDING padding = PADDING::ISO);
|
||||
|
||||
QAESEncryption(QAESEncryption::AES level, QAESEncryption::MODE mode, QAESEncryption::PADDING padding = QAESEncryption::ZERO);
|
||||
QAESEncryption(QAESEncryption::AES level, QAESEncryption::MODE mode, QAESEncryption::PADDING padding = QAESEncryption::ISO);
|
||||
|
||||
QByteArray encode(const QByteArray &rawText, const QByteArray &key, const QByteArray &iv = NULL);
|
||||
QByteArray decode(const QByteArray &rawText, const QByteArray &key, const QByteArray &iv = NULL);
|
||||
|
@ -59,7 +59,6 @@ void AesTest::ECB128Crypt()
|
||||
{
|
||||
QByteArray hexText, outputHex;
|
||||
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
|
||||
|
||||
QCOMPARE(encryption.encode(in, key16), outECB128);
|
||||
}
|
||||
|
||||
@ -103,7 +102,7 @@ void AesTest::ECB256Decrypt()
|
||||
|
||||
void AesTest::ECB256String()
|
||||
{
|
||||
QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
|
||||
QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB, QAESEncryption::PADDING::ISO);
|
||||
|
||||
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. "
|
||||
@ -113,12 +112,13 @@ void AesTest::ECB256String()
|
||||
QByteArray hashKey = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Sha256);
|
||||
|
||||
QByteArray encodeText = encryption.encode(inputStr.toLocal8Bit(), hashKey);
|
||||
QByteArray decodedText = QAESEncryption::RemovePadding(encryption.decode(encodeText, hashKey), QAESEncryption::PADDING::ISO);
|
||||
|
||||
QCOMPARE(QString(encryption.decode(encodeText, hashKey)), inputStr);
|
||||
QCOMPARE(QString(decodedText), inputStr);
|
||||
}
|
||||
|
||||
|
||||
//==================CBC TESTING=========================
|
||||
////==================CBC TESTING=========================
|
||||
|
||||
void AesTest::CBC128Crypt()
|
||||
{
|
||||
@ -148,6 +148,6 @@ void AesTest::CFB256String()
|
||||
QByteArray hashKey = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Sha256);
|
||||
|
||||
QByteArray encodeText = encryption.encode(inputStr.toLocal8Bit(), hashKey, iv);
|
||||
|
||||
QCOMPARE(QString(encryption.decode(encodeText, hashKey, iv)), inputStr);
|
||||
QByteArray decodedText = QAESEncryption::RemovePadding(encryption.decode(encodeText, hashKey, iv));
|
||||
QCOMPARE(QString(decodedText), inputStr);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user