Skip to content

Commit e05e387

Browse files
author
Matteo Brichese
committed
adding CFB-mode
1 parent f651250 commit e05e387

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

qaesencryption.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,8 @@ QByteArray QAESEncryption::encode(const QByteArray rawText, const QByteArray key
356356
alignedText.append(getPadding(rawText.size(), m_blocklen), 0); //filling the array with zeros
357357

358358
//Preparation for CFB
359-
if (m_mode == CFB){
360-
ivTemp = cipher(expandedKey, ivTemp);
361-
ret.append(byteXor(alignedText.mid(0, m_blocklen), ivTemp));
362-
}
359+
if (m_mode == CFB)
360+
ret.append(byteXor(alignedText.mid(0, m_blocklen), cipher(expandedKey, iv)));
363361

364362
//Looping thru all blocks
365363
for(int i=0; i < alignedText.size(); i+= m_blocklen){
@@ -374,8 +372,9 @@ QByteArray QAESEncryption::encode(const QByteArray rawText, const QByteArray key
374372
ivTemp = ret.mid(i, m_blocklen);
375373
break;
376374
case CFB:
377-
ivTemp = ret.mid(i, m_blocklen);
378-
ret.append(byteXor(alignedText.mid(i, m_blocklen), cipher(expandedKey, ivTemp)));
375+
if (i+m_blocklen < alignedText.size())
376+
ret.append(byteXor(alignedText.mid(i+m_blocklen, m_blocklen),
377+
cipher(expandedKey, ret.mid(i, m_blocklen))));
379378
break;
380379
default:
381380
//do nothing
@@ -392,29 +391,28 @@ QByteArray QAESEncryption::decode(const QByteArray rawText, const QByteArray key
392391

393392
QByteArray ret;
394393
QByteArray expandedKey = expandKey(key);
395-
QByteArray alignedText(rawText);
396394
QByteArray ivTemp(iv);
397395

398396
//Preparation for CFB
399-
if (m_mode == CFB){
400-
ivTemp = cipher(expandedKey, ivTemp);
401-
ret.append(byteXor(alignedText.mid(0, m_blocklen), ivTemp));
402-
}
397+
if (m_mode == CFB)
398+
ret.append(byteXor(rawText.mid(0, m_blocklen), cipher(expandedKey, iv)));
403399

404-
for(int i=0; i < alignedText.size(); i+= m_blocklen){
400+
for(int i=0; i < rawText.size(); i+= m_blocklen){
405401
switch(m_mode)
406402
{
407403
case ECB:
408-
ret.append(invCipher(expandedKey, alignedText.mid(i, m_blocklen)));
404+
ret.append(invCipher(expandedKey, rawText.mid(i, m_blocklen)));
409405
break;
410406
case CBC:
411-
ret.append(invCipher(expandedKey, alignedText.mid(i, m_blocklen)));
407+
ret.append(invCipher(expandedKey, rawText.mid(i, m_blocklen)));
412408
ret.replace(i, m_blocklen, byteXor(ret.mid(i, m_blocklen),ivTemp));
413-
ivTemp = alignedText.mid(i, m_blocklen);
409+
ivTemp = rawText.mid(i, m_blocklen);
414410
break;
415411
case CFB:
416-
ivTemp = alignedText.mid(i, m_blocklen);
417-
ret.append(byteXor(alignedText.mid(i, m_blocklen), cipher(expandedKey, ivTemp)));
412+
if (i+m_blocklen < rawText.size()){
413+
ret.append(byteXor(rawText.mid(i+m_blocklen, m_blocklen),
414+
cipher(expandedKey, rawText.mid(i, m_blocklen))));
415+
}
418416
break;
419417
default:
420418
//do nothing

unit_test/aestest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <QCryptographicHash>
55
#include "qaesencryption.h"
66

7+
#include <QDebug>
8+
79
void AesTest::initTestCase()
810
{
911
quint8 key_16[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
@@ -133,3 +135,21 @@ void AesTest::CBC128Decrypt()
133135

134136
QCOMPARE(encryption.decode(outCBC128, key16, iv), inCBC128);
135137
}
138+
139+
//=================== CFB TESTING ============================
140+
141+
void AesTest::CFB256String()
142+
{
143+
QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::CFB);
144+
145+
QString inputStr("The Advanced Encryption Standard (AES), also known by its original name Rijndael "
146+
"is a specification for the encryption of electronic data established by the U.S. "
147+
"National Institute of Standards and Technology (NIST) in 2001");
148+
QString key("123456789123");
149+
150+
QByteArray hashKey = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Sha256);
151+
152+
QByteArray encodeText = encryption.encode(inputStr.toLocal8Bit(), hashKey, iv);
153+
154+
QCOMPARE(QString(encryption.decode(encodeText, hashKey, iv)), inputStr);
155+
}

unit_test/aestest.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ private slots:
2525
void CBC128Crypt();
2626
void CBC128Decrypt();
2727

28+
void CFB256String();
29+
2830
void cleanupTestCase(){}
2931

3032
private:

0 commit comments

Comments
 (0)