diff --git a/qaesencryption.cpp b/qaesencryption.cpp index a21e087..ac240fc 100644 --- a/qaesencryption.cpp +++ b/qaesencryption.cpp @@ -325,7 +325,8 @@ QByteArray QAESEncryption::byteXor(const QByteArray &a, const QByteArray &b) QByteArray::const_iterator it_b = b.begin(); QByteArray ret; - for(int i = 0; i < m_blocklen; i++) + //for(int i = 0; i < m_blocklen; i++) + for(int i = 0; i < std::min(a.size(), b.size()); i++) ret.insert(i,it_a[i] ^ it_b[i]); return ret; @@ -397,48 +398,44 @@ QByteArray QAESEncryption::encode(const QByteArray &rawText, const QByteArray &k QByteArray ret; QByteArray expandedKey = expandKey(key); QByteArray alignedText(rawText); - QByteArray ivTemp(iv); - QByteArray ofbTemp; //Fill array with padding alignedText.append(getPadding(rawText.size(), m_blocklen)); - //Preparation for CFB - if (m_mode == CFB || m_mode == OFB) - ret.append(byteXor(alignedText.mid(0, m_blocklen), cipher(expandedKey, iv))); - if (m_mode == OFB) - ofbTemp.append(cipher(expandedKey, iv)); - - //Looping thru all blocks - for(int i=0; i < alignedText.size(); i+= m_blocklen){ - switch(m_mode) - { - case ECB: + switch(m_mode) + { + case ECB: + for(int i=0; i < alignedText.size(); i+= m_blocklen) ret.append(cipher(expandedKey, alignedText.mid(i, m_blocklen))); - break; - case CBC: - alignedText.replace(i, m_blocklen, byteXor(alignedText.mid(i, m_blocklen),ivTemp)); - ret.append(cipher(expandedKey, alignedText.mid(i, m_blocklen))); - ivTemp = ret.mid(i, m_blocklen); - break; - case CFB: - if (i+m_blocklen < alignedText.size()) - ret.append(byteXor(alignedText.mid(i+m_blocklen, m_blocklen), - cipher(expandedKey, ret.mid(i, m_blocklen)))); - break; - case OFB: - if (i+m_blocklen < alignedText.size()) - { - ret.append(byteXor(alignedText.mid(i+m_blocklen, m_blocklen), - cipher(expandedKey, ofbTemp.mid(i-m_blocklen, m_blocklen)))); - ofbTemp.append(cipher(expandedKey, ofbTemp.mid(i-m_blocklen, m_blocklen))); + break; + case CBC: { + QByteArray ivTemp(iv); + for(int i=0; i < alignedText.size(); i+= m_blocklen) { + alignedText.replace(i, m_blocklen, byteXor(alignedText.mid(i, m_blocklen),ivTemp)); + ret.append(cipher(expandedKey, alignedText.mid(i, m_blocklen))); + ivTemp = ret.mid(i, m_blocklen); } - - break; - default: - //do nothing - break; } + break; + case CFB: { + ret.append(byteXor(alignedText.left(m_blocklen), cipher(expandedKey, iv))); + for(int i=0; i < alignedText.size(); i+= m_blocklen) { + if (i+m_blocklen < alignedText.size()) + ret.append(byteXor(alignedText.mid(i+m_blocklen, m_blocklen), + cipher(expandedKey, ret.mid(i, m_blocklen)))); + } + } + break; + case OFB: { + QByteArray ofbTemp; + ofbTemp.append(cipher(expandedKey, iv)); + for (int i=m_blocklen; i < alignedText.size(); i += m_blocklen){ + ofbTemp.append(cipher(expandedKey, ofbTemp.right(m_blocklen))); + } + ret.append(byteXor(alignedText, ofbTemp)); + } + break; + default: break; } return ret; } @@ -450,33 +447,44 @@ QByteArray QAESEncryption::decode(const QByteArray &rawText, const QByteArray &k QByteArray ret; QByteArray expandedKey = expandKey(key); - QByteArray ivTemp(iv); - //Preparation for CFB - if (m_mode == CFB) - ret.append(byteXor(rawText.mid(0, m_blocklen), cipher(expandedKey, iv))); - - for(int i=0; i < rawText.size(); i+= m_blocklen){ - switch(m_mode) - { - case ECB: + switch(m_mode) + { + case ECB: + for(int i=0; i < rawText.size(); i+= m_blocklen) ret.append(invCipher(expandedKey, rawText.mid(i, m_blocklen))); - break; - case CBC: - ret.append(invCipher(expandedKey, rawText.mid(i, m_blocklen))); - ret.replace(i, m_blocklen, byteXor(ret.mid(i, m_blocklen),ivTemp)); - ivTemp = rawText.mid(i, m_blocklen); - break; - case CFB: - if (i+m_blocklen < rawText.size()){ - ret.append(byteXor(rawText.mid(i+m_blocklen, m_blocklen), - cipher(expandedKey, rawText.mid(i, m_blocklen)))); + break; + case CBC: { + QByteArray ivTemp(iv); + for(int i=0; i < rawText.size(); i+= m_blocklen){ + ret.append(invCipher(expandedKey, rawText.mid(i, m_blocklen))); + ret.replace(i, m_blocklen, byteXor(ret.mid(i, m_blocklen),ivTemp)); + ivTemp = rawText.mid(i, m_blocklen); } - break; - default: - //do nothing - break; } + break; + case CFB: { + ret.append(byteXor(rawText.mid(0, m_blocklen), cipher(expandedKey, iv))); + for(int i=0; i < rawText.size(); i+= m_blocklen){ + if (i+m_blocklen < rawText.size()) { + ret.append(byteXor(rawText.mid(i+m_blocklen, m_blocklen), + cipher(expandedKey, rawText.mid(i, m_blocklen)))); + } + } + } + break; + case OFB: { + QByteArray ofbTemp; + ofbTemp.append(cipher(expandedKey, iv)); + for (int i=m_blocklen; i < rawText.size(); i += m_blocklen){ + ofbTemp.append(cipher(expandedKey, ofbTemp.right(m_blocklen))); + } + ret.append(byteXor(rawText, ofbTemp)); + } + break; + default: + //do nothing + break; } return ret; } diff --git a/unit_test/aestest.cpp b/unit_test/aestest.cpp index 0119b1a..25ab728 100644 --- a/unit_test/aestest.cpp +++ b/unit_test/aestest.cpp @@ -1,5 +1,6 @@ #include "aestest.h" +#include #include #include #include @@ -139,7 +140,7 @@ void AesTest::CBC128Decrypt() void AesTest::CFB256String() { - QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::CFB); + QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::CFB, QAESEncryption::PKCS7); 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. " @@ -171,3 +172,19 @@ void AesTest::CFB256LongText() QByteArray decodedText = encryption.removePadding(encryption.decode(encodeText, hashKey, iv)); QCOMPARE(decodedText, input); } + +void AesTest::OFB256String() +{ + QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::OFB, QAESEncryption::PKCS7); + + 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, iv); + + QByteArray decodedText = encryption.removePadding(encryption.decode(encodeText, hashKey, iv)); + QCOMPARE(inputStr, decodedText); +} diff --git a/unit_test/aestest.h b/unit_test/aestest.h index bd6c03e..eeaa5fc 100644 --- a/unit_test/aestest.h +++ b/unit_test/aestest.h @@ -29,6 +29,8 @@ private slots: void CFB256LongText(); + void OFB256String(); + void cleanupTestCase(){} private: