[Qt-interest] RSA in QT
Konrad Rosenbaum
konrad at silmor.de
Tue Jan 27 15:56:44 CET 2009
Hi,
On Tue, January 27, 2009 12:54, Serge wrote:
> Konrad Rosenbaum wrote:
>>> I need to encrypt/decrypt text using RSA method.
>>
>> This sounds dangerous. What exactly do you want to achive? What is the
>> purpose of this encryption? What cryptographic protocol do you plan to
>> use?
> Why using RSA dangerous?
Despite its simplicity it is very easy to handle RSA the wrong way.
Eg. if you mess up the encoding it can happen that your precious data is
not encrypted at all:
c = m^e mod n
c -> cyphertext; m -> encoded message;
e -> the "small" part of the public key - usually the number 3, 5 or 7
n -> the "big" part of the public key (modulo)
now if m^e is still smaller than n, you can easily reverse the encryption
without knowing the secret key (just calculate the e'th root of c; which
is only a hard problem if the modulo n had an effect on the number)
This is just the least subtle problem with RSA, there are plenty of others.
> I want to use QCA example: rsatest.cpp
>
> QCA::PrivateKey seckey = QCA::KeyGenerator().createRSA(2048);
> // i'm using 2048 instead of 1024
Ok, so far you are using a 10m pole instead of a 3m pole. I can still walk
around it. Wouldn't it be wise to use a wall? ;-)
1024 bit is plenty. Except if what you are protecting is worth the hassle
of investing several million dollars in cracking the key. I suspect there
are easier ways.
> ..
> QCA::SecureArray result = pubkey.encrypt(arg, QCA::EME_PKCS1_OAEP);
>
> Encryption is needed for communication with web server while program
> activation using serial number. Program sends some secure data to web
> server and receives some secure data from server. Secure data - some XML
> text. It must be encrypted before transferring over internet. Program
> has only public key, which is used when data sent to server. Server
> knows private key and uses it for decryption. When server sends
> response, it uses another public key for encryption. Program knows
> private key and uses it for decryption.
> So there are 2 pairs of keys. One pair is used when data sent to server.
> Another - when data sent to program. Program knows public key of first
> pair and private key of second.
>
> Additionally i'm planning to maintain connection over https protocol.
HTTPS is a very good idea. Here is a plan for you:
1) store the public key of the server in a ressource
2) before establishing the connection purge the CA list of Qt and put your
servers public key in as the only CA key - this will make sure that it is
impossible to communicate with any other server (unless you call
ignoreSslErrors - don't call it!)
3) generate another SSL-certificate and put it completely (including
private and public key) into the ressources
4) use the second SSL-cert as client certificate
5) on the server side check the client certificate and refuse
communication if the wrong or no client certificate is used
This way you reach all your goals:
* the transport is secure (SSL)
* the server is authenticated (1/2)
* the client is authenticated (3-5)
> Currently i'm having these problems:
>
> 1. when program receives response from web server, it must have ability
> ONLY to decrypt received data using private key, which is stored in its
> resources. Server response is cached in QSettings. And i need so that
> hacker could not emulate these data using same encryption. It will work,
> if hacker will not know public key. But as i see, it is possible to find
> out RSA public key, if we know private key:
>
> pubkey = seckey.toPublicKey();
>
> How to achieve the goal, when one side in communication can only decrypt
> using RSA, but can't encrypt?
By using copious amounts of pixie dust and some black magic. A sacrificial
goat would be required as well.
Seriously: don't bother. If someone has control over the client machine it
is much easier to hack the program in other places.
Theoretically this is possible by implementing and using the cryptographic
primitives(*) directly, but I advise against it. Esp. with your limited
knowledge of cryptography and its pitfalls.
(*)cryptographic PRNG, RSA, RSA-keygen, AES de-/encryption, a secure hash
algorithm (eg. SHA-2), encoding and parsing routines for encryption and
signature with RSA, etc.pp.
Why does that data need to be encrypted in QSettings?
> 2. On the web server PHP script is working. I'm using OpenSSL module in
> it. It contains these functions for encryption/decryption:
>
> http://php.net/manual/en/function.openssl-seal.php
> http://php.net/manual/en/function.openssl-open.php
>
> >openssl_open() opens (decrypts) sealed_data using the private key
> >associated with the key identifier priv_key_id and the envelope key
> >env_key , and fills open_data with the decrypted data. The envelope
> >key is generated when the data are sealed and can only be used by one
> >specific private key.
>
> But QCA encrypt method does not use envelope key. How to decrypt in PHP
> data, encrypted by QCA and vice versa?
You would have to use the openssl functions directly. But why bother?
Konrad
More information about the Qt-interest-old
mailing list