1

I have mobile application that encrypts strings with RSA and sends that encrypted data to PHP web server.

After a search I found that phpseclib is used to decrypt RSA Latest version 2.0.

No matter what I do I receive despite error despite used mode

Am i doing something wrong?

What I have tried:

Private key for decryption:

-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBAJyHUgC1ijhsETeuoNMh4c4yrFoL4juL/yDderMC9fBd1TFgEoJ5dxzMzdlzVVj7Vc/H/I+k13yY3W0MknS//k8CAwEAAQJAaaL1l57s8lkUYZTL2tFh9+vA32BnxLIdc0ullAwqeJV21wXcEyDA67fbmBywdt+pVKkeO2NU7fD3e+DZREuJ0QIhAPZNo9jirkRl4i/Lv3jWt6SmeUBeyIKK0u4lZiBF9KAZAiEAorDjj2c9WBdP46S9hK7yj0U5/0QHB0pO01j9QSVBvqcCIQDrYre7hqdU5qmLVATgzxMiX5ZxViP53gJHZaZ8IV7vwQIgTTYEGafWjjsqGBC0PQdGaMZi+wnPCB+0/0rpjoRfClsCIBPzZw+lappnVxXHuUoQQeN6uevqSvmgvC42UyA4HABa
-----END RSA PRIVATE KEY-----

Encrypted message (base64 encoded):

SMZiVTAMizngWa5Yg2Xp0F3Coy4cIsLB6mru2rLhxnvS2SC\/rm9pgPVLdA\/hp+1TIbzHZqjc2lnP\nkvzh797WlA==\n

PHP sample code:

$rsa = new RSA();
$rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1);
$rsa->loadKey($privateKey); 
echo $rsa->decrypt(base64_decode($strBase64));

Result:

Notice: Decryption error in C:\xampp\htdocs\webservice\vendor\phpseclib\phpseclib\phpseclib\Crypt\RSA.php on line 2553

Mode:

$rsa->setEncryptionMode(RSA::ENCRYPTION_OAEP); 

Result:

Notice: Decryption error in C:\xampp\htdocs\webservice\vendor\phpseclib\phpseclib\phpseclib\Crypt\RSA.php on line 2432

Mode:

$rsa->setEncryptionMode(RSA::ENCRYPTION_NONE); 

Result:

string(128) "I�W��B'q����;k��}�1������=��x���*���_��aq�)�D� '�m{��� ��n���C:��t �E����R=�S�y�3$QC�EV.3C�{�.Y�jx�6��!�e�˱]�I ����S�/�'I�|"
8
  • 1
    did you decode the base64? Commented Nov 30, 2018 at 10:13
  • yes, i decode it. Commented Nov 30, 2018 at 11:59
  • I can't test php right now but the private in your question does decrypt the cipher in your question. NO padding is used. The result is a readable ASCII string. Commented Nov 30, 2018 at 14:45
  • @JamesKPolk - idk how you're doing your decryption but I'm not able to decrypt the OP's ciphertext. I did openssl rsautl -decrypt -in encrypted.txt -out plaintext.txt -raw -inkey priv.pem via the CLI and got a "data greater than mod len" back. This makes sense because the ciphertext is 65 bytes long whereas the modulo for the key is 64 bytes long (512 bits) Commented Dec 1, 2018 at 18:34
  • 1
    @neubert: I see now that the base64 encrypted message contains several invalid escape "\" characters and an embedded newline. Here is the value after removal of those characters: SMZiVTAMizngWa5Yg2Xp0F3Coy4cIsLB6mru2rLhxnvS2SC/rm9pgPVLdA/hp+1TIbzHZqjc2lnPkvzh797WlA== Commented Dec 1, 2018 at 19:28

2 Answers 2

2

As James J Polk observed, your base64 encoded string has some bad characters in it. idk what you're using in PHP as your delimiter but this worked for me:

$strBase64 = 'SMZiVTAMizngWa5Yg2Xp0F3Coy4cIsLB6mru2rLhxnvS2SC\/rm9pgPVLdA\/hp+1TIbzHZqjc2lnP\nkvzh797WlA==\n';
$strBase64 = str_replace(['\/', '\n'], ['/', ''], $strBase64);

If you're using double quotes instead of single quotes do this:

$strBase64 = "SMZiVTAMizngWa5Yg2Xp0F3Coy4cIsLB6mru2rLhxnvS2SC\/rm9pgPVLdA\/hp+1TIbzHZqjc2lnP\nkvzh797WlA==\n";
$strBase64 = str_replace(['\/', "\n"], ['/', ''], $strBase64);
Sign up to request clarification or add additional context in comments.

Comments

0
$rsa = new RSA();
  $rsa->setEncryptionMode(RSA::ENCRYPTION_OAEP);
  $ciphertext = base64_decode($string);
  $rsa->setMGFHash('sha1');
  $rsa->setHash('sha256');
  $rsa->loadKey($pkey); // public key
  $output = $rsa->decrypt($ciphertext);
 return $output;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.