sayana
sayana

Reputation: 69

RSA Decryption using private key between two systems

I am developing an encryption decryption software. I used RSA encryption to encypt my symmetric key.

I followed the code provided in Walkthrough: Creating a Cryptographic Application

My encryption and decryption done successfully in same machine. But when I tried to decrypt from other computer, an error: bad data is occurring.(It can be decrypted from same machine.)

I think the problem is on getting private key from keycontainer. How to get the private key generated in first machine in the second machine.

I googled a lot but everything in same machine.

Please help me, give me an idea to get private key in other machine.

public void GetPrivateKey()
    {
        string c;

        cspp.KeyContainerName = keyName;

        rsa = new RSACryptoServiceProvider(cspp);
        rsa.PersistKeyInCsp = true;

        if (rsa.PublicOnly == true)
            c= "Key: " + cspp.KeyContainerName + " - Public Only";
        else
            c = "Key: " + cspp.KeyContainerName + " - Full Key Pair";
    }    


 public string decryptkey(string at)
    {
        byte[] KeyEncrypted;
        KeyEncrypted = File.ReadAllBytes(at);
        //System.IO.File.ReadAllBytes(at);//for good 

        objr.GetPrivateKey();
       byte[] KeyDecrypted = objr.rsa.Decrypt(KeyEncrypted, false);
        string skey = GetString(KeyDecrypted);
        return skey;
    }

Bad data Error happens in this line,

byte[] KeyDecrypted = objr.rsa.Decrypt(KeyEncrypted, false);.

Please..

Upvotes: 2

Views: 2191

Answers (1)

Yacoub Massad
Yacoub Massad

Reputation: 27861

Use the RSACryptoServiceProvider.ToXmlString method to export the private key. You need to pass true to this method to export the private key. This will generate for you an XML document that contains the key parameters including the private parameters.

On the second machine, use RSACryptoServiceProvider.FromXmlString to import the private key into a RSACryptoServiceProvider instance.

However, for security reasons, I recommend that instead of doing this, generate the private key on one machine (the machine that will do the decryption part), and then use the RSACryptoServiceProvider.ToXmlString and pass false to it to just export the public key. On the other machine (that will do the encryption part), import the public key using the RSACryptoServiceProvider.FromXmlString method.

Using the public key alone, you can do the encryption part of the process.

It is only for decryption that you are required to have the private key.

Here is some sample code:

//Do this on one machine
RSACryptoServiceProvider rsa_machine1 = new RSACryptoServiceProvider(); //You might initialize this in a different way

var xml = rsa_machine1.ToXmlString(true); //or pass false to just export the public key

Now take the value of the xml variable to the other machine (maybe by saving it to a file and then manually copying that file to the second machine)

//This is done on the second machine
RSACryptoServiceProvider rsa_machine2 = new RSACryptoServiceProvider();

rsa_machine2.FromXmlString(xml);

Upvotes: 4

Related Questions