fixed a example and remove a old static methods

This commit is contained in:
Andrei Yankovich 2019-11-16 10:28:48 +03:00
parent 4df74b4fe4
commit df7de5282e
5 changed files with 99 additions and 102 deletions

View File

@ -5,13 +5,13 @@ SecretWorker::SecretWorker(QObject *parent) : QObject(parent)
void SecretWorker::generateKeys(int rsa) {
QRSAEncryption::generatePairKeyS(pubKey, privKey, QRSAEncryption::Rsa(rsa));
QRSAEncryption::generatePairKey(pubKey, privKey, QRSAEncryption::Rsa(rsa));
emit showKeysOnQml();
}
void SecretWorker::encryptMessage(QString encPubKey, QString inputText) {
message = QString(QRSAEncryption::encodeS(inputText.toUtf8(),
message = QString(QRSAEncryption::encode(inputText.toUtf8(),
QByteArray::fromHex(encPubKey.toUtf8()),
QRSAEncryption::Rsa(encPubKey.length() * 2)).toHex());
emit showEncDecResOnQml();
@ -19,7 +19,7 @@ void SecretWorker::encryptMessage(QString encPubKey, QString inputText) {
void SecretWorker::decryptMessage(QString decPrivKey, QString inputMessage) {
message = QString(QRSAEncryption::decodeS(QByteArray::fromHex(inputMessage.toUtf8()),
message = QString(QRSAEncryption::decode(QByteArray::fromHex(inputMessage.toUtf8()),
QByteArray::fromHex(decPrivKey.toUtf8()),
QRSAEncryption::Rsa(decPrivKey.length() * 2)));
emit showEncDecResOnQml();
@ -27,7 +27,7 @@ void SecretWorker::decryptMessage(QString decPrivKey, QString inputMessage) {
void SecretWorker::signMessage(QString signPrivKey, QString inputMessage) {
message = QString(QRSAEncryption::signMessageS(inputMessage.toUtf8(),
message = QString(QRSAEncryption::signMessage(inputMessage.toUtf8(),
QByteArray::fromHex(signPrivKey.toUtf8()),
QRSAEncryption::Rsa(signPrivKey.length() * 2)));
emit showSignResOnQml();
@ -35,7 +35,7 @@ void SecretWorker::signMessage(QString signPrivKey, QString inputMessage) {
void SecretWorker::checkSign(QString signPubKey, QString inputMessage) {
message = QRSAEncryption::checkSignMessageS(inputMessage.toUtf8(),
message = QRSAEncryption::checkSignMessage(inputMessage.toUtf8(),
QByteArray::fromHex(signPubKey.toUtf8()),
QRSAEncryption::Rsa(signPubKey.length() * 2)) ? "Sign is true" : "Sign is false";
emit showSignResOnQml();

View File

@ -105,13 +105,16 @@ SUBDIRS += \
QByteArray pub, priv;
QRSAEncryption e(QRSAEncryption::Rsa::RSA_2048);
e.generatePairKey(pub, priv);
e.generatePairKey(pub, priv); // or other rsa size
QByteArray msg = "test message";
auto encodeData = e.encode(msg, pub);
auto decodeData = e.decode(encodeData, priv);
auto signedMessage = e.signMessage(msg, priv);
if (e.checkSignMessage(signedMessage, pub)) {
qInfo() <<" message signed success";
}
```
@ -120,18 +123,24 @@ SUBDIRS += \
``` cpp
#include <qrsaencryption.h>
bool testExample() {
QByteArray pub, priv;
QRSAEncryption e;
e.generatePairKey(pub, priv, QRSAEncryption::Rsa::RSA_128); // or other rsa size
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)) {
// message signed success
qInfo() <<" message signed success";
return true;
}
return false;
}
```
## AES

View File

@ -164,39 +164,6 @@ QRSAEncryption::QRSAEncryption(Rsa rsa) {
_rsa = rsa;
}
bool QRSAEncryption::generatePairKeyS(QByteArray &pubKey, QByteArray &privKey, QRSAEncryption::Rsa rsa) {
qWarning() << "method " << Q_FUNC_INFO <<
" will be deleted in newxt version. please use generatePairKey method";
return generatePairKey(pubKey, privKey, rsa);
}
QByteArray QRSAEncryption::encodeS(const QByteArray &rawData, const QByteArray &pubKey, QRSAEncryption::Rsa rsa, QRSAEncryption::BlockSize blockSizeMode) {
qWarning() << "method " << Q_FUNC_INFO <<
" will be deleted in newxt version. please use encode method";
return encode(rawData, pubKey, rsa, blockSizeMode);
}
QByteArray QRSAEncryption::decodeS(const QByteArray &rawData, const QByteArray &privKey, QRSAEncryption::Rsa rsa, QRSAEncryption::BlockSize blockSizeMode) {
qWarning() << "method " << Q_FUNC_INFO <<
" will be deleted in newxt version. please use decode method";
return decode(rawData, privKey, rsa, blockSizeMode);
}
QByteArray QRSAEncryption::signMessageS(QByteArray rawData, const QByteArray &privKey, QRSAEncryption::Rsa rsa) {
qWarning() << "method " << Q_FUNC_INFO <<
" will be deleted in newxt version. please use signMessage method";
return signMessage(rawData, privKey, rsa);
}
bool QRSAEncryption::checkSignMessageS(const QByteArray &rawData, const QByteArray &pubKey, QRSAEncryption::Rsa rsa) {
qWarning() << "method " << Q_FUNC_INFO <<
" will be deleted in newxt version. please use signMessage method";
return checkSignMessage(rawData, pubKey, rsa);
}
unsigned int QRSAEncryption::getKeyBytesSize(QRSAEncryption::Rsa rsa) {
return rsa / 4;

View File

@ -48,19 +48,6 @@ public:
// static methods
// OLDMETHODS DELETE IN next Version
static bool generatePairKeyS(QByteArray &pubKey, QByteArray &privKey,
QRSAEncryption::Rsa rsa = RSA_256);
static QByteArray encodeS(const QByteArray &rawData, const QByteArray &pubKey,
Rsa rsa = RSA_256, BlockSize blockSizeMode = BlockSize::Auto);
static QByteArray decodeS(const QByteArray &rawData, const QByteArray &privKey,
Rsa rsa = RSA_256, BlockSize blockSizeMode = BlockSize::Auto);
static QByteArray signMessageS(QByteArray rawData, const QByteArray &privKey,
Rsa rsa = RSA_256);
static bool checkSignMessageS(const QByteArray &rawData, const QByteArray &pubKey,
Rsa rsa);
// OLDMETHODS END
static bool generatePairKey(QByteArray &pubKey, QByteArray &privKey,
QRSAEncryption::Rsa rsa);
static QByteArray encode(const QByteArray &rawData, const QByteArray &pubKey,

View File

@ -38,6 +38,60 @@ QByteArray randomArray(int length = -1) {
return res;
}
bool checkKeys(const QByteArray& pubKey, const QByteArray& privKey,
QRSAEncryption::Rsa rsa) {
QRSAEncryption e(rsa);
qInfo() << QString("Private key: %0").arg(QString(pubKey.toHex()));
qInfo() << QString("Public key: %0").arg(QString(privKey.toHex()));
if (pubKey.size() != rsa / 4) {
qCritical() << "pubKey size wrong RSA" << rsa;
return false;
}
if (privKey.size() != rsa / 4) {
qCritical() << "privKey size wrong RSA" << rsa;
return false;
}
for (int i = 0; i < testSize[rsa]; i++) {
auto base = randomArray();
auto encodeData = e.encode(base, pubKey);
auto decodeData = e.decode(encodeData, privKey);
if ( base != decodeData) {
qCritical() << "encode/decode data error RSA" << rsa;
return false;
}
encodeData = e.signMessage(base, privKey);
if (!e.checkSignMessage(encodeData, pubKey)) {
qCritical() << "sig message error RSA" << rsa;
return false;
}
encodeData += "work it";
if (e.checkSignMessage(encodeData, pubKey)) {
qCritical() << "sig message error RSA with added value to back" << rsa;
return false;
}
encodeData.push_front("not work");
if (e.checkSignMessage(encodeData, pubKey)) {
qCritical() << "sig message error RSA with added value to front" << rsa;
return false;
}
}
return true;
}
bool testCrypto(QRSAEncryption::Rsa rsa) {
QByteArray pub, priv;
@ -52,58 +106,38 @@ bool testCrypto(QRSAEncryption::Rsa rsa) {
return false;
}
qInfo() << QString("Private key: %0").arg(QString(priv.toHex()));
qInfo() << QString("Public key: %0").arg(QString(pub.toHex()));
if (pub.size() != rsa / 4) {
qCritical() << "pubKey size wrong RSA" << rsa;
if (!checkKeys(pub, priv, rsa)) {
return false;
}
if (priv.size() != rsa / 4) {
qCritical() << "privKey size wrong RSA" << rsa;
return false;
}
for (int i = 0; i < testSize[rsa]; i++) {
auto base = randomArray();
auto encodeData = e.encode(base, pub);
auto decodeData = e.decode(encodeData, priv);
if ( base != decodeData) {
qCritical() << "encode/decode data error RSA" << rsa;
return false;
}
encodeData = e.signMessage(base, priv);
if (!e.checkSignMessage(encodeData, pub)) {
qCritical() << "sig message error RSA" << rsa;
return false;
}
encodeData += "work it";
if (e.checkSignMessage(encodeData, pub)) {
qCritical() << "sig message error RSA with added value to back" << rsa;
return false;
}
encodeData.push_front("not work");
if (e.checkSignMessage(encodeData, pub)) {
qCritical() << "sig message error RSA with added value to front" << rsa;
return false;
}
}
}
return true;
}
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()) {s
return 1;
}
if(!testCrypto(QRSAEncryption::Rsa::RSA_64)) {
return 1;
}