L
L
LINKeR UA2017-05-23 12:14:24
PHP
LINKeR UA, 2017-05-23 12:14:24

RSA encryption. Is it supposed to be like this or is it such an implementation of the library?

Hello! Can you please tell me if it should be like this or is it not quite RSA? I am using the phpseclib
library in a test project. I generate 100 keys and save them to configuration files, where the name === salted hash from the public and private keys. Private key length === 2048.

public function gen_rsa()
    {        
        $this->_include_crypt('Math_BigInteger');
        $this->_include_crypt('Crypt_RSA');

        $rsa = new \Crypt_RSA();

        for($i=0;$i<=100; $i++){

            $keys = $rsa->createKey(2048);
            $this->_save_rsa($keys['privatekey'], $keys['publickey']);

        }
}

Then I try to work with this library
public function test_rsa()
{
        $response = [];
        $this->_include_crypt('Math_BigInteger');
        $this->_include_crypt('Crypt_RSA');

        $rsa = new \Crypt_RSA();
        $k = $this->_load_rsa('9962e61c03618f2e3eace79d604d6783'); // произвольно выбранный ключ из 100

        # encrypt
        $rsa->loadKey($k['public']); // шифрую сообщение#1 публичным
        $response['enc_with_publ'] = $rsa->encrypt('public key encrypted string');

        $rsa->loadKey($k['private']); // шифрую сообщение#2 приватным
        $response['enc_with_priv'] = $rsa->encrypt('private key encrypted string');

        # decrypt

        $rsa->loadKey($k['public']);
        $response['dec_by_publ_enc_with_private'] = $rsa->decrypt($response['enc_with_priv']);

        $rsa->loadKey($k['private']);
        $response['dec_by_priv_enc_with_publ'] = $rsa->decrypt($response['enc_with_publ']);

}

in $response:
[
 "dec_by_publ_enc_with_private" => "private key encrypted string"
  "dec_by_priv_enc_with_publ" => "public key encrypted string"
]

Now the question is:
Is this function "two-way"?
v1 : You can encrypt with public and decrypt with private
v2 : You can encrypt with private and decrypt with public
I ask because I am not familiar enough with encryption and I am convinced that this should not be the case. That encryption through RSA should only go in one direction as in v1 . They say that the library that I use is "curve".
Please judge me and my colleague, who is right and who is not! Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Ocelot, 2017-05-23
@LINKeRxUA

Yes, both modes are perfectly normal use of RSA.
v1 is used for encryption itself, v2 - for example, for digital signature.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question