Noob RSA Decryption support

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.

Noob RSA Decryption support

Postby clonan » Fri Aug 23, 2013 4:38 pm

Hello, I am very new to encryption and fairly new to PHP in general. I am writing a script that decrypts an AES key. When I run the script I get an empty string for the results.

Any assistance is greatly appreciated! I have attached the current script which contains the private key as well as the base64 encoded string.

Thanks!



Code: Select all
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib/');

include('encrypt/Net/SSH2.php');


$string1 = "Of34ruzydY2aWZhqWvW4OLlXh51Xi9OQWqbRJNu6EbVFbYeLJ7eLg5bY+bc3QazaY/d6zgeJLv92eiRnGGCF1+wh3VUWwJB/+YW2RR0cX3bm972EOYqVrBmbbiZrpN+vFwIJ5StmcA+Pi89lTSGHPcYsewCPvKih+tnzzcAXZsOLP6oEADvt1YxtHjzm62Y+COiPJs0fuSnYZMIE/4IojUispL4GjPdtwv7CNEOacwSSPIh3IxXXSVtdyMfM/8sspXTp5jvYQRqHymkiaNQF2JcHEklt+qrib0UqYg8Ux0KGepAfQH/0R9bTEwXpx+6S9sHNJZ9Q82UAsx8Te/5gK/szz+nKJtcFn0mf3P8YlQNJT8LlWF7i6s1FFWvcssDXc+LcSIOk3LC+sUwxrKyUlxPutn5Rg+C+aD1a6/Id/tphW7HSSXxUZxgfnNy6McvA+kUYRNyphIC17jNF3WF6ID+pNtcV/CkZlpXyrULN+StrW6dvEOyyH+mfJfKKlpxB2ejWW+3iz84LLiu7k86TimM1vN913H6jTA+WomcYBmgYw/ypBXSm9niUtTihnZfJiFZq237+F/KCXlkeDPTGgwHC+zNiZTkiYQiEiBHMJFK4LLXNzBN1DQh+tOtSwPEh3FkSHow8w2kPVWis77F7C+oYnum//rxWSNk89QitqcRd6+rPjqu9TDIvusLiPartYrknIw2RwvNlGRC6RRoiLibhqU+Rxcr29Nx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p201eBbwkdeI9G7a4gJ7g3kdxDvLKs1ClTQq
SbHQ/4WKX0AxohTNsMOHP/s8fM6AwNFOoPo8T+EBxGRcLxr9VfP4+pIUkC1rsmQ2
MgPSXwSaMAOOpk18qic3LQ/GCwbDwvvXLXastnLGXVQwxjqIKQuyF1tFnygibgiN
jNIY5+tPYgGa4e2T14Xe0kQnJvLf/9suMWFCIaDJj9F42hSUb+PXshxPA7A=
-----END RSA PRIVATE KEY-----

echo $privatekey;
//echo $string1;

echo "<br><br>";
include('encrypt/Crypt/RSA.php');

$rsa = new Crypt_RSA();
//echo $rsa->setHash();
//$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
//$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_OAEP);

//echo  base64_decode($string1);

//$rsa->setHash('sha256');

echo "<br><br>";
$rsa->loadKey($privatekey); // private key
//echo $string1;
//echo $rsa->getPrivateKey();
echo "<br><br>";
echo "decrypted";
echo "<br><br>";
echo $rsa->decrypt($string1);
echo $ssh->getRSAErrors();
echo "<br><br>";
echo $rsa->decrypt(base64_decode($string1));
?>
clonan
Traveler
 
Posts: 2
Joined: Fri Aug 23, 2013 4:25 pm

Re: Noob RSA Decryption support

Postby TerraFrost » Sun Aug 25, 2013 8:08 am

Your code is full of syntax errors.

I don't know why you're including Net/SSH2.php and then, later, you're calling a non-existent function on an undefined variable:

echo $ssh->getRSAErrors();

The fact that $ssh is never initialized not withstanding Net_SSH2 does not contain a getRSAErrors function.

Also, you're attempting to use the heredoc syntax for $privatekey but <<<EOS needs to be on its own line and there needs to be an EOS; at the end as well.

That said, I'm not able to decrypt the string either. How are you encrypting it?
TerraFrost
Legendary Guard
 
Posts: 12357
Joined: Wed Dec 04, 2002 6:37 am

Re: Noob RSA Decryption support

Postby clonan » Wed Aug 28, 2013 4:43 pm

Hi TerraFrost,

Thanks for the review. My general method of creating scripts is to try every solution google has to offer than filter out the junk when it works. So the code I included will have a LOT if useless stuff plus I quickly converted the sensitive parts into something I was willing to put online.

So theoverall purpose is to secure data that is being sent over a http POST.
The data is encrypted with DES (more than secure enough for my purpose).
The 8 digit DES key is encrypted using RSA.
The RSA public key is downloaded from my server. (http://www.aquilasolutions.us/test/pub_key.php)
A random 4 digit code identifies which public key is used.(this allows me to retire key sets easily)
Since everything is being sent through an http POST I am also encoding everything in base64

So:
1. script takes the 4 digit code and pulls the private key from my database.
2. it needs to decrypt the DES key
3. it needs to decrypt the data
4. do other stuff with data.

The program that encrypts the strings is written in c#. the code is as follows:

Code: Select all
           HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.aquilasolutions.us/test/pub_key.php");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Stream resStream = response.GetResponseStream();
            string tempString = null;
            int count = 0;
            StringBuilder sb = new StringBuilder();

            do
            {
                byte[] buffer = new byte[1024];

                count = resStream.Read(buffer, 0, buffer.Length);

                if (count != 0)
                {
                    tempString = Encoding.UTF8.GetString(buffer, 0, count);

                    sb.Append(tempString);
                }
            }
            while (count > 0);

            string publicKey = sb.ToString().Replace(" ", "").Replace("\r\n", "").Substring(sb.ToString().IndexOf("<key>") + 55, 702);
            string publicCode = sb.ToString().Substring(sb.ToString().IndexOf("<code>") + 6, 4);

            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

            RSAParameters keyInfo = rsa.ExportParameters(false);
            keyInfo.Modulus = Encoding.UTF8.GetBytes(publicKey);
            keyInfo.Exponent = Encoding.UTF8.GetBytes(publicCode);

            rsa.ImportParameters(keyInfo);

            byte[] plaintextarray = Encoding.UTF8.GetBytes(DESkey);
            byte[] ciphertextarray = rsa.Encrypt(plaintextarray, false);

            string encryptedKey = Convert.ToBase64String(ciphertextarray);


Do you have any ideas?

Thanks!!
clonan
Traveler
 
Posts: 2
Joined: Fri Aug 23, 2013 4:25 pm

Re: Noob RSA Decryption support

Postby TerraFrost » Wed Aug 28, 2013 9:53 pm

Code: Select all
            byte[] ciphertextarray = rsa.Encrypt(plaintextarray, false);

Per that you're using PKCS#1 padding so you do want to do this:

Code: Select all
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);

Another thought: are you trying to load the keys in C# in the same format that you're trying to load them in the PHP code you posted? I'm not a C# expert or anything but it's my understanding C#'s RSA stuff uses a different format:

http://phpseclib.sourceforge.net/rsa/ex ... onvert,xml

Maybe the keys just aren't of the right format?
TerraFrost
Legendary Guard
 
Posts: 12357
Joined: Wed Dec 04, 2002 6:37 am


Return to phpseclib support

Who is online

Users browsing this forum: No registered users and 3 guests

cron