mirror of
https://github.com/QuasarApp/Qt-AES.git
synced 2025-04-26 21:54:32 +00:00
adding OFB with basic test
This commit is contained in:
parent
4ce12493f6
commit
278565d661
@ -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;
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include "aestest.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QByteArray>
|
||||
#include <QCryptographicHash>
|
||||
#include <QFile>
|
||||
@ -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);
|
||||
}
|
||||
|
@ -29,6 +29,8 @@ private slots:
|
||||
|
||||
void CFB256LongText();
|
||||
|
||||
void OFB256String();
|
||||
|
||||
void cleanupTestCase(){}
|
||||
|
||||
private:
|
||||
|
Loading…
x
Reference in New Issue
Block a user