mirror of
https://github.com/QuasarApp/Qt-Secret.git
synced 2025-05-11 22:49:34 +00:00
test version
This commit is contained in:
parent
48ede08d64
commit
2e5ddada3c
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,6 +1,3 @@
|
|||||||
[submodule "uint256_t"]
|
|
||||||
path = uint256_t
|
|
||||||
url = git@github.com:QuasarApp/uint256_t.git
|
|
||||||
[submodule "src/Qt-AES"]
|
[submodule "src/Qt-AES"]
|
||||||
path = src/Qt-AES
|
path = src/Qt-AES
|
||||||
url = git@github.com:QuasarApp/Qt-AES.git
|
url = git@github.com:QuasarApp/Qt-AES.git
|
||||||
|
@ -170,15 +170,22 @@ QByteArray toArray(INT i, short sizeBlok = -1) {
|
|||||||
if (sizeBlok < 0) {
|
if (sizeBlok < 0) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
while (res.rbegin() != res.rend() && !*res.rbegin()) {
|
||||||
|
res.remove(res.size() -1, 1);
|
||||||
|
}
|
||||||
|
|
||||||
return res.left(sizeBlok);
|
return res.left(sizeBlok);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class INT>
|
template<class INT>
|
||||||
INT fromArray(const QByteArray& array) {
|
INT fromArray(const QByteArray& array) {
|
||||||
|
INT res = 0;
|
||||||
|
|
||||||
return *(reinterpret_cast<INT*>(const_cast<char*>(array.data())));
|
memcpy(&res, array.data(),
|
||||||
|
static_cast<unsigned int>(std::min(array.size(),
|
||||||
return 0;
|
static_cast<int>(sizeof(INT)))));
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class INT>
|
template<class INT>
|
||||||
@ -200,16 +207,12 @@ bool keyGenerator(QByteArray &pubKey,
|
|||||||
|
|
||||||
} while((!isMutuallyPrime(eilor, e)));
|
} while((!isMutuallyPrime(eilor, e)));
|
||||||
|
|
||||||
INT d = 0;
|
INT d = ExtEuclid<INT>(eilor , e);;
|
||||||
|
|
||||||
d = ExtEuclid<INT>(eilor , e);
|
|
||||||
|
|
||||||
while(d < 0 ) {
|
while(d < 0 ) {
|
||||||
d += eilor;
|
d += eilor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pubKey.append(toArray(e));
|
pubKey.append(toArray(e));
|
||||||
pubKey.append(toArray(modul));
|
pubKey.append(toArray(modul));
|
||||||
privKey.append(toArray(d));
|
privKey.append(toArray(d));
|
||||||
@ -239,7 +242,7 @@ QByteArray decodeBlok(const INT& block, const INT &d, const INT &m) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<class INT>
|
template<class INT>
|
||||||
QByteArray encodeArray(const QByteArray &rawData, const QByteArray &pubKey) {
|
QByteArray encodeArray(QByteArray rawData, const QByteArray &pubKey) {
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
|
||||||
QByteArray block;
|
QByteArray block;
|
||||||
@ -255,14 +258,10 @@ QByteArray encodeArray(const QByteArray &rawData, const QByteArray &pubKey) {
|
|||||||
|
|
||||||
QByteArray res;
|
QByteArray res;
|
||||||
|
|
||||||
|
rawData.append(ENDLINE);
|
||||||
|
|
||||||
while ((block = rawData.mid(index, blockSize)).size()) {
|
while ((block = rawData.mid(index, blockSize)).size()) {
|
||||||
|
|
||||||
auto i = fromArray<INT>(block);
|
|
||||||
QByteArray j = toArray(i, blockSize);
|
|
||||||
|
|
||||||
auto i2 = fromArray<INT>(j);
|
|
||||||
auto j2 = toArray(i2, blockSize);
|
|
||||||
|
|
||||||
res.append(encodeBlok(fromArray<INT>(block), e, m));
|
res.append(encodeBlok(fromArray<INT>(block), e, m));
|
||||||
index += blockSize;
|
index += blockSize;
|
||||||
}
|
}
|
||||||
@ -286,8 +285,7 @@ QByteArray decodeArray(const QByteArray &rawData, const QByteArray &privKey) {
|
|||||||
res.append(decodeBlok(fromArray<INT>(block), d, m));
|
res.append(decodeBlok(fromArray<INT>(block), d, m));
|
||||||
index += blockSize;
|
index += blockSize;
|
||||||
}
|
}
|
||||||
|
return res.remove(res.lastIndexOf(ENDLINE), res.size());
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QRSAEncryption::QRSAEncryption() {
|
QRSAEncryption::QRSAEncryption() {
|
||||||
@ -325,10 +323,31 @@ QByteArray QRSAEncryption::decode(const QByteArray &rawData, const QByteArray &p
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QByteArray QRSAEncryption::signMessage(QByteArray rawData, const QByteArray &privKey) {
|
||||||
|
auto msg = encode(rawData, privKey);
|
||||||
|
int size = rawData.size();
|
||||||
|
rawData.insert(0, reinterpret_cast<char*>(&size), sizeof (int));
|
||||||
|
rawData.append(msg);
|
||||||
|
|
||||||
|
return rawData;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool QRSAEncryption::checkSignMessage(const QByteArray &rawData, const QByteArray &pubKey) {
|
||||||
|
int mSize = 0;
|
||||||
|
memcpy(&mSize, rawData.left(sizeof (int)), sizeof (int));
|
||||||
|
|
||||||
|
auto message = rawData.mid(sizeof (int), mSize);
|
||||||
|
auto sig = rawData.mid(mSize + static_cast<int>(sizeof (int)));
|
||||||
|
|
||||||
|
return message == decode(sig, pubKey);
|
||||||
|
}
|
||||||
|
|
||||||
bool QRSAEncryption::generatePairKey(QByteArray &pubKey,
|
bool QRSAEncryption::generatePairKey(QByteArray &pubKey,
|
||||||
QByteArray &privKey,
|
QByteArray &privKey,
|
||||||
QRSAEncryption::Rsa rsa)
|
QRSAEncryption::Rsa rsa) {
|
||||||
{
|
|
||||||
|
pubKey.clear();
|
||||||
|
privKey.clear();
|
||||||
|
|
||||||
switch (rsa) {
|
switch (rsa) {
|
||||||
case RSA_64: {
|
case RSA_64: {
|
||||||
|
@ -4,20 +4,25 @@
|
|||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
|
|
||||||
|
#define ENDLINE "#_end_#"
|
||||||
|
|
||||||
class QRSAEncryption
|
class QRSAEncryption
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
enum Rsa {
|
enum Rsa {
|
||||||
RSA_64 = 64, // long time of key generate : 1 ms in processor (2.0 KHz)
|
RSA_64 = 64,
|
||||||
RSA_128 = 128, // long time of key generate : 5 s in processor (2.0 KHz)
|
RSA_128 = 128
|
||||||
// RSA_256 = 256, // long time of key generate : 1 m in processor (2.0 KHz)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
QRSAEncryption();
|
QRSAEncryption();
|
||||||
|
|
||||||
QByteArray encode(const QByteArray &rawData, const QByteArray &pubKey);
|
QByteArray encode(const QByteArray &rawData, const QByteArray &pubKey);
|
||||||
QByteArray decode(const QByteArray &rawData, const QByteArray &privKey);
|
QByteArray decode(const QByteArray &rawData, const QByteArray &privKey);
|
||||||
|
|
||||||
|
QByteArray signMessage(QByteArray rawData, const QByteArray &privKey);
|
||||||
|
bool checkSignMessage(const QByteArray &rawData, const QByteArray &pubKey);
|
||||||
|
|
||||||
bool generatePairKey(QByteArray &pubKey, QByteArray &privKey, Rsa = RSA_128);
|
bool generatePairKey(QByteArray &pubKey, QByteArray &privKey, Rsa = RSA_128);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -2,31 +2,71 @@
|
|||||||
#include <qrsaencryption.h>
|
#include <qrsaencryption.h>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <qdebug.h>
|
#include <qdebug.h>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
QByteArray randomArray() {
|
||||||
|
srand(static_cast<unsigned int>(time(nullptr)));
|
||||||
|
QByteArray res;
|
||||||
|
|
||||||
|
int length = rand() % 1024 * 1024;
|
||||||
|
|
||||||
|
for (int i = 0; i < length; ++i) {
|
||||||
|
res.push_back(static_cast<char>(rand() % 0xFF));
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool testCrypto(QRSAEncryption::Rsa rsa) {
|
||||||
|
QByteArray pub, priv;
|
||||||
|
QRSAEncryption e;
|
||||||
|
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
e.generatePairKey(pub, priv, rsa);
|
||||||
|
|
||||||
|
if (pub.size() != rsa / 4) {
|
||||||
|
qCritical() << "pubKey size wrong RSA" << rsa;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (priv.size() != rsa / 4) {
|
||||||
|
qCritical() << "privKey size wrong RSA" << rsa;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
auto base = randomArray();
|
||||||
|
|
||||||
|
if ( base != e.decode(e.encode(base, pub), priv)) {
|
||||||
|
qCritical() << "encode decode data error RSA" << rsa;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!e.checkSignMessage(e.signMessage(base, pub), priv)) {
|
||||||
|
qCritical() << "sig message error RSA" << rsa;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
|
if(!testCrypto(QRSAEncryption::Rsa::RSA_64)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
QRSAEncryption e;
|
if(!testCrypto(QRSAEncryption::Rsa::RSA_128)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
QByteArray pub, priv;
|
qInfo() << "Tests passed successfully";
|
||||||
|
|
||||||
auto t = QDateTime::currentMSecsSinceEpoch();
|
|
||||||
e.generatePairKey(pub, priv, QRSAEncryption::Rsa::RSA_64);
|
|
||||||
|
|
||||||
QByteArray message = "L";
|
|
||||||
|
|
||||||
auto data = e.encode(message, pub);
|
|
||||||
|
|
||||||
auto res = e.decode(data, priv);
|
|
||||||
qDebug() << "key generate time RSA_64: " << (QDateTime::currentMSecsSinceEpoch() - t);
|
|
||||||
|
|
||||||
t = QDateTime::currentMSecsSinceEpoch();
|
|
||||||
e.generatePairKey(pub, priv, QRSAEncryption::Rsa::RSA_128);
|
|
||||||
|
|
||||||
qDebug() << "key generate time RSA_128: " << (QDateTime::currentMSecsSinceEpoch() - t);
|
|
||||||
|
|
||||||
auto data2 = e.encode(message, pub);
|
|
||||||
|
|
||||||
auto res2 = e.decode(data2, priv);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1 +0,0 @@
|
|||||||
Subproject commit a176a96ab181388d19c3415ba0225a645da43221
|
|
Loading…
x
Reference in New Issue
Block a user