Find the public and private keys using RSACryptoServiceProvider in c#
Asked Answered
F

2

8

I have the following code.

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//Save the public key information to an RSAParameters structure.
RSAParameters RSAKeyInfo = RSA.ExportParameters(true);

byte[] toEncryptData = Encoding.ASCII.GetBytes("hello world");
byte[] encryptedRSA = RSAEncrypt(toEncryptData, RSAKeyInfo, false);
string EncryptedResult = System.Text.Encoding.Default.GetString(encryptedRSA);

byte[] decryptedRSA = RSADecrypt(encryptedRSA, RSAKeyInfo, false);
string originalResult = System.Text.Encoding.Default.GetString(decryptedRSA);
return userDetails.ToString();

When I use the RSAEncrypt method it takes the parameter "RSAKeyInfo" (Public key for encryption and Private key for decryption).

How can I get the value of private and public keys, which this method used for encryption and decryption.

Thanks,

Fragonard answered 23/5, 2013 at 10:42 Comment(0)
D
9

You need to use RSA.ToXmlString

Code below uses two different RSA instances with a shared string containing public and private keys. To obtain only public key, use a false parameters, true parameter will return public + private key.

class Program
{
    public static void Main(string[] args)
    {
        //Encrypt and export public and private keys
        var rsa1 = new RSACryptoServiceProvider();
        string publicPrivateXml = rsa1.ToXmlString(true);   // <<<<<<< HERE
        byte[] toEncryptData = Encoding.ASCII.GetBytes("hello world");
        byte[] encryptedRSA = rsa1.Encrypt(toEncryptData, false);
        string EncryptedResult = Encoding.Default.GetString(encryptedRSA);

        //Decrypt using exported keys
        var rsa2 = new RSACryptoServiceProvider();
        rsa2.FromXmlString(publicPrivateXml);    
        byte[] decryptedRSA = rsa2.Decrypt(encryptedRSA, false);
        string originalResult = Encoding.Default.GetString(decryptedRSA);

    }
}
Delay answered 23/5, 2013 at 11:8 Comment(2)
This works only if you're encrypting and decrypting in the Microsoft world. What happens if a C# client communicates with a JavaScript server? There must be a less specific format for the public key than that Xml.Fleisher
Cross-platform encryption exists, but it's not specified in the question. For cross-platform encryption, it's easier to use a cross-platform library that handles various intricate details like padding, envelopes, endianness of CPU, etc. Indeed, there are various ways to specify the format of the public key, it doesn't need to be in XML, it can be in text, json, or binary formats. There are several public key formats to choose from.Delay
F
0

When you say "get the value of private and public keys", do you really mean that Xml format no one outside the Microsoft world is able to understand? Or the more common and compatible Pem file format?

If it's the latter, then you should have at least .NET 5.0:

    // read Pem format
    var rsa = new RSACryptoServiceProvider(4096);
    rsa.ImportFromPem("-----BEGIN PUBLIC KEY-----" +
        "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAl+ZjexCnoZgRsYuJ7aOD" +
        "dchVUP0wux5efUBltsENabfmnbEiUUL8gNCE+OKKDDUx0g1elohBV4B9B35Atqh1" +
        "c2XsB2a9crh/g/HfnQZfcTLggy+WkXnqa9FIdyEkvvKsxMu2fVNlJV5IXWb3Rz7A" +
        "UgYyleHa9VREa4jSUTq1o/4tm3qx7vqIWg8279q89D+oazXaKmt/xmEOyVM8o/ZM" +
        "rV8J7rEpSNeg0KLLxFqGQ29WT3iUTDETbfSpo7N5pjiK2luUv251MFKGjLIKZJJ6" +
        "iSUionNc8n2e7hMiz0NKbcMb5RBQJyzqlQBjt/GgsIBk1GCp1pQK6+ycAp/CrGcz" +
        "TCDxidNIhcn7iMXKB0M9gJ5GOnibP3p5SBn/HnGJGOOhmTl2N8YYuFtFTsyuOvVq" +
        "hcZru1shAO1bPACoJtkuCH8uQxFp/1Sa1m0ohiEkgVZ40mfsZ6j9RWlq+F8PQIam" +
        "gAOjatyRjhKImWbdQh2C1fK+yGhkkvPiCZiUJ2vZsk+raQjLwtsS/Maj0PY9pQpk" +
        "JklzxxVywGlIoQ0LsRE4RjP+bqBHb2WlKud6vbEQ3FJXC1D5Y3dSJAw8YQZzxx6e" +
        "sDu5wjuZUWC1UzcwckC4Igu8PVPlLz1qcECH45SX5gHkRYCYqnOTzTDt9Wnq/cOm" +
        "FHknm8Ae4uRZjpfnil0bVAkCAwEAAQ==" +
        "-----END PUBLIC KEY-----");

    // write Pem format
    string publicKeyStr = Convert.ToBase64String(rsa.ExportRSAPublicKey());
Fleisher answered 29/11, 2023 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.