From df7de5282e5cfbdda49fabdf20d8d8d0f879cd14 Mon Sep 17 00:00:00 2001 From: "a.yankovich" Date: Sat, 16 Nov 2019 10:28:48 +0300 Subject: [PATCH] fixed a example and remove a old static methods --- Qt-Secret-GUI/secretworker.cpp | 10 +-- README.md | 25 ++++--- src/Qt-RSA/qrsaencryption.cpp | 33 --------- src/Qt-RSA/qrsaencryption.h | 13 ---- tests/main.cpp | 120 +++++++++++++++++++++------------ 5 files changed, 99 insertions(+), 102 deletions(-) diff --git a/Qt-Secret-GUI/secretworker.cpp b/Qt-Secret-GUI/secretworker.cpp index ac78b38..7caadbf 100644 --- a/Qt-Secret-GUI/secretworker.cpp +++ b/Qt-Secret-GUI/secretworker.cpp @@ -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(); diff --git a/README.md b/README.md index 242e2b1..15f9395 100644 --- a/README.md +++ b/README.md @@ -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 +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 diff --git a/src/Qt-RSA/qrsaencryption.cpp b/src/Qt-RSA/qrsaencryption.cpp index 01a3344..ab70855 100644 --- a/src/Qt-RSA/qrsaencryption.cpp +++ b/src/Qt-RSA/qrsaencryption.cpp @@ -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; diff --git a/src/Qt-RSA/qrsaencryption.h b/src/Qt-RSA/qrsaencryption.h index 6fdffb3..2ce0be3 100644 --- a/src/Qt-RSA/qrsaencryption.h +++ b/src/Qt-RSA/qrsaencryption.h @@ -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, diff --git a/tests/main.cpp b/tests/main.cpp index 7f26a61..2b48692 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -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; }