mirror of
https://github.com/QuasarApp/Qt-AES.git
synced 2025-05-05 09:49:34 +00:00
cleaning code
This commit is contained in:
parent
a81328ae54
commit
899c759ad8
62
main.cpp
62
main.cpp
@ -5,17 +5,6 @@
|
||||
#include <QCryptographicHash>
|
||||
#include "qaesencryption.h"
|
||||
|
||||
//const uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
|
||||
|
||||
/*inline int getAlignedSize(int currSize, int alignment) {
|
||||
int padding = (alignment - currSize % alignment) % alignment;
|
||||
return currSize + padding;
|
||||
}*/
|
||||
|
||||
/*QString encodeText(const QString rawText, const QString key);
|
||||
QString decodeText(const QString hexEncodedText, const QString key);
|
||||
*/
|
||||
|
||||
QString print(QByteArray in)
|
||||
{
|
||||
QString ret="";
|
||||
@ -40,6 +29,7 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
|
||||
|
||||
QByteArray encodedString = encryption.encode(plainText, keyText);
|
||||
QByteArray decodedString = encryption.decode(encodedString, keyText);
|
||||
|
||||
@ -52,55 +42,5 @@ int main(int argc, char *argv[])
|
||||
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
QString encodeText(const QString rawText, const QString key) {
|
||||
QCryptographicHash hash(QCryptographicHash::Md5);
|
||||
hash.addData(key.toUtf8());
|
||||
QByteArray keyData = hash.result();
|
||||
|
||||
const ushort *rawData = rawText.utf16();
|
||||
void *rawDataVoid = (void*)rawData;
|
||||
const char *rawDataChar = static_cast<const char*>(rawDataVoid);
|
||||
QByteArray inputData;
|
||||
|
||||
// ushort is 2*uint8_t + 1 byte for '\0'
|
||||
inputData.append(rawDataChar, rawText.size() * 2 + 1);
|
||||
|
||||
const int length = inputData.size();
|
||||
int encryptionLength = getAlignedSize(length, 16);
|
||||
|
||||
QByteArray encodingBuffer(encryptionLength, 0);
|
||||
inputData.resize(encryptionLength);
|
||||
|
||||
AES_CBC_encrypt_buffer((uint8_t*)encodingBuffer.data(), (uint8_t*)inputData.data(),
|
||||
encryptionLength, (const uint8_t*)keyData.data(), iv);
|
||||
|
||||
QByteArray data(encodingBuffer.data(), encryptionLength);
|
||||
QString hex = QString::fromLatin1(data.toHex());
|
||||
return hex;
|
||||
}
|
||||
|
||||
QString decodeText(const QString hexEncodedText, const QString key) {
|
||||
QCryptographicHash hash(QCryptographicHash::Md5);
|
||||
hash.addData(key.toUtf8());
|
||||
QByteArray keyData = hash.result();
|
||||
|
||||
const int length = hexEncodedText.size();
|
||||
int encryptionLength = getAlignedSize(length, 16);
|
||||
|
||||
QByteArray encodingBuffer(encryptionLength, 0);
|
||||
|
||||
QByteArray encodedText = QByteArray::fromHex(hexEncodedText.toLatin1());
|
||||
encodedText.resize(encryptionLength);
|
||||
|
||||
AES_CBC_decrypt_buffer((uint8_t*)encodingBuffer.data(), (uint8_t*)encodedText.data(),
|
||||
encryptionLength, (const uint8_t*)keyData.data(), iv);
|
||||
|
||||
void *data = encodingBuffer.data();
|
||||
const ushort *decodedData = static_cast<const ushort*>(data);
|
||||
QString result = QString::fromUtf16(decodedData);
|
||||
|
||||
return result;
|
||||
}*/
|
||||
|
||||
|
||||
|
@ -326,10 +326,8 @@ QByteArray QAESEncryption::encode(const QByteArray rawText, const QByteArray key
|
||||
if (m_mode == CBC && iv.isNull())
|
||||
return QByteArray();
|
||||
|
||||
//qDebug() << "key" << print(key);
|
||||
QByteArray expandedKey = expandKey(key);
|
||||
|
||||
// The next function call encrypts the PlainText with the Key using AES algorithm.
|
||||
return cipher(expandedKey, rawText);
|
||||
}
|
||||
|
||||
|
0
sample.txt
Normal file
0
sample.txt
Normal file
Loading…
x
Reference in New Issue
Block a user