Answer the question
In order to leave comments, you need to log in
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']);
}
}
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']);
}
[
"dec_by_publ_enc_with_private" => "private key encrypted string"
"dec_by_priv_enc_with_publ" => "public key encrypted string"
]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question