Decrypt .NET

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.

Re: Decrypt .NET

Postby shekary_f » Fri Oct 29, 2010 7:19 am

hello
I am not sore that it's data was "test" or "123"
but when I test it by before private and public keys and data "ali"
encrypted "ali":
Njt/nsV9WicG9cuCLoKpFUtRmeN/39Oo+t16/g3ACtbpBufw1ft2YbvcM9zJk3mc6mwiyk3fWxqrblFzcPMjvPJMfn//lZeGnDeOQiqNVKU1vxyHVDv4B59weRPlQJiN9sSHYuNF09+vci9OLkH/lqvmcReBR/FsdxBXSdAWn4c=

and so by data "TerraFrost"
encrypted "TerraFrost":
syTD/vD8CZ0UVr69kvNonzV2YAbmiBM56JzpLF1Lz0SgSFdb9SqyGsmZrG2nP3QABMzwt/+of5422LPL7xPcCy+AjA0sHc5fslVNgul/05bjBsO0sZl0PpZaYXmLbEb2uOnkkDTaoHM9IPF07J8xh/8GoZWL36coVFX1HnsjI1s=
It show to me invalid chars!!!
I tested it by
echo $rsa->_exponentiate($ciphertext)->toString();//it was not OK
Tanks for your help.
shekary_f
Traveler
 
Posts: 6
Joined: Tue Oct 26, 2010 7:23 am

Re: Decrypt .NET

Postby shekary_f » Fri Oct 29, 2010 7:24 am

hello
I am not sore that it's data was "test" or "123"
but when I test it by before private and public keys and data "ali"
encrypted "ali":
Njt/nsV9WicG9cuCLoKpFUtRmeN/39Oo+t16/g3ACtbpBufw1ft2YbvcM9zJk3mc6mwiyk3fWxqrblFzcPMjvPJMfn//lZeGnDeOQiqNVKU1vxyHVDv4B59weRPlQJiN9sSHYuNF09+vci9OLkH/lqvmcReBR/FsdxBXSdAWn4c=

and so by data "TerraFrost"
encrypted "TerraFrost":
syTD/vD8CZ0UVr69kvNonzV2YAbmiBM56JzpLF1Lz0SgSFdb9SqyGsmZrG2nP3QABMzwt/+of5422LPL7xPcCy+AjA0sHc5fslVNgul/05bjBsO0sZl0PpZaYXmLbEb2uOnkkDTaoHM9IPF07J8xh/8GoZWL36coVFX1HnsjI1s=
It show to me invalid chars!!!
I tested it by
echo $rsa->_exponentiate($ciphertext)->toString();//it was not OK
Can you current this problem?
tanks.
shekary_f
Traveler
 
Posts: 6
Joined: Tue Oct 26, 2010 7:23 am

Re: Decrypt .NET

Postby TerraFrost » Sat Oct 30, 2010 2:41 pm

The RSAEncryptionTester binary you included in your post concurs with my results. I replace the PrivateKey.xml and the PublicKey.xml files with the private and public keys in your post (note that they aren't the same), do "Load Private Key" and then do "Private Decryption" and get "123".

Using the PrivateKey.xml, PublicKey.xml and the data.txt files that came in the *.rar you posted (as opposed to the public / private keys you posted and the encrypted content) I do get "test". There, however, I'm not doing "Private Decryption" but rather "Public Decryption". eg.

Code: Select all
<?php
include('Crypt/RSA.php');

$publickey = file_get_contents('silverlight/PublicKey.xml');

$xml = new DOMDocument();
$xml->loadXML($publickey);

$modulus = new Math_BigInteger(base64_decode($xml->getElementsByTagName('Modulus')->item(0)->nodeValue), 256);
$exponent = new Math_BigInteger(base64_decode($xml->getElementsByTagName('Exponent')->item(0)->nodeValue), 256);

$rsa = new Crypt_RSA();
$rsa->modulus = $modulus;
$rsa->exponent = $exponent;
$rsa->publicExponent = $exponent;
$rsa->k = strlen($rsa->modulus->toBytes());

$ciphertext = base64_decode(file_get_contents('silverlight/data.txt'));
$ciphertext = new Math_BigInteger($ciphertext, 256);
echo $rsa->_exponentiate($ciphertext)->toBytes();
TerraFrost
Legendary Guard
 
Posts: 12357
Joined: Wed Dec 04, 2002 6:37 am

Re: Decrypt .NET

Postby jjlopez » Fri Mar 04, 2011 6:09 pm

duplicated post ...
Last edited by jjlopez on Mon Mar 14, 2011 9:45 am, edited 1 time in total.
jjlopez
Traveler
 
Posts: 5
Joined: Fri Mar 04, 2011 5:59 pm

Re: Decrypt .NET

Postby jjlopez » Fri Mar 11, 2011 2:33 pm

duplicated post ...
Last edited by jjlopez on Mon Mar 14, 2011 9:45 am, edited 1 time in total.
jjlopez
Traveler
 
Posts: 5
Joined: Fri Mar 04, 2011 5:59 pm

Re: Decrypt .NET

Postby TerraFrost » Fri Mar 11, 2011 4:37 pm

Can you post the code you're using to do the decryption?

Also, I apologize for the delay in getting back to you - I didn't see your post!!
TerraFrost
Legendary Guard
 
Posts: 12357
Joined: Wed Dec 04, 2002 6:37 am

Re: Decrypt .NET

Postby jjlopez » Mon Mar 14, 2011 10:04 am

hi Terrafrost,

tnks for get my post out of the dark side ...

my previous post was a little bit confusing so I've edited it to start from scratch:

I've used the code you published in this post to decode an ecrypted pwd from .NET with the public/private key in XML format. Now I'm trying to encrypt the pwd from phpseclib and decrypt it with .NET with the same public/private key XML.

The code I'm using to encrypt the pwd is:

Code: Select all
function PWRSA_Encriptar ( $data )
   {
       $publickey ='<RSAKeyValue> <Modulus> .......... </Modulus> <Exponent>....</Exponent> </RSAKeyValue>';
                                 $privatekey ='<RSAKeyValue><D>...</D><DP>...</DP> <DQ>...</DQ> <InverseQ>....</InverseQ>     <P>...</P> <Q>...</Q>  </RSAKeyValue>';

       $xml = new DOMDocument();
       $xml->loadXML($publickey);

       $modulus = new Math_BigInteger(base64_decode($xml->getElementsByTagName('Modulus')->item(0)->nodeValue), 256);
       $exponent = new Math_BigInteger(base64_decode($xml->getElementsByTagName('Exponent')->item(0)->nodeValue), 256);
                    $key = array('modulus' => $modulus, 'publicExponent' => $exponent);

       $rsa = new Crypt_RSA();

       $rsa->loadkey($key, CRYPT_RSA_PUBLIC_FORMAT_RAW);
       $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);

       $ciphertext = base64_encode ( $data );
                                 $res = $rsa->encrypt($data);

       return  base64_encode ( $res );

   }


and the code I'm using in c# to decrypt the pwd is :

Code: Select all
using System.Security.Cryptography;
using System.Text;

public class Security
{
    static string XmlKeys = null;

   /// <summary>
    /// Decrypt data
    /// </summary>
    /// <param name="dato">Data to decrypt byte[]</param>
    /// <returns>Decrypted data (string)</returns>
    public static string DeCrypt(byte[] data)
    {
        string result="";

        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

        if (XmlKeys == null)
        {
            XmlKeys = ReadKey();
        }

        rsa.FromXmlString(XmlKeys);       

        Encoding.Default.GetString(rsa.Decrypt( data, false));

        return result;

    }

    /// <summary>
    /// Read key from config file
    /// </summary>
    /// <returns>Returns encryption key</returns>
    public static string ReadKey()
    {
        string result = null;
        try
        {
            StringBuilder key = new StringBuilder("");
            StringBuilder Modulus;
            StringBuilder Exponent;
            StringBuilder P;
            StringBuilder Q;
            StringBuilder DP;
            StringBuilder DQ;
            StringBuilder InverseQ;
            StringBuilder D;

            System.Configuration.AppSettingsReader confRead = new System.Configuration.AppSettingsReader();

            Modulus = new StringBuilder(((string)(confRead.GetValue("ModulusValue", typeof(string)))));
            Exponent = new StringBuilder(((string)(confRead.GetValue("ExponentValue", typeof(string)))));
            P = new StringBuilder(((string)(confRead.GetValue("PValue", typeof(string)))));
            Q = new StringBuilder(((string)(confRead.GetValue("QValue", typeof(string)))));
            DP = new StringBuilder(((string)(confRead.GetValue("DPValue", typeof(string)))));
            DQ = new StringBuilder(((string)(confRead.GetValue("DQValue", typeof(string)))));
            InverseQ = new StringBuilder(((string)(confRead.GetValue("InverseQValue", typeof(string)))));
            D = new StringBuilder(((string)(confRead.GetValue("DValue", typeof(string)))));

            StringBuilder RSAKeyValueStart = new StringBuilder("<RSAKeyValue>");
            StringBuilder RSAKeyValueEnd = new StringBuilder("</RSAKeyValue>");
            StringBuilder ModulusStart = new StringBuilder("<Modulus>");
            StringBuilder ModulusEnd = new StringBuilder("</Modulus>");
            StringBuilder ExponentStart = new StringBuilder("<Exponent>");
            StringBuilder ExponentEnd = new StringBuilder("</Exponent>");
            StringBuilder PStart = new StringBuilder("<P>");
            StringBuilder PEnd = new StringBuilder("</P>");
            StringBuilder QStart = new StringBuilder("<Q>");
            StringBuilder QEnd = new StringBuilder("</Q>");
            StringBuilder DPStart = new StringBuilder("<DP>");
            StringBuilder DPEnd = new StringBuilder("</DP>");
            StringBuilder DQStart = new StringBuilder("<DQ>");
            StringBuilder DQEnd = new StringBuilder("</DQ>");
            StringBuilder InverseQStart = new StringBuilder("<InverseQ>");
            StringBuilder InverseQEnd = new StringBuilder("</InverseQ>");
            StringBuilder DStart = new StringBuilder("<D>");
            StringBuilder DEnd = new StringBuilder("</D>");

            key.Append(RSAKeyValueStart);
            key.Append(ModulusStart);
            key.Append(Modulus);
            key.Append(ModulusEnd);
            key.Append(ExponentStart);
            key.Append(Exponent);
            key.Append(ExponentEnd);
            key.Append(PStart);
            key.Append(P);
            key.Append(PEnd);
            key.Append(QStart);
            key.Append(Q);
            key.Append(QEnd);
            key.Append(DPStart);
            key.Append(DP);
            key.Append(DPEnd);
            key.Append(DQStart);
            key.Append(DQ);
            key.Append(DQEnd);
            key.Append(InverseQStart);
            key.Append(InverseQ);
            key.Append(InverseQEnd);
            key.Append(DStart);
            key.Append(D);
            key.Append(DEnd);
            key.Append(RSAKeyValueEnd);

            result = key.ToString();
        }
        catch
        {
            result = "";
        }
        return result;
    }

}


any help would be appreciated
jjlopez
Traveler
 
Posts: 5
Joined: Fri Mar 04, 2011 5:59 pm

Re: Decrypt .NET

Postby TerraFrost » Thu Mar 17, 2011 8:53 am

Looking at the code it's not clear to me whether it's the public key that's being used to do the decryption or the private key. Since it's the public key that's doing the encryption you'd need to use the private key to do the decryption. This article makes me think the private key is being used to do the decryption but, just to make sure, you could try to encrypt with the private key with phpseclib or call the encrypt function in Java instead of the decrypt function. I don't imagine either of those would actually fix the problem but it's worth a shot!

If you could provide me with a compiled executable of the program doing the decrypting (key included) and the PHP script with the key included as well I could do more testing instead of guessing. Could you generate new keys for this purpose? If not you could email me the info maybe.
TerraFrost
Legendary Guard
 
Posts: 12357
Joined: Wed Dec 04, 2002 6:37 am

Re: Decrypt .NET

Postby jjlopez » Thu Mar 17, 2011 7:12 pm

hi,

the key used by .NET to decrypt is:

Code: Select all
<appSettings>
        <add key="ModulusValue" value="yEQs2LxSHBZgZCH0rRQQy9kmry8g2tNhQL1B9f5azNz9Ce9pXPgSRjVUo1B9Ggb/FK3jy41wWd2IfS6rse3vBzRsabMj29CVODM/19yZPmwEmjJHCgYd+AA2qweKZanDp4FLsSw/kyV5WoPN16GHEMLmLGkJFNIWtzzH5jV+S80="/>
        <add key="ExponentValue" value="AQAB"/>
        <add key="PValue" value="+SYfJCBnleSlNOjF4CTc3HXJwWNnOyLPkRTl1+snA+iOljHvZSuKT0/s4gTMIAarFSorw4AzEb8ozTS0Rg3vcw=="/>
        <add key="QValue" value="zcXyDShGa3ZxtGEqmTJYdccCnj7CuPs8GBaQjQOqylWd8/zZJDFsBRVK05Ktg8b5ImquNgSTNs0SckYx3iGHvw=="/>
        <add key="DPValue" value="VUVk1xp4GWcd29PnFWXIaXOuz49T33t0ivUaDSIgQkgvtNv2USvrtGVob5qwui1OFHpQbCLOV76TGlh1cpyGXQ=="/>
        <add key="DQValue" value="KWBWLaMdldo5PuYS5L8k3nnn4t4H1Z5fFRDkfBZyr0DMBBWhrA9sAxGm/CgQT+Egk3UUFrSQzYIq5WOL/3iKjQ=="/>
        <add key="InverseQValue" value="shBnbX+byy+9zxQ0q2oipP0fP+b422vmRzzsspOkfzbvYCx6zANAHFgmJkZZdxmrHUQzZRWWBcRT4TscEy3EAg=="/>
        <add key="DValue" value="p6lD7nPDPlaRfmNbJ6e75B25oEKRfAIp0nxgA6VduVNt2OqByF67VeICKPQSuD6RQWvPYTPZkrLAOSVggwcS6/Vf1TlqGPD71rDH2V8lAt99glhOxnTrUJ8S948HNwM6vdayciUkGWuFMxp3M7teiHweqihImUPXF7TZySu0VpU="/>
</appSettings>


and the values for private/public key I'm using in my code are :

Code: Select all
$publickey ='<RSAKeyValue>
      <Modulus>yEQs2LxSHBZgZCH0rRQQy9kmry8g2tNhQL1B9f5azNz9Ce9pXPgSRjVUo1B9Ggb/FK3jy41wWd2IfS6rse3vBzRsabMj29CVODM/19yZPmwEmjJHCgYd+AA2qweKZanDp4FLsSw/kyV5WoPN16GHEMLmLGkJFNIWtzzH5jV+S80=</Modulus>
      <Exponent>AQAB</Exponent></RSAKeyValue>';

$privatekey ='<RSAKeyValue>
      <D>p6lD7nPDPlaRfmNbJ6e75B25oEKRfAIp0nxgA6VduVNt2OqByF67VeICKPQSuD6RQWvPYTPZkrLAOSVggwcS6/Vf1TlqGPD71rDH2V8lAt99glhOxnTrUJ8S948HNwM6vdayciUkGWuFMxp3M7teiHweqihImUPXF7TZySu0VpU=</D>
    <DP>VUVk1xp4GWcd29PnFWXIaXOuz49T33t0ivUaDSIgQkgvtNv2USvrtGVob5qwui1OFHpQbCLOV76TGlh1cpyGXQ==</DP>
    <DQ>KWBWLaMdldo5PuYS5L8k3nnn4t4H1Z5fFRDkfBZyr0DMBBWhrA9sAxGm/CgQT+Egk3UUFrSQzYIq5WOL/3iKjQ==</DQ>
    <InverseQ>shBnbX+byy+9zxQ0q2oipP0fP+b422vmRzzsspOkfzbvYCx6zANAHFgmJkZZdxmrHUQzZRWWBcRT4TscEy3EAg==</InverseQ>
    <P>+SYfJCBnleSlNOjF4CTc3HXJwWNnOyLPkRTl1+snA+iOljHvZSuKT0/s4gTMIAarFSorw4AzEb8ozTS0Rg3vcw==</P>
    <Q>zcXyDShGa3ZxtGEqmTJYdccCnj7CuPs8GBaQjQOqylWd8/zZJDFsBRVK05Ktg8b5ImquNgSTNs0SckYx3iGHvw==</Q>
    </RSAKeyValue>';


With the following function (the same wich you published in the top of this post) I'm able to decrypt anything from .NET:

Code: Select all
function PWRSA_decrypt ( $cadena )
     {
            $publickey ='<RSAKeyValue>
      <Modulus>yEQs2LxSHBZgZCH0rRQQy9kmry8g2tNhQL1B9f5azNz9Ce9pXPgSRjVUo1B9Ggb/FK3jy41wWd2IfS6rse3vBzRsabMj29CVODM/19yZPmwEmjJHCgYd+AA2qweKZanDp4FLsSw/kyV5WoPN16GHEMLmLGkJFNIWtzzH5jV+S80=</Modulus>
      <Exponent>AQAB</Exponent></RSAKeyValue>';

      $privatekey ='<RSAKeyValue>
      <D>p6lD7nPDPlaRfmNbJ6e75B25oEKRfAIp0nxgA6VduVNt2OqByF67VeICKPQSuD6RQWvPYTPZkrLAOSVggwcS6/Vf1TlqGPD71rDH2V8lAt99glhOxnTrUJ8S948HNwM6vdayciUkGWuFMxp3M7teiHweqihImUPXF7TZySu0VpU=</D>
    <DP>VUVk1xp4GWcd29PnFWXIaXOuz49T33t0ivUaDSIgQkgvtNv2USvrtGVob5qwui1OFHpQbCLOV76TGlh1cpyGXQ==</DP>
    <DQ>KWBWLaMdldo5PuYS5L8k3nnn4t4H1Z5fFRDkfBZyr0DMBBWhrA9sAxGm/CgQT+Egk3UUFrSQzYIq5WOL/3iKjQ==</DQ>
    <InverseQ>shBnbX+byy+9zxQ0q2oipP0fP+b422vmRzzsspOkfzbvYCx6zANAHFgmJkZZdxmrHUQzZRWWBcRT4TscEy3EAg==</InverseQ>
    <P>+SYfJCBnleSlNOjF4CTc3HXJwWNnOyLPkRTl1+snA+iOljHvZSuKT0/s4gTMIAarFSorw4AzEb8ozTS0Rg3vcw==</P>
    <Q>zcXyDShGa3ZxtGEqmTJYdccCnj7CuPs8GBaQjQOqylWd8/zZJDFsBRVK05Ktg8b5ImquNgSTNs0SckYx3iGHvw==</Q>
    </RSAKeyValue>';




         $xml = new DOMDocument();
        $xml->loadXML($publickey);

        $modulus = new Math_BigInteger(base64_decode($xml->getElementsByTagName('Modulus')->item(0)->nodeValue), 256);
         $exponent = new Math_BigInteger(base64_decode($xml->getElementsByTagName('Exponent')->item(0)->nodeValue), 256);

         $xml = new DOMDocument();
         $xml->loadXML($privatekey);

         $d = new Math_BigInteger(base64_decode($xml->getElementsByTagName('D')->item(0)->nodeValue), 256);
         $dp = new Math_BigInteger(base64_decode($xml->getElementsByTagName('DP')->item(0)->nodeValue), 256);
         $dq = new Math_BigInteger(base64_decode($xml->getElementsByTagName('DQ')->item(0)->nodeValue), 256);
         $inverseq = new Math_BigInteger(base64_decode($xml->getElementsByTagName('InverseQ')->item(0)->nodeValue), 256);
         $p = new Math_BigInteger(base64_decode($xml->getElementsByTagName('P')->item(0)->nodeValue), 256);
         $q = new Math_BigInteger(base64_decode($xml->getElementsByTagName('Q')->item(0)->nodeValue), 256);

         $rsa = new Crypt_RSA();
         $rsa->modulus = $modulus;
         $rsa->publicExponent = $exponent;
         $rsa->exponents = array(1=> $dp, $dq);
         $rsa->coefficients = array(2 => $inverseq);
         $rsa->primes = array(1 => $p, $q);
         $rsa->k = strlen($rsa->modulus->toBytes());

         $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);


         $ciphertext = base64_decode ( $cadena );
         $res = $rsa->decrypt($ciphertext);

         return $res;

   }


but when I try to encrypt anything with the following function, .NET cannot decrypt it:

Code: Select all
function PWRSA_Crypt ( $cadena )
   {
       $publickey ='<RSAKeyValue>
      <Modulus>yEQs2LxSHBZgZCH0rRQQy9kmry8g2tNhQL1B9f5azNz9Ce9pXPgSRjVUo1B9Ggb/FK3jy41wWd2IfS6rse3vBzRsabMj29CVODM/19yZPmwEmjJHCgYd+AA2qweKZanDp4FLsSw/kyV5WoPN16GHEMLmLGkJFNIWtzzH5jV+S80=</Modulus>
      <Exponent>AQAB</Exponent>
      </RSAKeyValue>';

       $xml = new DOMDocument();
       $xml->loadXML($publickey);



       $modulus = new Math_BigInteger(base64_decode($xml->getElementsByTagName('Modulus')->item(0)->nodeValue), 256);
       $exponent = new Math_BigInteger(base64_decode($xml->getElementsByTagName('Exponent')->item(0)->nodeValue), 256);
      $key = array('modulus' => $modulus, 'publicExponent' => $exponent);

       $rsa = new Crypt_RSA();

       $rsa->loadkey($key, CRYPT_RSA_PUBLIC_FORMAT_RAW);
      
       $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);

       $ciphertext = base64_encode ( $cadena );
     $res = $rsa->encrypt($cadena);

       return  base64_encode ( $res );

   }


thnks for your help,
jjlopez
Traveler
 
Posts: 5
Joined: Fri Mar 04, 2011 5:59 pm

Re: Decrypt .NET

Postby TerraFrost » Sat Mar 19, 2011 3:12 am

Hmmm. PWRSA_decrypt(PWRSA_Crypt('zzz')) works just fine.

This is just a shot in the dark but can you try this?:

Code: Select all
#
#-----[ OPEN ]------------------------------------------
#
Crypt/RSA.php
#
#-----[ FIND ]------------------------------------------
#
        $em = chr(0) . chr(2) . $ps . chr(0) . $m;

#
#-----[ REPLACE WITH ]----------------------------------
#
        $em = chr(0) . chr(0) . $ps . chr(0) . $m;

The following comment, from Crypt/RSA.php, kinda explains the reason behind that change:

Code: Select all
     * For compatability purposes, this function departs slightly from the description given in RFC3447.
     * The reason being that RFC2313#section-8.1 (PKCS#1 v1.5) states that ciphertext's encrypted by the
     * private key should have the second byte set to either 0 or 1 and that ciphertext's encrypted by the
     * public key should have the second byte set to 2.  In RFC3447 (PKCS#1 v2.1), the second byte is supposed
     * to be 2 regardless of which key is used.  for compatability purposes, we'll just check to make sure the
     * second byte is 2 or less.  If it is, we'll accept the decrypted string as valid.
     *
     * As a consequence of this, a private key encrypted ciphertext produced with Crypt_RSA may not decrypt
     * with a strictly PKCS#1 v1.5 compliant RSA implementation.  Public key encrypted ciphertext's should but
     * not private key encrypted ciphertext's.

You're encrypting with the public key so it doesn't seem like this change should work but who knows... it's worth a shot!
TerraFrost
Legendary Guard
 
Posts: 12357
Joined: Wed Dec 04, 2002 6:37 am

Re: Decrypt .NET

Postby jjlopez » Mon Mar 21, 2011 7:01 pm

thanks terrafrost,

your shot in the dark has been the answer :banana

thanks for your time, and keep on

you're doing a very good job with phpseclib

thakns again.
jjlopez
Traveler
 
Posts: 5
Joined: Fri Mar 04, 2011 5:59 pm

Re: Decrypt .NET

Postby TerraFrost » Mon Apr 18, 2011 12:21 pm

Just FYI... phpseclib should now have built-in support for XML keys.

Thus far, I've seen two different types. shekary_f's private key contained the Exponent and Modulus whereas rsanubee's private key did not. Thus to make shekary_f's private key work with phpseclib the public key and the private key will need to be combined thusly:

$private = '<contianer>' . $public . $private . '</container';

eg. the end result being something like this:

Code: Select all
<container>
<RSAKeyValue>
  <Modulus>yEQs2LxSHBZgZCH0rRQQy9kmry8g2tNhQL1B9f5azNz9Ce9pXPgSRjVUo1B9Ggb/FK3jy41wWd2IfS6rse3vBzRsabMj29CVODM/19yZPmwEmjJHCgYd+AA2qweKZanDp4FLsSw/kyV5WoPN16GHEMLmLGkJFNIWtzzH5jV+S80=</Modulus>
  <Exponent>AQAB</Exponent>
</RSAKeyValue>
<RSAKeyValue>
  <D>p6lD7nPDPlaRfmNbJ6e75B25oEKRfAIp0nxgA6VduVNt2OqByF67VeICKPQSuD6RQWvPYTPZkrLAOSVggwcS6/Vf1TlqGPD71rDH2V8lAt99glhOxnTrUJ8S948HNwM6vdayciUkGWuFMxp3M7teiHweqihImUPXF7TZySu0VpU=</D>
  <DP>VUVk1xp4GWcd29PnFWXIaXOuz49T33t0ivUaDSIgQkgvtNv2USvrtGVob5qwui1OFHpQbCLOV76TGlh1cpyGXQ==</DP>
  <DQ>KWBWLaMdldo5PuYS5L8k3nnn4t4H1Z5fFRDkfBZyr0DMBBWhrA9sAxGm/CgQT+Egk3UUFrSQzYIq5WOL/3iKjQ==</DQ>
  <InverseQ>shBnbX+byy+9zxQ0q2oipP0fP+b422vmRzzsspOkfzbvYCx6zANAHFgmJkZZdxmrHUQzZRWWBcRT4TscEy3EAg==</InverseQ>
  <P>+SYfJCBnleSlNOjF4CTc3HXJwWNnOyLPkRTl1+snA+iOljHvZSuKT0/s4gTMIAarFSorw4AzEb8ozTS0Rg3vcw==</P>
  <Q>zcXyDShGa3ZxtGEqmTJYdccCnj7CuPs8GBaQjQOqylWd8/zZJDFsBRVK05Ktg8b5ImquNgSTNs0SckYx3iGHvw==</Q>
</RSAKeyValue>
</container>
TerraFrost
Legendary Guard
 
Posts: 12357
Joined: Wed Dec 04, 2002 6:37 am

Re: Decrypt .NET

Postby lalex86 » Tue Jul 12, 2011 4:02 pm

Hi jedi,

i'm try to use new phpseclib XML support with keys generated from .NET (RSACryptoServiceProvider) with combine Private and Public.

Using the following code i'm able to encrypt but i get an error while descryptin....

Code: Select all
   // Public + Private Keys from .NET   
   $key="<RSAKeyValue><Modulus>tG4+44/wgkQiWde5YQs9VS8Tbcrg/+MnSWFlS5nW5Wqikh6UQNM+UBPdqgE1m1U8biXnr3OIGXmXDX1yPTgnoEdwtYRa6/QLMjvxRO/cLmPX7aUqcwMKsnsx2qTEhplAOCwkGUNnAnNBqTuPRqUyC7FqaiHWEo7Wd+O8YCGFVXE=</Modulus><Exponent>AQAB</Exponent><P>2VEA5QPEBNMfPFAJ3/LXevkgUseaZTHxeOqXkYjiE4bfCXdrM6tud6GiDMQbsXtCOXAJZ9HkRRjb1XW3KFHmXw==</P><Q>1Ixga0ARN8NjPXK2Yq+Dhz7fMNgcgIpsRGnLoNFskBFmpS8PmkgutReYW4SMKQ10bGZc/HiwhbkSOeevLpw2Lw==</Q><DP>EFMIaSumPuKJFyPACiWY0DEEn5NE4tMkm3ur1Q/eQ68+89wXnnMDQlNO8/yLRALJT709x6G5rwMWCGvmTU4Rqw==</DP><DQ>GcexBjEuJXJYfiKExrF9JZFVpun/gwPHIAEpBqeAIHVL2us7XD43FBX5j9gi2/Pa1C8SRjkpR/7rnARi9idSVw==</DQ><InverseQ>q9pVrFX52MO8PcsY3kaln/cyRpCOUCDUCIYUl4h5VjEYztfulIVYuxS+B/rXYI10M7Bs9GpxbLR5BzfRHKM8cw==</InverseQ><D>oDpzALT3eUc2ViB7kJFhov8OOub6jmLTpZenU87HBq5+tlIPh83QPV6W73zpdQrpgkuJBscLKsxOdv1a8MCodnCdZB5qDpNWPSqtEseeXXgJhXgl8s33nMuLG6LwbsdpoK8OWiSlGF/dksVupl8EpvCEPhoaANKM0TVWLm9ZgX0=</D></RSAKeyValue>";

   $plaintext = 'terrafrost';

   // Should load both?
   $rsa->loadKey($key,CRYPT_RSA_PRIVATE_FORMAT_XML);
   
   // Crypt
   $ciphertext = $rsa->encrypt($plaintext);

   // Descrypt
   echo $rsa->decrypt($ciphertext);



I get the error "Notice: Decryption error in /web/htdocs/xxx.yyy.zz/home/_private/PEAR/Crypt/RSA.php on line 1819"

Can you help me?

Thanks
Alessandro
lalex86
Traveler
 
Posts: 2
Joined: Tue Jul 12, 2011 3:57 pm

Re: Decrypt .NET

Postby TerraFrost » Mon Jul 18, 2011 12:00 am

I apologize for the delay.

Anyway, try this:

viewtopic.php?p=121838#p121838
TerraFrost
Legendary Guard
 
Posts: 12357
Joined: Wed Dec 04, 2002 6:37 am

Re: Decrypt .NET

Postby lalex86 » Mon Jul 18, 2011 8:52 am

Thank you.

FYI i've resolved in the meanwhile.

The trick is tthat loadKey doesn't load both key even if you pass it the .Net-called public+private key.

So you have to call loadKey passing public-only key before encrypt and then call again loadKey passing (public+)private key before decrypt.

Now it works.
lalex86
Traveler
 
Posts: 2
Joined: Tue Jul 12, 2011 3:57 pm

PreviousNext

Return to phpseclib support

Who is online

Users browsing this forum: No registered users and 1 guest

cron