I need to encrypt a string using an RSA 1.5 algorithm. I have been provided with a private key. However, I cannot for the life of me figure out how to add this key to the class. It seems as tho the key needs to be of type RSAParameter stuct. However this requires a set of values I have not been given such as Modulus, Exponent, P, Q, etc.. All I have is the private key. Can anyone help?
Asked
Active
Viewed 2.3k times
14
-
What do you have for the key obviously, you may not want to supply it verbatim but can you describe it some more? – Jodrell May 17 '11 at 11:19
-
Post what you have tried. I suspect you simply do not understand how RSA works to be honest. I don't know what classes your using exactly so I cannot advise. Read this: http://stackoverflow.com/questions/1181421/is-possible-to-encrypt-with-private-key-using-net-rsacryptoserviceprovider – Security Hound May 17 '11 at 11:25
-
1The key I have been provided looks like -----BEGIN RSA PRIVATE KEY-----MIIadfdafCXdfawIBAAKBgQCIgynd6pvlCF= -----END RSA PRIVATE KEY----- -----BEGIN PUBLIC KEY-----jaz+wadfadIDAQAB -----END PUBLIC KEY----- This is all I have.. – Steve Kiss May 17 '11 at 11:29
2 Answers
28
You should be aware of the Bouncycastle C# library. There are in particular two very useful classes: Org.BouncyCastle.OpenSsl.PemReader
which will convert from the openssl style key you have to a bouncycastle key object, and Org.BouncyCastle.Security.DotNetUtilities
, which will convert a bouncycastle key to a .NET RSAParameters
object.
Here is a tiny bit of untested code that shows how to use it
using System;
using System.IO;
using System.Security.Cryptography;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Parameters;
namespace RSAOpensslToDotNet
{
class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader("../../privatekey.pem");
PemReader pr = new PemReader(sr);
AsymmetricCipherKeyPair KeyPair = (AsymmetricCipherKeyPair)pr.ReadObject();
RSAParameters rsa = DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)KeyPair.Private);
}
}
}

President James K. Polk
- 40,516
- 21
- 95
- 125
-
-
I need this exact thing to be done in Java. When I use the pr.readObject() it is returning PEMKeyPair and throwing an exception when I cast it to AsymmetricCipherKeyPair. I cannot figure out why for the life of me. Please help! – C0D3 Apr 04 '13 at 22:12
-
-
1@mikerobi: Are you sure your keyfile contains a private key? – President James K. Polk Sep 28 '14 at 21:42
-
for those using Bouncy Castle under Linux/Docker: https://github.com/bcgit/bc-csharp/issues/160 – monty Sep 12 '19 at 07:07
5
I guess that is what are you looking for:
// Import ASymmetric RSA Key from a system file.
public static RSAParameters ImportRSAKey(String fileName)
{
// Create a stream to a the specified system file.
Stream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
// Extract/Deserialize the key from the file.
IFormatter soapFormatter = new SoapFormatter();
RSAParameters rsaParameter =
(RSAParameters) soapFormatter.Deserialize(fileStream);
// Close the file stream.
fileStream.Close();
return rsaParameter;
}
To generate a new key, you can use RSACryptoServiceProvider.ExportParameters method.
Refer to the following:

Akram Shahda
- 14,655
- 4
- 45
- 65