9 return QAESEncryption(level, mode, padding).encode(rawText, key, iv);
15 return QAESEncryption(level, mode, padding).decode(rawText, key, iv);
25 QByteArray ret(rawText);
30 while (ret.at(ret.length()-1) == 0x00)
31 ret.remove(ret.length()-1, 1);
34 ret.remove(ret.length() - ret.at(ret.length()-1), ret.at(ret.length()-1));
37 ret.truncate(ret.lastIndexOf(0x80));
54 return ((x<<1) ^ (((x>>7) & 1) * 0x1b));
58 return (((y & 1) * x) ^ ((y>>1 & 1) *
xTime(x)) ^ ((y>>2 & 1) *
xTime(
xTime(x))) ^ ((y>>3 & 1)
69 : m_nb(4), m_blocklen(16), m_level(level), m_mode(mode), m_padding(padding)
78 m_keyLen = aes.keylen;
80 m_expandedKey = aes.expandedKey;
86 m_keyLen = aes.keylen;
88 m_expandedKey = aes.expandedKey;
94 m_keyLen = aes.keylen;
96 m_expandedKey = aes.expandedKey;
102 m_keyLen = aes.keylen;
104 m_expandedKey = aes.expandedKey;
110 QByteArray QAESEncryption::getPadding(
int currSize,
int alignment)
112 int size = (alignment - currSize % alignment) % alignment;
113 if (size == 0)
return QByteArray();
117 return QByteArray(size, 0x00);
120 return QByteArray(size,size);
123 return QByteArray (size-1, 0x00).prepend(0x80);
126 return QByteArray(size, 0x00);
129 return QByteArray(size, 0x00);
136 QByteArray roundKey(key);
143 for(i = m_nk; i < m_nb * (m_nr + 1); i++)
145 tempa[0] = (quint8) roundKey.at((i-1) * 4 + 0);
146 tempa[1] = (quint8) roundKey.at((i-1) * 4 + 1);
147 tempa[2] = (quint8) roundKey.at((i-1) * 4 + 2);
148 tempa[3] = (quint8) roundKey.at((i-1) * 4 + 3);
163 tempa[0] = getSBoxValue(tempa[0]);
164 tempa[1] = getSBoxValue(tempa[1]);
165 tempa[2] = getSBoxValue(tempa[2]);
166 tempa[3] = getSBoxValue(tempa[3]);
168 tempa[0] = tempa[0] ^ Rcon[i/m_nk];
170 if (m_level ==
AES_256 && i % m_nk == 4)
173 tempa[0] = getSBoxValue(tempa[0]);
174 tempa[1] = getSBoxValue(tempa[1]);
175 tempa[2] = getSBoxValue(tempa[2]);
176 tempa[3] = getSBoxValue(tempa[3]);
178 roundKey.insert(i * 4 + 0, (quint8) roundKey.at((i - m_nk) * 4 + 0) ^ tempa[0]);
179 roundKey.insert(i * 4 + 1, (quint8) roundKey.at((i - m_nk) * 4 + 1) ^ tempa[1]);
180 roundKey.insert(i * 4 + 2, (quint8) roundKey.at((i - m_nk) * 4 + 2) ^ tempa[2]);
181 roundKey.insert(i * 4 + 3, (quint8) roundKey.at((i - m_nk) * 4 + 3) ^ tempa[3]);
188 void QAESEncryption::addRoundKey(
const quint8 round,
const QByteArray expKey)
190 QByteArray::iterator it = m_state->begin();
191 for(
int i=0; i < 16; ++i)
192 it[i] = (quint8) it[i] ^ (quint8) expKey.at(round * m_nb * 4 + (i/4) * m_nb + (i%4));
197 void QAESEncryption::subBytes()
199 QByteArray::iterator it = m_state->begin();
200 for(
int i = 0; i < 16; i++)
201 it[i] = getSBoxValue((quint8) it[i]);
207 void QAESEncryption::shiftRows()
209 QByteArray::iterator it = m_state->begin();
214 temp = (quint8)it[1];
215 it[1] = (quint8)it[5];
216 it[5] = (quint8)it[9];
217 it[9] = (quint8)it[13];
218 it[13] = (quint8)temp;
221 temp = (quint8)it[2];
222 it[2] = (quint8)it[10];
223 it[10] = (quint8)temp;
224 temp = (quint8)it[6];
225 it[6] = (quint8)it[14];
226 it[14] = (quint8)temp;
229 temp = (quint8)it[3];
230 it[3] = (quint8)it[15];
231 it[15] = (quint8)it[11];
232 it[11] = (quint8)it[7];
233 it[7] = (quint8)temp;
238 void QAESEncryption::mixColumns()
240 QByteArray::iterator it = m_state->begin();
243 for(
int i = 0; i < 16; i += 4){
245 tmp = (quint8)it[i] ^ (quint8)it[i+1] ^ (quint8)it[i+2] ^ (quint8)it[i+3] ;
247 tm =
xTime( (quint8)it[i] ^ (quint8)it[i+1] );
248 it[i] = (quint8)it[i] ^ (quint8)tm ^ (quint8)tmp;
250 tm =
xTime( (quint8)it[i+1] ^ (quint8)it[i+2]);
251 it[i+1] = (quint8)it[i+1] ^ (quint8)tm ^ (quint8)tmp;
253 tm =
xTime( (quint8)it[i+2] ^ (quint8)it[i+3]);
254 it[i+2] =(quint8)it[i+2] ^ (quint8)tm ^ (quint8)tmp;
256 tm =
xTime((quint8)it[i+3] ^ (quint8)t);
257 it[i+3] =(quint8)it[i+3] ^ (quint8)tm ^ (quint8)tmp;
264 void QAESEncryption::invMixColumns()
266 QByteArray::iterator it = m_state->begin();
268 for(
int i = 0; i < 16; i+=4){
270 b = (quint8) it[i+1];
271 c = (quint8) it[i+2];
272 d = (quint8) it[i+3];
283 void QAESEncryption::invSubBytes()
285 QByteArray::iterator it = m_state->begin();
286 for(
int i = 0; i < 16; ++i)
287 it[i] = getSBoxInvert((quint8) it[i]);
290 void QAESEncryption::invShiftRows()
292 QByteArray::iterator it = m_state->begin();
298 temp = (quint8)it[13];
299 it[13] = (quint8)it[9];
300 it[9] = (quint8)it[5];
301 it[5] = (quint8)it[1];
302 it[1] = (quint8)temp;
305 temp = (quint8)it[10];
306 it[10] = (quint8)it[2];
307 it[2] = (quint8)temp;
308 temp = (quint8)it[14];
309 it[14] = (quint8)it[6];
310 it[6] = (quint8)temp;
313 temp = (quint8)it[15];
314 it[15] = (quint8)it[3];
315 it[3] = (quint8)it[7];
316 it[7] = (quint8)it[11];
317 it[11] = (quint8)temp;
320 QByteArray QAESEncryption::byteXor(
const QByteArray &a,
const QByteArray &b)
322 QByteArray::const_iterator it_a = a.begin();
323 QByteArray::const_iterator it_b = b.begin();
327 for(
int i = 0; i < std::min(a.size(), b.size()); i++)
328 ret.insert(i,it_a[i] ^ it_b[i]);
334 QByteArray QAESEncryption::cipher(
const QByteArray &expKey,
const QByteArray &in)
338 QByteArray output(in);
342 addRoundKey(0, expKey);
347 for(quint8 round = 1; round < m_nr; ++round){
351 addRoundKey(round, expKey);
358 addRoundKey(m_nr, expKey);
363 QByteArray QAESEncryption::invCipher(
const QByteArray &expKey,
const QByteArray &in)
366 QByteArray output(in);
370 addRoundKey(m_nr, expKey);
375 for(quint8 round=m_nr-1; round>0 ; round--){
378 addRoundKey(round, expKey);
386 addRoundKey(0, expKey);
393 if (m_mode >=
CBC && (iv.isNull() || iv.size() != m_blocklen))
398 QByteArray alignedText(rawText);
401 alignedText.append(getPadding(rawText.size(), m_blocklen));
406 for(
int i=0; i < alignedText.size(); i+= m_blocklen)
407 ret.append(cipher(expandedKey, alignedText.mid(i, m_blocklen)));
410 QByteArray ivTemp(iv);
411 for(
int i=0; i < alignedText.size(); i+= m_blocklen) {
412 alignedText.replace(i, m_blocklen, byteXor(alignedText.mid(i, m_blocklen),ivTemp));
413 ret.append(cipher(expandedKey, alignedText.mid(i, m_blocklen)));
414 ivTemp = ret.mid(i, m_blocklen);
419 ret.append(byteXor(alignedText.left(m_blocklen), cipher(expandedKey, iv)));
420 for(
int i=0; i < alignedText.size(); i+= m_blocklen) {
421 if (i+m_blocklen < alignedText.size())
422 ret.append(byteXor(alignedText.mid(i+m_blocklen, m_blocklen),
423 cipher(expandedKey, ret.mid(i, m_blocklen))));
429 ofbTemp.append(cipher(expandedKey, iv));
430 for (
int i=m_blocklen; i < alignedText.size(); i += m_blocklen){
431 ofbTemp.append(cipher(expandedKey, ofbTemp.right(m_blocklen)));
433 ret.append(byteXor(alignedText, ofbTemp));
443 if (m_mode >=
CBC && (iv.isNull() || iv.size() != m_blocklen))
452 for(
int i=0; i < rawText.size(); i+= m_blocklen)
453 ret.append(invCipher(expandedKey, rawText.mid(i, m_blocklen)));
456 QByteArray ivTemp(iv);
457 for(
int i=0; i < rawText.size(); i+= m_blocklen){
458 ret.append(invCipher(expandedKey, rawText.mid(i, m_blocklen)));
459 ret.replace(i, m_blocklen, byteXor(ret.mid(i, m_blocklen),ivTemp));
460 ivTemp = rawText.mid(i, m_blocklen);
465 ret.append(byteXor(rawText.mid(0, m_blocklen), cipher(expandedKey, iv)));
466 for(
int i=0; i < rawText.size(); i+= m_blocklen){
467 if (i+m_blocklen < rawText.size()) {
468 ret.append(byteXor(rawText.mid(i+m_blocklen, m_blocklen),
469 cipher(expandedKey, rawText.mid(i, m_blocklen))));
476 ofbTemp.append(cipher(expandedKey, iv));
477 for (
int i=m_blocklen; i < rawText.size(); i += m_blocklen){
478 ofbTemp.append(cipher(expandedKey, ofbTemp.right(m_blocklen)));
480 ret.append(byteXor(rawText, ofbTemp));
492 QByteArray ret(rawText);
497 while (ret.at(ret.length()-1) == 0x00)
498 ret.remove(ret.length()-1, 1);
501 ret.remove(ret.length() - ret.at(ret.length()-1), ret.at(ret.length()-1));
504 ret.truncate(ret.lastIndexOf(0x80));
Aes
The Aes enum AES Level AES Levels The class supports all AES key lenghts.
static QByteArray Crypt(QAESEncryption::Aes level, QAESEncryption::Mode mode, const QByteArray &rawText, const QByteArray &key, const QByteArray &iv=NULL, QAESEncryption::Padding padding=QAESEncryption::ISO)
Crypt Static encode function.
Mode
The Mode enum AES Mode The class supports the following operating modes ECB CBC CFB OFB...
QByteArray decode(const QByteArray &rawText, const QByteArray &key, const QByteArray &iv=NULL)
decode Decodes data with AES
static QByteArray Decrypt(QAESEncryption::Aes level, QAESEncryption::Mode mode, const QByteArray &rawText, const QByteArray &key, const QByteArray &iv=NULL, QAESEncryption::Padding padding=QAESEncryption::ISO)
Decrypt Static decode function.
QByteArray removePadding(const QByteArray &rawText)
RemovePadding Removes padding.
quint8 multiply(quint8 x, quint8 y)
QByteArray encode(const QByteArray &rawText, const QByteArray &key, const QByteArray &iv=NULL)
encode Encodes data with AES
QAESEncryption(QAESEncryption::Aes level, QAESEncryption::Mode mode, QAESEncryption::Padding padding=QAESEncryption::ISO)
Padding
The Padding enum Padding By default the padding method is ISO, however, the class supports: ...
static QByteArray ExpandKey(QAESEncryption::Aes level, QAESEncryption::Mode mode, const QByteArray &key)
ExpandKey Expands the key.
QByteArray expandKey(const QByteArray &key)
ExpandKey Expands the key.
static QByteArray RemovePadding(const QByteArray &rawText, QAESEncryption::Padding padding)
RemovePadding Removes padding.