From 1809de722fb17a8d9c6a6635766e9ca3b8d0ff7a Mon Sep 17 00:00:00 2001
From: Matteo Brichese <mbrichese@ics.com>
Date: Wed, 28 Mar 2018 17:42:09 -0700
Subject: [PATCH 1/3] working on padding feature

---
 main.cpp              |  1 +
 qaesencryption.cpp    | 25 ++++++++++++
 qaesencryption.h      |  1 +
 unit_test/aestest.cpp | 88 +++++++++++++++++++++----------------------
 unit_test/aestest.h   | 14 +++----
 5 files changed, 78 insertions(+), 51 deletions(-)

diff --git a/main.cpp b/main.cpp
index 40e661c..aea8e26 100644
--- a/main.cpp
+++ b/main.cpp
@@ -2,6 +2,7 @@
 #include <QTest>
 #include "unit_test/aestest.h"
 
+
 int main(int argc, char *argv[])
 {
     QCoreApplication a(argc, argv);
diff --git a/qaesencryption.cpp b/qaesencryption.cpp
index ddc0d3e..e4d2e7f 100644
--- a/qaesencryption.cpp
+++ b/qaesencryption.cpp
@@ -85,6 +85,8 @@ QByteArray QAESEncryption::getPadding(int currSize, int alignment)
 {
     QByteArray ret(0);
     int size = (alignment - currSize % alignment) % alignment;
+//    if (size == 0)
+//        size = alignment;
     switch(m_padding)
     {
     case PADDING::ZERO:
@@ -158,6 +160,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)
diff --git a/qaesencryption.h b/qaesencryption.h
index ff73c53..ae42a2f 100644
--- a/qaesencryption.h
+++ b/qaesencryption.h
@@ -29,6 +29,7 @@ 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::ZERO);
 
     QAESEncryption(QAESEncryption::AES level, QAESEncryption::MODE mode, QAESEncryption::PADDING padding = QAESEncryption::ZERO);
 
diff --git a/unit_test/aestest.cpp b/unit_test/aestest.cpp
index a2d9f34..a99e9e7 100644
--- a/unit_test/aestest.cpp
+++ b/unit_test/aestest.cpp
@@ -59,51 +59,50 @@ void AesTest::ECB128Crypt()
 {
     QByteArray hexText, outputHex;
     QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
-
     QCOMPARE(encryption.encode(in, key16), outECB128);
 }
 
-void AesTest::ECB128Decrypt()
-{
-    QByteArray hexText, outputHex;
-    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
+//void AesTest::ECB128Decrypt()
+//{
+//    QByteArray hexText, outputHex;
+//    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
 
-    QCOMPARE(encryption.decode(outECB128, key16), in);
-}
+//    QCOMPARE(QAESEncryption::RemovePadding(encryption.decode(outECB128, key16)), in);
+//}
 
-void AesTest::ECB192Crypt()
-{
-    QByteArray outputHex;
-    QAESEncryption encryption(QAESEncryption::AES_192, QAESEncryption::ECB);
+//void AesTest::ECB192Crypt()
+//{
+//    QByteArray outputHex;
+//    QAESEncryption encryption(QAESEncryption::AES_192, QAESEncryption::ECB);
 
-    QCOMPARE(encryption.encode(in, key24), outECB192);
-}
+//    QCOMPARE(QAESEncryption::RemovePadding(encryption.encode(in, key24)), outECB192);
+//}
 
-void AesTest::ECB192Decrypt()
-{
-    QByteArray hexText;
-    QAESEncryption encryption(QAESEncryption::AES_192, QAESEncryption::ECB);
+//void AesTest::ECB192Decrypt()
+//{
+//    QByteArray hexText;
+//    QAESEncryption encryption(QAESEncryption::AES_192, QAESEncryption::ECB);
 
-    QCOMPARE(encryption.decode(outECB192, key24), in);
-}
+//    QCOMPARE(QAESEncryption::RemovePadding(encryption.decode(outECB192, key24)), in);
+//}
 
-void AesTest::ECB256Crypt()
-{
-    QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
+//void AesTest::ECB256Crypt()
+//{
+//    QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
 
-    QCOMPARE(encryption.encode(in, key32), outECB256);
-}
+//    QCOMPARE(QAESEncryption::RemovePadding(encryption.encode(in, key32)), outECB256);
+//}
 
-void AesTest::ECB256Decrypt()
-{
-    QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
+//void AesTest::ECB256Decrypt()
+//{
+//    QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
 
-    QCOMPARE(encryption.decode(outECB256, key32), in);
-}
+//    QCOMPARE(QAESEncryption::RemovePadding(encryption.decode(outECB256, key32)), in);
+//}
 
 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,26 +112,27 @@ 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()
-{
-    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::CBC);
+//void AesTest::CBC128Crypt()
+//{
+//    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::CBC);
 
-    QCOMPARE(encryption.encode(inCBC128, key16, iv), outCBC128);
-}
+//    QCOMPARE(QAESEncryption::RemovePadding(encryption.encode(inCBC128, key16, iv)), outCBC128);
+//}
 
-void AesTest::CBC128Decrypt()
-{
-    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::CBC);
+//void AesTest::CBC128Decrypt()
+//{
+//    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::CBC);
 
-    QCOMPARE(encryption.decode(outCBC128, key16, iv), inCBC128);
-}
+//    QCOMPARE(QString(QAESEncryption::RemovePadding(encryption.decode(outCBC128, key16, iv))), inCBC128);
+//}
 
 //=================== CFB TESTING ============================
 
@@ -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);
 }
diff --git a/unit_test/aestest.h b/unit_test/aestest.h
index 2bc2c00..bfc4efd 100644
--- a/unit_test/aestest.h
+++ b/unit_test/aestest.h
@@ -12,18 +12,18 @@ private slots:
     void initTestCase();
 
     void ECB128Crypt();
-    void ECB128Decrypt();
+//    void ECB128Decrypt();
 
-    void ECB192Crypt();
-    void ECB192Decrypt();
+//    void ECB192Crypt();
+//    void ECB192Decrypt();
 
-    void ECB256Crypt();
-    void ECB256Decrypt();
+//    void ECB256Crypt();
+//    void ECB256Decrypt();
 
     void ECB256String();
 
-    void CBC128Crypt();
-    void CBC128Decrypt();
+//    void CBC128Crypt();
+//    void CBC128Decrypt();
 
     void CFB256String();
 

From f94a6339def30f540b44f8214380f4c4280d98a3 Mon Sep 17 00:00:00 2001
From: Matteo Brichese <mbrichese@ics.com>
Date: Wed, 28 Mar 2018 17:47:20 -0700
Subject: [PATCH 2/3] padding tests

---
 qaesencryption.cpp    |  3 +-
 qaesencryption.h      |  4 +--
 unit_test/aestest.cpp | 76 +++++++++++++++++++++----------------------
 unit_test/aestest.h   | 14 ++++----
 4 files changed, 48 insertions(+), 49 deletions(-)

diff --git a/qaesencryption.cpp b/qaesencryption.cpp
index e4d2e7f..94038bf 100644
--- a/qaesencryption.cpp
+++ b/qaesencryption.cpp
@@ -85,8 +85,7 @@ QByteArray QAESEncryption::getPadding(int currSize, int alignment)
 {
     QByteArray ret(0);
     int size = (alignment - currSize % alignment) % alignment;
-//    if (size == 0)
-//        size = alignment;
+    if (size == 0) return ret;
     switch(m_padding)
     {
     case PADDING::ZERO:
diff --git a/qaesencryption.h b/qaesencryption.h
index ae42a2f..0db119a 100644
--- a/qaesencryption.h
+++ b/qaesencryption.h
@@ -29,9 +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::ZERO);
+    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);
diff --git a/unit_test/aestest.cpp b/unit_test/aestest.cpp
index a99e9e7..0c055a0 100644
--- a/unit_test/aestest.cpp
+++ b/unit_test/aestest.cpp
@@ -62,43 +62,43 @@ void AesTest::ECB128Crypt()
     QCOMPARE(encryption.encode(in, key16), outECB128);
 }
 
-//void AesTest::ECB128Decrypt()
-//{
-//    QByteArray hexText, outputHex;
-//    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
+void AesTest::ECB128Decrypt()
+{
+    QByteArray hexText, outputHex;
+    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
 
-//    QCOMPARE(QAESEncryption::RemovePadding(encryption.decode(outECB128, key16)), in);
-//}
+    QCOMPARE(encryption.decode(outECB128, key16), in);
+}
 
-//void AesTest::ECB192Crypt()
-//{
-//    QByteArray outputHex;
-//    QAESEncryption encryption(QAESEncryption::AES_192, QAESEncryption::ECB);
+void AesTest::ECB192Crypt()
+{
+    QByteArray outputHex;
+    QAESEncryption encryption(QAESEncryption::AES_192, QAESEncryption::ECB);
 
-//    QCOMPARE(QAESEncryption::RemovePadding(encryption.encode(in, key24)), outECB192);
-//}
+    QCOMPARE(encryption.encode(in, key24), outECB192);
+}
 
-//void AesTest::ECB192Decrypt()
-//{
-//    QByteArray hexText;
-//    QAESEncryption encryption(QAESEncryption::AES_192, QAESEncryption::ECB);
+void AesTest::ECB192Decrypt()
+{
+    QByteArray hexText;
+    QAESEncryption encryption(QAESEncryption::AES_192, QAESEncryption::ECB);
 
-//    QCOMPARE(QAESEncryption::RemovePadding(encryption.decode(outECB192, key24)), in);
-//}
+    QCOMPARE(encryption.decode(outECB192, key24), in);
+}
 
-//void AesTest::ECB256Crypt()
-//{
-//    QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
+void AesTest::ECB256Crypt()
+{
+    QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
 
-//    QCOMPARE(QAESEncryption::RemovePadding(encryption.encode(in, key32)), outECB256);
-//}
+    QCOMPARE(encryption.encode(in, key32), outECB256);
+}
 
-//void AesTest::ECB256Decrypt()
-//{
-//    QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
+void AesTest::ECB256Decrypt()
+{
+    QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
 
-//    QCOMPARE(QAESEncryption::RemovePadding(encryption.decode(outECB256, key32)), in);
-//}
+    QCOMPARE(encryption.decode(outECB256, key32), in);
+}
 
 void AesTest::ECB256String()
 {
@@ -120,19 +120,19 @@ void AesTest::ECB256String()
 
 ////==================CBC TESTING=========================
 
-//void AesTest::CBC128Crypt()
-//{
-//    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::CBC);
+void AesTest::CBC128Crypt()
+{
+    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::CBC);
 
-//    QCOMPARE(QAESEncryption::RemovePadding(encryption.encode(inCBC128, key16, iv)), outCBC128);
-//}
+    QCOMPARE(encryption.encode(inCBC128, key16, iv), outCBC128);
+}
 
-//void AesTest::CBC128Decrypt()
-//{
-//    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::CBC);
+void AesTest::CBC128Decrypt()
+{
+    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::CBC);
 
-//    QCOMPARE(QString(QAESEncryption::RemovePadding(encryption.decode(outCBC128, key16, iv))), inCBC128);
-//}
+    QCOMPARE(encryption.decode(outCBC128, key16, iv), inCBC128);
+}
 
 //=================== CFB TESTING ============================
 
diff --git a/unit_test/aestest.h b/unit_test/aestest.h
index bfc4efd..2bc2c00 100644
--- a/unit_test/aestest.h
+++ b/unit_test/aestest.h
@@ -12,18 +12,18 @@ private slots:
     void initTestCase();
 
     void ECB128Crypt();
-//    void ECB128Decrypt();
+    void ECB128Decrypt();
 
-//    void ECB192Crypt();
-//    void ECB192Decrypt();
+    void ECB192Crypt();
+    void ECB192Decrypt();
 
-//    void ECB256Crypt();
-//    void ECB256Decrypt();
+    void ECB256Crypt();
+    void ECB256Decrypt();
 
     void ECB256String();
 
-//    void CBC128Crypt();
-//    void CBC128Decrypt();
+    void CBC128Crypt();
+    void CBC128Decrypt();
 
     void CFB256String();
 

From 56c2554df57056942296803f8dd349f22ceb663c Mon Sep 17 00:00:00 2001
From: Matteo Brichese <mbrichese@ics.com>
Date: Wed, 28 Mar 2018 17:50:41 -0700
Subject: [PATCH 3/3] added padding stuff

---
 README.md | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index f1ffd57..c6191fd 100644
--- a/README.md
+++ b/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