Encrypt and Decrypt in RSA Public_key MOD

Asked

Viewed 60 times

3

I’m trying to encrypt strings in RSA , but did not succeed.

I tried this way (with the lib phpseclib):

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '...');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'donotcache=153265'.mt_rand(1111111,9999999).'&username=abc'); 
$data = curl_exec($ch);
$json = json_decode($data);
$key = $json->{'publickey_mod'};

$privatekey = $key;

$rsa = new Crypt_RSA();
$rsa->loadKey($privatekey);

$plaintext = new Math_BigInteger('teste');
echo $rsa->_exponentiate($plaintext)->toBytes();

And this is the return:

Fatal error: Uncaught Error: Call to a Member Function abs() on null in C: xampp htdocs phpseclib1.0.11 Math Biginteger.php:1683 Stack trace: #0 C: xampp htdocs phpseclib1.0.11 Crypt RSA.php(2194): Math_biginteger->modPow(NULL, NULL) #1 C: xampp htdocs phpseclib1.0.11 rsa.php(10): Crypt_rsa->_exponentiate(Object(Math_biginteger)) #2 {main} thrown in C: xampp htdocs phpseclib1.0.11 Math Biginteger.php on line 1683

I wanted to know if you have how to encrypt/decrypt in a simpler way, or how to solve this error.

Output from publickey_mod ($key):

c1b2bafb799b023c9b56e3e7375dc4ee77f6ee7aa28c274d919205774ae5e9941144333efebfa2bd34a3b5dfd7770da09237394cfe0e3b88f9f3543757a145bd0669d51344d611531fed466eb0a478c23cea404a42acfb426785fa0645bedb97c474e0da17612b62933166106d0f7866e379e4265bcd480b4b843f9e5c6025cc51ed22136ca34ce2e42fc19e4ee7f490304c526b6e389f9fcce1d0766d7a8af958112a163a88c44094533338019af9217e3eb804e26a763e79f68102d46dd88339e0f97ba0581b954c89d979a6d1ab3b4664e7660a0c35a5f00de64e2950ed7659bc9bee2582be987980d01461f12da5cbcf463d9607ac6616bfe57d7dc90217

Remembering that publickey_mod changes with each refresh (post on page)

  • Where you are using the method abs? Doesn’t seem to be in the code snippet you asked the question.

  • is the first time I try to use this lib, friend. I don’t know where I’m going wrong. I want to encrypt a text to RSA using publickey_mod.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.