Asked  7 Months ago    Answers:  5   Viewed   34 times

Is there any class for PHP 5.3 that provides RSA encryption/decryption without padding?

I've got private and public key, p,q, and modulus.

 Answers

80

You can use phpseclib, a pure PHP RSA implementation:

<?php
include('Crypt/RSA.php');

$privatekey = file_get_contents('private.key');

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

$plaintext = new Math_BigInteger('aaaaaa');
echo $rsa->_exponentiate($plaintext)->toBytes();
?>
Wednesday, March 31, 2021
 
Jimenemex
answered 7 Months ago
27

Start with this: http://www.ibm.com/developerworks/opensource/library/os-php-encrypt/

After that, have a look at Pascal MARTIN's answer in How do I encrypt a string in PHP?

Wednesday, March 31, 2021
 
e_i_pi
answered 7 Months ago
60

My problem was "e" in javascript.

I changed "65537" to "010001" and now I can encrypt server side using the public key and PHP. Phew!

Saturday, May 29, 2021
 
dotoree
answered 5 Months ago
75

I figured out the solution. It is two parts.

Encode the hex to binary correctly using these two lines:

$modulus = new Math_BigInteger($modulus, 16);
$exponent = new Math_BigInteger($exponent, 16);

And out put the result as a bin to hex as Nate stated.

echo bin2hex($ciphertext);
Saturday, May 29, 2021
 
Gil
answered 5 Months ago
Gil
50

You're encoding the output after decryption.

public String decrypt_string(final String plain) throws ...
{
    byte[] encryptedBytes = decrypt(Base64.decode(plain, 0));
    return Base64.encodeToString( encryptedBytes, Base64.DEFAULT);
    //     ^--------------------| this 
}

If you only encrypt printable data then you can safely remove the Base64.encodeToString call from the above code. To return the correct type, you can do

return new String(encryptedBytes);
Sunday, September 12, 2021
 
Bohemian
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :