PDA

View Full Version : QCA::RSAPublicKey generating random cryptograms



enkidu
30th July 2009, 18:29
Hi! I am on my way to implement RSA encryption in my app. Here is function:



void QTlenCryptedSocket::setCryptInfo(QString e, QString pq, QString iv)
{
QCA::BigInteger ipq = QCA::BigInteger();
ipq.fromArray(QCA::SecureArray(QCA::hexToArray(pq. toAscii())));
QCA::BigInteger ie(e);
QCA::RSAPublicKey pubkey = QCA::RSAPublicKey(ipq, ie);
qDebug(QCA::arrayToHex(pubkey.n().toArray().toByte Array()).toAscii());
//serverIV = QCA::InitializationVector(QCA::hexToArray(iv));
//wektor i kluczyk
//clientIV = QCA::InitializationVector(16);
//aesKey = QCA::SymmetricKey(16);
QCA::SymmetricKey aesKey(QCA::hexToArray("c73b79e172d22ba4d8d0dec8f80b9c7c")); // = QCA::SymmetricKey(QCA::hexToArray("c73b79e172d22ba4d8d0dec8f80b9c7c"));
QByteArray key = aesKey.toByteArray();
//żeby kluczyk pasował
while (key.length() != 64)
key.append(QCA::hexToArray("00"));
QCA::SecureArray SAKey(key);
//dotąd działa, tu coś pieprzy
QCA::SecureArray result = pubkey.encrypt(SAKey, QCA::EME_PKCS1_OAEP);
qDebug("------------------------------------------------------------");
qDebug("Expected value for RSA e");
qDebug(e.toAscii());
qDebug("Value for RSA e passed to pubkey object");
qDebug(ie.toString().toAscii());
qDebug("Value for RSA e stored in pubkey object");
qDebug(pubkey.e().toString().toAscii());
qDebug("------------------------------------------------------------");
qDebug("Expected value for RSA pq");
qDebug(pq.toAscii());
qDebug("Value for RSA e passed to pubkey object");
qDebug(QCA::arrayToHex(ipq.toArray().toByteArray() ).toAscii());
qDebug("Value for RSA e stored in pubkey object");
qDebug(QCA::arrayToHex(pubkey.n().toArray().toByte Array()).toAscii());
qDebug("------------------------------------------------------------");
qDebug("Expected value for raw string (without leading zeroes)");
qDebug("c73b79e172d22ba4d8d0dec8f80b9c7c");
qDebug("Raw string (with leading zeroes)");
qDebug(QCA::arrayToHex(SAKey.toByteArray()).toAsci i());
qDebug("------------------------------------------------------------");
qDebug("Expected value for encrypted string");
qDebug("12fcb5bf57d24b0ed9f2a04aaf4e381b1b8d04de5096cf41ba 9c097a3d17ed7aaaa52383f189296844257e98629049c1f84a 2493f2fbb5d3889e27a59f0f95b");
qDebug("Encrypted string");
qDebug(QCA::arrayToHex(result.toByteArray()).toAsc ii());
qDebug("------------------------------------------------------------");
result = pubkey.encrypt(SAKey, QCA::EME_PKCS1_OAEP);
qDebug("Encrypted string");
qDebug(QCA::arrayToHex(result.toByteArray()).toAsc ii());
qDebug("------------------------------------------------------------");
QByteArray hash = QCA::arrayToHex(result.toByteArray()).toAscii();
QByteArray ivHash = QCA::arrayToHex(clientIV.toByteArray()).toAscii();
outCipher = new QCA::Cipher(QString("aes128"),QCA::Cipher::CBC,
// use no padding, as we need to use our own version
QCA::Cipher::NoPadding,
// this object will encrypt
QCA::Encode,
aesKey, clientIV);
inCipher = new QCA::Cipher(QString("aes128"),QCA::Cipher::CBC,
// use no padding, as we need to use our own version
QCA::Cipher::NoPadding,
// this object will decrypt
QCA::Decode,
aesKey, QCA::InitializationVector(QCA::hexToArray(iv)));
socket->write("<cipher k1='" + hash + "' k2='" + ivHash + "' />");
}

there is some info dumped with debug inlines

------------------------------------------------------------
Expected value for RSA e
10001
Value for RSA e passed to pubkey object
10001
Value for RSA e stored in pubkey object
10001
------------------------------------------------------------
Expected value for RSA pq
1554a7873b24bb3e0c0101675e018fe184fa3c9e66e80a4c33 b6f2552e7e9c2b671865e1b56ce1701804c550cf124a8614b2 5e1f66c1c58a629f7be94b3650fd
Value for RSA e passed to pubkey object
1554a7873b24bb3e0c0101675e018fe184fa3c9e66e80a4c33 b6f2552e7e9c2b671865e1b56ce1701804c550cf124a8614b2 5e1f66c1c58a629f7be94b3650fd
Value for RSA e stored in pubkey object
1554a7873b24bb3e0c0101675e018fe184fa3c9e66e80a4c33 b6f2552e7e9c2b671865e1b56ce1701804c550cf124a8614b2 5e1f66c1c58a629f7be94b3650fd
------------------------------------------------------------
Expected value for raw string (without leading zeroes)
c73b79e172d22ba4d8d0dec8f80b9c7c
Raw string (with leading zeroes)
c73b79e172d22ba4d8d0dec8f80b9c7c000000000000000000 00000000000000000000000000000000000000000000000000 0000000000000000000000000000
------------------------------------------------------------
Expected value for encrypted string
12fcb5bf57d24b0ed9f2a04aaf4e381b1b8d04de5096cf41ba 9c097a3d17ed7aaaa52383f189296844257e98629049c1f84a 2493f2fbb5d3889e27a59f0f95b
Encrypted string
0b569fa16b74ce23a47a7b9b887c4c5e269106801eb999c13d 0a88df9bb83bd7494ca574ef351f90afc5346ea9f9853bf13e 7adf0d9da0d979f8ff54addb74d1
------------------------------------------------------------
Encrypted string
0cdfbfad1876a59c0eaa3e217181f61f31f7afb798f7721bff 4d93c4f43ae555ee22658ed8da3e3a1cef46ebccfbc2fd3666 37a0b685fe63de7a18c5071ee7ec
------------------------------------------------------------


how to get proper and not changing values?

enkidu
9th August 2009, 22:52
sorry for double post - its QCA "fault" - it cannot provide no-padding RSA encoding right now. I asked on devel mailing list to introduce that, for now I am using OpenSSL and it works somehow. Can be closed.