Can not decrypt text by public key format XML

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.

Can not decrypt text by public key format XML

Postby jugioh1412 » Wed Jun 19, 2013 3:15 pm

I code like here:
Code: Select all
   include('Crypt/RSA.php');

    $rsa = new Crypt_RSA();
    $rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_XML);
    $rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_XML);
    $key_arrays = $rsa->createKey(1024);
    $privatekey = $key_arrays['privatekey'];
    $publickey = $key_arrays['publickey'];
    file_put_contents('Keys/private.xml', $privatekey);
    file_put_contents('Keys/public.xml', $publickey);

    $plaintext = 'Im superman';
   echo "<br> Plaintext:<br>" . $plaintext . "<br>";

    // encrypt by privatekey
    $rsa->loadKey($privatekey, false);
    $ciphertext = $rsa->encrypt($plaintext);
    echo "<br> Ciphertext:<br>" . $ciphertext . "<br>";

    // decrypt by public key
    $rsa->loadKey($publickey, false);
   
    echo "<br> Plaintext again:<br>" . $rsa->decrypt($ciphertext) . "<br>";

But "Plaintext again" not show?
jugioh1412
Traveler
 
Posts: 1
Joined: Wed Jun 19, 2013 3:00 pm

Re: Can not decrypt text by public key format XML

Postby TerraFrost » Thu Jun 20, 2013 10:08 pm

I made a commit that I think will fix this:

https://github.com/phpseclib/phpseclib/ ... 8c053bbd8f

Thanks for the heads up!
TerraFrost
Legendary Guard
 
Posts: 12357
Joined: Wed Dec 04, 2002 6:37 am

Re: Can not decrypt text by public key format XML

Postby ifernandez » Fri Jun 21, 2013 3:51 pm

I'm having the same issue with RSA, loading an XML Key encrypts text just fine but, decrypt throws an error, also after the commit i downloaded the git sources and testing the shows this when calling encrypt:

Code: Select all
Warning: Crypt_RSA::_stop_element_handler(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in XXXXXXXXXX\Crypt\RSA.php on line 1333

Notice: Undefined property: Crypt_RSA::$current inXXXXXXXXXX\Crypt\RSA.php on line 1333


It does return the encrypted value just fine and they are warnings but....

When i try to decrypt the returned encrypted String an error is thrown:

Code: Select all
Warning: user_error(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in XXXXXXXXXX\RSA-Crypto-PHP\includes\Crypt\RSA.php on line 2223

Notice: Decryption error in XXXXXXXXXX\Crypt\RSA.php on line 2223



The code im using to test is:

Code: Select all
                $xmlKey = $_REQUEST['xmlSignature'];
      
      //Get the parameter to encrypt
      $plaintext = utf8_encode($_REQUEST['plainText']);
      
      include('Crypt/RSA.php');
      
      set_time_limit(0);
      
      $rsa = new Crypt_RSA();
      $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
      $rsa->loadKey($xmlKey, CRYPT_RSA_PRIVATE_FORMAT_XML);
      
      $ciphertext = $rsa->encrypt($plaintext);
      
      $base64Encrypted = base64_encode($ciphertext);
      
      echo "<p>Encrypted Value: " . $base64Encrypted . "</p>" . PHP_EOL;
      
      $base64Decrypted = base64_decode($base64Encrypted);
      
      $plainText2 = utf8_decode($rsa->decrypt($base64Decrypted));
                // Using this line throws the same error
      //$plainText2 = utf8_decode($rsa->decrypt($ciphertext));
      
      echo "<p>Plain Value: " . $plainText2 . "</p>" . PHP_EOL;
ifernandez
Traveler
 
Posts: 3
Joined: Wed Jun 19, 2013 9:57 pm

Re: Can not decrypt text by public key format XML

Postby TerraFrost » Sat Jun 22, 2013 5:50 am

The E_WARNINGs are a bit weird. Like it says, you need to have "date.timezone" defined in php.ini, although normally it's only triggered when you're calling functions like date() or time() or whatever. user_error() isn't a function I'd expect to trigger it.

Anyway, it looks like you're trying to decrypt the ciphertext with the same key that you're producing it with? Well that's obviously not going to work. RSA, itself, doesn't work like that.

So that explains the second E_NOTICE. As for the first... I don't know.

It would be more helpful if you included what you're using for $_REQUEST['xmlSignature'] and $_REQUEST['plainText']
TerraFrost
Legendary Guard
 
Posts: 12357
Joined: Wed Dec 04, 2002 6:37 am

Re: Can not decrypt text by public key format XML

Postby ifernandez » Mon Jun 24, 2013 2:43 pm

Usually the time zone warning only shows when some kind of warning or error is thrown, when everything is OK it doesn't show, so i think it is safe to ignore that one.

The XML Key I'm using contains the private key version as well as the public key part (Modulus, Exponent AND P,Q, DP, DQ, InverseQ, D), so it should be able to Encrypt as well as decrypt any text using the same key.

Im working on a project that requires inter operation between different languages and im trying to find a suitable RSA implementation for all the languages involved so far i have working AS3, C#, JAVA and PHP.

Using the same key I can Encrypt in one language and Decrypt in the others, my current compatibility Matrix for RSA is:
Code: Select all
RSA                             ORIGIN
Destination   C#   PHP   AS3   JAVA
   C#             Y   N   Y   Y
   PHP            Y   N   Y   Y
   AS3            Y   N   Y   Y
   JAVA            Y   N   Y   Y

Yes means that the current implementation is able to decrypt the data from the origin language in the destination language

So far All the languages can encrypt a test message and that message can be decrypted by the others, the only exception is PHP, the library can decrypt the data from all the languages, but the data encrypted in PHP cannot be decrypted in any other language, including PHP itself, that leads me to think that something in the encryption routine is weird, I'm not sure if I'm messing something up or not.

As for the key I'm using is this one:

Code: Select all
<RSAKeyValue>
   <Modulus>u9H+c5lEudTxDp5QRDJc0wR5Bf5xURJB6NQXFvnXDCuQiLDq/0LUGwhqkvU03r95RyzlUNwFa7jKn380sa4c1JfCqoFGRY6YPSvhZYp4TNMi0etBu9mQB4/AXx9TS3dOqQ8QEpqxLuNpSWKSFsGnPFycglm5FHXMtsvxQYzPUWs=</Modulus>
   <Exponent>AQAB</Exponent>
   <P>+isWF0at2KMfMoGJkzrpO0+nA03YsGpKJJ5+TRfcPJwijm/drd2wKKM75gFksBVQB5Sph18FiZ+ySJTdY2iqWQ==</P>
   <Q>wDLWWdtDEXJxpRzBpIavE0noUD7r50h+KINh7P3sBVMKKeiB4HSXysDw8r86WXvkz65HJQ1glPvMPM7RPHDZYw==</Q>
   <DP>A13lFDcdS9cWKmXX5iVa0jOGOeMoUWd8y/VeGceCbUZkqKoUv6xGHKXlvniyVDRVOWTkyy+aMXn2ukdWm2njMQ==</DP>
   <DQ>KtfqQlOHKs95F9ZGMDe5tTIHcEPmcEMPpFZNMPgVccZmAJuMwA0dfRvvWh1Yk3PzuHvf7hmCSxOqHm/34mJBgw==</DQ>
   <InverseQ>4hzD5kwpy730DChlTvXcJPshGT/F/BlOFoR7h3sZMSV9aVCffh4ODcs+zZEve5Jz415kuFI3lQW2gCAfYvHdZw==</InverseQ>
   <D>SYMot7AusRmCfDlEq1yW7lTa1ZjAW33s5CdXHjwNiormmEm+wMg0YpxJslldJy4EVvJgkuR7Dq2WSRUt17mROItE6gtBXQvCxZZ5aS0kq8UQQEtGnlxeL2tww7QP/nH+fJkob2ERKAybw47BuL0JikoMQ7Nzxu0Dq8YZlX8/uhE=</D>
</RSAKeyValue>


As for the plain text im using small strings like: Conejo Pepito

I'm using the same options in all the languages: RSA, 1024 key length, no OEAP and using instead PKCS#1 version 1.5 i think, the Java keys were generated using OpenSSL and the transformed to the XML Format for use in AS3, PHP and C#. If it helps i can send you the test beds in all the languages.

One last thing, so far I'm using messages that are smaller than one block.

Thanks,

Ivan
ifernandez
Traveler
 
Posts: 3
Joined: Wed Jun 19, 2013 9:57 pm

Re: Can not decrypt text by public key format XML

Postby TerraFrost » Mon Jun 24, 2013 8:14 pm

Maybe the other languages you've been trying this out in always use the public key portion of the private key when encrypting and the private key when decrypting. phpseclib, however, doesn't do that.

Anyway, like I said, you need to load the public key explicitely. eg. maybe do something like this:

Code: Select all
#
#-----[ FIND ]------------------------------------------
#
$rsa->loadKey($xmlKey, CRYPT_RSA_PRIVATE_FORMAT_XML);
#
#-----[ AFTER, ADD ]------------------------------------
#
$rsa->loadKey($rsa->getPublicKey());
#
#-----[ FIND ]------------------------------------------
#
      $plainText2 = utf8_decode($rsa->decrypt($base64Decrypted));
#
#-----[ BEFORE, ADD ]-----------------------------------
#
$rsa->loadKey($xmlKey);

That'll make it encrypt with the public key and decrypt with the corresponding private key. Your code is trying to both encrypt and decrypt with the private key and that's never going to work.
TerraFrost
Legendary Guard
 
Posts: 12357
Joined: Wed Dec 04, 2002 6:37 am

Re: Can not decrypt text by public key format XML

Postby ifernandez » Mon Jun 24, 2013 9:48 pm

Loading the public key for encryption instead of the private key did the trick.

Thanks
ifernandez
Traveler
 
Posts: 3
Joined: Wed Jun 19, 2013 9:57 pm

Re: Can not decrypt text by public key format XML

Postby j796160836us » Fri Sep 06, 2013 6:18 am

It works! Thanks.
j796160836us
Traveler
 
Posts: 2
Joined: Fri Sep 06, 2013 4:40 am


Return to phpseclib support

Who is online

Users browsing this forum: No registered users and 1 guest

cron