The OpenSSL library is better for security and performance, so we implemented a Qt wrapper library to make it easier to use. See the EasySSL library.
Fast encryption library supporting RSA and AES algorithms.
The Qt-Secret library supports the following algorithms:
The current implementation supports the following key sizes:
AES implementation was borrowed from bricke, because it fulfills the goals of this library.
Individual thanks bricke for implementing the AES encryption class.
The class supports all AES key lengths
The class supports the following operating modes
By default the padding method is ISO
, however, the class supports:
DEFINE+=WITHOUT_GUI fot build without gui example, if you want build gui example remove this line. For build the gui example you need to install qml controls 2 in you os. Try sudo apt install qml-module-qtquick-controls2 qtdeclarative5-dev qtdeclarative5-qtquick2-plugin
Add to the list of libraries for the Qt-Secret assembly. For example, you can create Main.Pro in which connect Qt-Secret and your project.pro files as subprojects.
Main.pro:
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += \ Qt-Secret \ MyProject
* Include in your MyProject.pro file the pri file of Qt-Secret library
``` qmake
include($$PWD/../Qt-Secret/src/Qt-Secret.pri)
add_subdirectory(Qt-Secret)
target_link_libraries(MyBinary PUBLIC Qt-Secret)
By Default Qt-Secret makes as a static library. If you want to create a shared library just add the BUILD_SHARED_LIBS into your main CMakeLists.txt file. Example :
set(BUILD_SHARED_LIBS ON)
add_subdirectory(Qt-Secret)
target_link_libraries(MyBinary PUBLIC Qt-Secret)
#include <qrsaencryption.h>
#include <QDebug>
bool testEncryptAndDecryptExample() {
QByteArray pub, priv;
QRSAEncryption e(QRSAEncryption::Rsa::RSA_2048);
e.generatePairKey(pub, priv); // or other rsa size
QByteArray msg = "test message";
auto encryptMessage = e.encode(msg, pub);
if (encryptMessage == msg)
return false;
auto decodeMessage = e.decode(encryptMessage, priv);
return decodeMessage == msg;
}
int main() {
if (testEncryptAndDecryptExample()) {
qInfo() << "Success!";
}
}
#include <qrsaencryption.h>
#include <QDebug>
bool testExample() {
QByteArray pub, priv;
QRSAEncryption e(QRSAEncryption::Rsa::RSA_2048);
e.generatePairKey(pub, priv); // or other rsa size
QByteArray msg = "test message";
auto signedMessage = e.signMessage(msg, priv);
if (e.checkSignMessage(signedMessage, pub)) {
qInfo() <<" message signed success";
return true;
}
return false;
}
int main() {
if (testExample()) {
qInfo() <<"success!";
}
}
Sample code using a 128bit key in ECB mode
#include "qaesencryption.h"
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
QByteArray encodedText = encryption.encode(plainText, key);
QByteArray decodedText = encryption.decode(encodedText, key);
Example for 256bit CBC using QString
#include <QCryptographicHash>
#include "qaesencryption.h"
QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::CBC);
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("your-string-key");
QString iv("your-IV-vector");
QByteArray hashKey = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Sha256);
QByteArray hashIV = QCryptographicHash::hash(iv.toLocal8Bit(), QCryptographicHash::Md5);
QByteArray encodeText = encryption.encode(inputStr.toLocal8Bit(), hashKey, hashIV);
QByteArray decodeText = encryption.decode(encodeText, hashKey, hashIV);
QString decodedString = QString(encryption.removePadding(decodeText));
//decodedString == inputStr !!!
Static invocation without creating instances, 256 bit key, ECB mode, starting from QString text/key
#include <QCryptographicHash>
#include "qaesencryption.h"
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("your-string-key");
QString iv("your-IV-vector");
QByteArray hashKey = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Sha256);
QByteArray hashIV = QCryptographicHash::hash(iv.toLocal8Bit(), QCryptographicHash::Md5);
//Static invocation
QByteArray encrypted = QAESEncryption::Crypt(QAESEncryption::AES_256, QAESEncryption::CBC,
inputStr.toLocal8Bit(), hashKey, hashIV);
//...
// Removal of Padding via Static function
QString decodedString = QString(QAESEncryption::RemovePadding(decodeText));