RSA (PKCS1 padding) compatibility for BouncyCastle & OpenSSL

Get help with using the PHP Secure Communications Library.

Moderator: Nuxius

Forum rules
The purpose of this forum is to provide support for phpseclib, a pure PHP SSH / SFTP / RSA library.

Posts by new users are held in a moderation queue and are not publicly visible until the post is approved.

RSA (PKCS1 padding) compatibility for BouncyCastle & OpenSSL

Postby Pada » Mon Apr 26, 2010 12:49 pm

Hi,

I've noticed that Bouncy Castle and OpenSSL is not coded strictly according to PKCS#1 v2.1 RFC, which results in phpseclib not being able to decrypt messages encrypted with the private key. They both set the 2nd-bit to 2 when encrypting with a private key, instead of to 1 !

I have a very simple solution for this: Replace the following line in RSA.php, in the _rsaes_pkcs1_v1_5_decrypt function:
Code: Select all
if (ord($em[0]) != 0 || ord($em[1]) != 2) {
with
Code: Select all
$type = ord($em[1]);
if (ord($em[0]) != 0 || (($type != 1) && ($type != 2))) {


I hope that this would be helpful to others.

Regards,
Chris

* Thanks for the great PHP library. It's definitely one of the best/easiest Cryptography libraries to use that I've came across!
Pada
Traveler
 
Posts: 2
Joined: Mon Apr 26, 2010 12:33 pm

Re: RSA (PKCS1 padding) compatibility for BouncyCastle & OpenSSL

Postby TerraFrost » Mon Apr 26, 2010 6:31 pm

I actually committed a similar fix to CVS a short time ago :):

http://phpseclib.cvs.sourceforge.net/vi ... 14&r2=1.15

Anyway, thanks for the feedback! :)
TerraFrost
Legendary Guard
 
Posts: 12217
Joined: Wed Dec 04, 2002 6:37 am

Re: RSA (PKCS1 padding) compatibility for BouncyCastle & OpenSSL

Postby Pada » Mon Apr 26, 2010 9:06 pm

Thank you very much for the fast response.

I didn't know that you included a compatibility fix, since the latest archived version of phpseclib (v0.2.0) was released like 5 Dec 2009.

It really took me (actually my colleague found it) a couple of days to figure out why my colleague's J2ME application was unable to communicate with my PHP script. Your modification should be slightly faster than mine, but you're allowing the second bit to be 0 too :)
Pada
Traveler
 
Posts: 2
Joined: Mon Apr 26, 2010 12:33 pm


Return to phpseclib support

Who is online

Users browsing this forum: No registered users and 0 guests

cron