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.



You can use phpseclib, a pure PHP RSA implementation:


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

$rsa = new Crypt_RSA();

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

Start with this:

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

Wednesday, March 31, 2021
answered 7 Months ago

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
answered 5 Months ago

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
answered 5 Months ago

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
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 :