C# RSA Encryption to PHP Decryption using PHP OpenSSL public key - c#
I'm trying to load a OpenSSL public key from a SOAP server through Nusoap into C#, encrypt my data using the public key, then send the data back to the PHP server for decryption using the private key.
My C# looks like this:
static void Main(string[] args)
{
PHPRef.AddService test = new PHPRef.AddService();
var pkey = test.getPublicKey();
//Console.WriteLine(pkey.ToString());
byte[] PublicKey = GetBytes(pkey);
//Values to store encrypted symmetric keys.
byte[] EncryptedSymmetricKey;
byte[] EncryptedSymmetricIV;
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048);
//Get an instance of RSAParameters from ExportParameters function.
RSAParameters RSAKeyInfo = RSA.ExportParameters(false);
//Set RSAKeyInfo to the public key values.
RSAKeyInfo.Modulus = PublicKey;
//Import key parameters into RSA.
RSA.ImportParameters(RSAKeyInfo);
//Create a new instance of the RijndaelManaged class.
RijndaelManaged RM = new RijndaelManaged();
//Encrypt the symmetric key and IV.
EncryptedSymmetricKey = RSA.Encrypt(RM.Key, false);
EncryptedSymmetricIV = RSA.Encrypt(RM.IV, false);
Console.WriteLine("RijndaelManaged Key and IV have been encrypted with RSACryptoServiceProvider.");
byte[] encryptedData = RSA.Encrypt(GetBytes("password"), false);
//byte[] returned = (byte[])(Array)test.getDecrypted((sbyte[])(Array)encryptedData);
//string answer = GetString(returned);
string answer = test.getDecrypted((sbyte[])(Array)encryptedData);
Console.WriteLine(answer);
Console.ReadLine();
}
static byte[] GetBytes(string str)
{
byte[] bytes = Encoding.ASCII.GetBytes(str);
return bytes;
}
static string GetString(byte[] bytes)
{
char[] chars = Encoding.ASCII.GetChars(bytes);
return new string(chars);
}
And my PHP like so:
function getPublicKey()
{
$crt = file_get_contents("public.crt");
// $publickey = str_ireplace("\r", "", $crt);
// $publickey = str_ireplace("\n", "", $publickey);
// $publickey = str_ireplace("-----BEGIN CERTIFICATE-----", "", $publickey);
// $publickey = str_ireplace("-----END CERTIFICATE-----", "", $publickey);
return $crt;
}
function getDecrypted($input)
{
global $privateRSA;
// $privateRSA = str_ireplace("\r", "", $privateRSA);
// $privateRSA = str_ireplace("\n", "", $privateRSA);
// $privateRSA = str_ireplace("-----BEGIN RSA PRIVATE KEY-----", "", $privateRSA);
// $privateRSA = str_ireplace("-----END RSA PRIVATE KEY-----", "", $privateRSA);
if(!openssl_private_decrypt($input, $decrypted, $privateRSA))
return "fail";
else
return "success";
return $decrypted;
}
Needless to say I get "fail" every time. Any suggestions? I'm trying to do this with pure PHP and pure C#, no special libraries. The keys are 2048 bit.
After nearly a full day trying to find this, it was incredibly simple. You don't need BouncyCastle, SecLib, any third-party libraries, nothing.
C#:
static void Main(string[] args)
{
PHPRef.AddService test = new PHPRef.AddService();
var pkey = test.getPublicKey();
byte[] pkeybyte = GetBytes(pkey);
X509Certificate2 cert = new X509Certificate2();
cert.Import(pkeybyte);
RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PublicKey.Key;
byte[] encryptedData = rsa.Encrypt(GetBytes("password"), false);
Console.WriteLine(GetString(encryptedData));
string answer = test.getDecrypted((sbyte[])(Array)encryptedData);
Console.WriteLine(answer);
Console.ReadLine();
}
And the PHP:
Just change getPublicKey like so
function getPublicKey()
{
$crt = file_get_contents("public.crt");
$publickey = str_ireplace("\r", "", $crt);
$publickey = str_ireplace("\n", "", $publickey);
$publickey = str_ireplace("-----BEGIN CERTIFICATE-----", "", $publickey);
$publickey = str_ireplace("-----END CERTIFICATE-----", "", $publickey);
return $publickey;
}
Related
How to write RSA encrypt function in C#
I have following nodejs code to encrypt RSA: const encryptWithRSA = (PublicKey, selData) => { let encrypted = crypto.publicEncrypt( { key: -----BEGIN PUBLIC KEY-----\n${PublicKey}\n-----END PUBLIC KEY-----, padding: crypto.constants.RSA_PKCS1_PADDING, }, Buffer.from(selData) ); return encrypted.toString("base64"); }; Then I tried to convert this code block to C# : using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Security; public static string Encrypt(string publickey, string data) { string key = publickey; Asn1Object obj = Asn1Object.FromByteArray(Convert.FromBase64String(key)); DerSequence publicKeySequence = (DerSequence)obj; DerBitString encodedPublicKey = (DerBitString)publicKeySequence[1]; DerSequence publicKey = (DerSequence)Asn1Object.FromByteArray(encodedPublicKey.GetBytes()); DerInteger modulus = (DerInteger)publicKey[0]; DerInteger exponent = (DerInteger)publicKey[1]; RsaKeyParameters keyParameters = new RsaKeyParameters(false, modulus.PositiveValue, exponent.PositiveValue); RSAParameters parameters = DotNetUtilities.ToRSAParameters(keyParameters); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.ImportParameters(parameters); byte[] dataToEncrypt = Encoding.UTF8.GetBytes(data); byte[] encryptedData = rsa.Encrypt(dataToEncrypt, RSAEncryptionPadding.Pkcs1); return Convert.ToBase64String(encryptedData); } Two code return 2 different results. Anyone can show me what wrong? Thanks!
Associating an X509Certificate2 certificate with a private key in .NET
I'm trying to create an X509Certificate2 object by using this code: https://stackoverflow.com/a/9250034/5589417 How can I get the private key that corresponds to the certificate's public key? I have these methods for encryption and decryption: public static byte[] Encrypt(byte[] plainBytes, X509Certificate2 cert) { RSACryptoServiceProvider publicKey = (RSACryptoServiceProvider)cert.PublicKey.Key; byte[] encryptedBytes = publicKey.Encrypt(plainBytes, false); return encryptedBytes; } public static byte[] Decrypt(byte[] encryptedBytes, X509Certificate2 cert) { RSACryptoServiceProvider privateKey = (RSACryptoServiceProvider)cert.PrivateKey; byte[] decryptedBytes = privateKey.Decrypt(encryptedBytes, false); return decryptedBytes; } When I use the Decrypt method I get a NullRefereneceException for privateKey.
We have to setup manually PrivateKey property for certificate instance. I also updated code from old answer you linked and removed using of obsolete methods: static void Main(string[] args) { var cert = GenerateCertificate("localhost"); byte[] ciphertext = Encrypt(Encoding.ASCII.GetBytes("Hello world!"), cert); byte[] plaintext = Decrypt(ciphertext, cert); Console.WriteLine(Encoding.ASCII.GetString(plaintext)); } static X509Certificate2 GenerateCertificate(string certName) { var secureRandom = new SecureRandom(new CryptoApiRandomGenerator()); var keypairgen = new RsaKeyPairGenerator(); // RSA key size = 1024 bits keypairgen.Init(new KeyGenerationParameters(secureRandom, 1024)); var keypair = keypairgen.GenerateKeyPair(); var gen = new X509V3CertificateGenerator(); // we will use SHA256 signature var signatureFactory = new Asn1SignatureFactory("SHA256WITHRSA", keypair.Private, secureRandom); var CN = new X509Name("CN=" + certName); var SN = BigInteger.ProbablePrime(120, new Random()); gen.SetSerialNumber(SN); gen.SetSubjectDN(CN); gen.SetIssuerDN(CN); gen.SetNotAfter(DateTime.MaxValue); gen.SetNotBefore(DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0))); gen.SetPublicKey(keypair.Public); var newCert = gen.Generate(signatureFactory); var x509cert = new X509Certificate2(DotNetUtilities.ToX509Certificate(newCert)); var rsa = RSA.Create(); var publicKey = (RsaKeyParameters)keypair.Public; var privateKey = (RsaPrivateCrtKeyParameters)keypair.Private; var parameters = new RSAParameters { Modulus = publicKey.Modulus.ToByteArrayUnsigned(), Exponent = publicKey.Exponent.ToByteArrayUnsigned(), P = privateKey.P.ToByteArrayUnsigned(), Q = privateKey.Q.ToByteArrayUnsigned(), DP = privateKey.DP.ToByteArrayUnsigned(), DQ = privateKey.DQ.ToByteArrayUnsigned(), InverseQ = privateKey.QInv.ToByteArrayUnsigned(), D = privateKey.Exponent.ToByteArrayUnsigned(), }; rsa.ImportParameters(parameters); // at this point X509Certificate2 will check if PrivateKey matches PublicKey x509cert.PrivateKey = rsa; return x509cert; } public static byte[] Encrypt(byte[] plainBytes, X509Certificate2 cert) { RSACryptoServiceProvider publicKey = (RSACryptoServiceProvider)cert.PublicKey.Key; byte[] encryptedBytes = publicKey.Encrypt(plainBytes, false); return encryptedBytes; } public static byte[] Decrypt(byte[] encryptedBytes, X509Certificate2 cert) { RSACryptoServiceProvider privateKey = (RSACryptoServiceProvider)cert.PrivateKey; byte[] decryptedBytes = privateKey.Decrypt(encryptedBytes, false); return decryptedBytes; }
RSA decryption - Key does not exist
I'm trying to encrypt and decrypt file with RSA. Encryption is working fine. But I get error when I'm decrypting. Error is key does not exist. Here is the error: http://i.imgur.com/ebF09cU.png public byte[] RSA_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes, RSAParameters RSAKeyInfo) { //initialze the byte arrays to the public key information. byte[] PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56, 74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222, 207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175, 108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194, 240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139, 168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171, 38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93, 106,99,179,68,175,211,164,116,64,148,226,254,172,147}; //Values to store encrypted symmetric keys. byte[] EncryptedSymmetricKey; byte[] EncryptedSymmetricIV; byte[] encryptedBytes = null; // Set your salt here, change it to meet your flavor: // The salt bytes must be at least 8 bytes. byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; using (MemoryStream ms = new MemoryStream()) { using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(_stBitov)) { //Set RSAKeyInfo to the public key values. RSAKeyInfo.Modulus = PublicKey; //Import key parameters into RSA. RSA.ImportParameters(RSAKeyInfo); //Create a new instance of the RijndaelManaged class. RijndaelManaged RM = new RijndaelManaged(); var key = new Rfc2898DeriveBytes(PublicKey, saltBytes, 1000); //Encrypt the symmetric key and IV. EncryptedSymmetricKey = RSA.Encrypt(RM.Key, false); EncryptedSymmetricIV = RSA.Encrypt(RM.IV, false); encryptedBytes = RSA.Encrypt(bytesToBeEncrypted, false); } } return encryptedBytes; } RSAParameters _RSAKeyInfo; public void EncryptFile() { RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); //Get an instance of RSAParameters from ExportParameters function. RSAParameters RSAKeyInfo = RSA.ExportParameters(false); _RSAKeyInfo = RSAKeyInfo; string path = ofd.FileName; if (File.Exists(path)) { string dirPath = Path.GetDirectoryName(path); byte[] bytesToBeEncrypted = File.ReadAllBytes(path); byte[] passwordBytes = File.ReadAllBytes(dirPath + "/KEY_" + ofd.SafeFileName); byte[] bytesEncrypted = RSA_Encrypt(bytesToBeEncrypted, passwordBytes, RSAKeyInfo); string fileEncrypted = dirPath + "/ENCRYPTED_" + ofd.SafeFileName; File.WriteAllBytes(fileEncrypted, bytesEncrypted); } } private void button5_Click(object sender, EventArgs e) { string path = ofd2.FileName; if (File.Exists(path)) { DecryptFile(); richTextBox4.Text = "Dekripcija uspesna"; } else { richTextBox6.Text = "Datoteka ni dodana"; } } private void richTextBox4_TextChanged(object sender, EventArgs e) { } public byte[] RSA_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes, RSAParameters RSAKeyInfo) { byte[] PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56, 74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222, 207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175, 108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194, 240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139, 168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171, 38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93, 106,99,179,68,175,211,164,116,64,148,226,254,172,147}; //Values to store encrypted symmetric keys. byte[] EncryptedSymmetricKey; byte[] EncryptedSymmetricIV; byte[] decryptedBytes = null; // Set your salt here, change it to meet your flavor: // The salt bytes must be at least 8 bytes. byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; using (MemoryStream ms = new MemoryStream()) { using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(_stBitov)) { //Set RSAKeyInfo to the public key values. RSAKeyInfo.Modulus = PublicKey; //Import key parameters into RSA. RSA.ImportParameters(RSAKeyInfo); //Create a new instance of the RijndaelManaged class. RijndaelManaged RM = new RijndaelManaged(); //Encrypt the symmetric key and IV. EncryptedSymmetricKey = RSA.Encrypt(RM.Key, false); EncryptedSymmetricIV = RSA.Encrypt(RM.IV, false); decryptedBytes = RSA.Decrypt(bytesToBeDecrypted, false); } } return decryptedBytes; } public void DecryptFile() { string path = ofd2.FileName; if (File.Exists(path)) { string dirPath = Path.GetDirectoryName(path); byte[] bytesToBeDecrypted = File.ReadAllBytes(path); byte[] passwordBytes = File.ReadAllBytes(dirPath + "/KEY_" + ofd.SafeFileName); byte[] bytesDecrypted = RSA_Decrypt(bytesToBeDecrypted, passwordBytes, _RSAKeyInfo); string file = dirPath + "/DECRYPTED_" + ofd.SafeFileName; File.WriteAllBytes(file, bytesDecrypted); } } Can somebody tell me what to do that decryption is going to work.
RSA is a kind of public-key cryptography. That means you need a public key to encrypt the message and a private key to decrypt your message. It looks like you're using your public key for both encryption and decryption. Where's your private key?
It seems you're trying to do hybrid encryption with RSA+AES, but you forgot to actually use AES to encrypt the plaintext and you forgot to encrypt the symmetric key with RSA. You also need to generate the symmetric key randomly and should not be derived from the public key which is supposed to be constant and public. The error that you presented here is the least of your problems, but as ElectroByt already said, you need to use a private key (RSACryptoServiceProvider#ExportParameters(true)) to decrypt something with RSA. In your case, you would need to decrypt with RSA to get the symmetric key to use it to decrypt the symmetric ciphertext to get the actual message back.
WinRT RSA encryption from public key exponent/modulus
I'm trying to port this method from .NET 4.5 desktop app to a WinRT app: static byte[] DotNetRsaEncrypt(string modulus, string exponent, byte[] data) { var modulusBytes = Convert.FromBase64String(modulus); var exponentBytes = Convert.FromBase64String(exponent); var rsaParameters = new RSAParameters { Modulus = modulusBytes, Exponent = exponentBytes }; var rsa = new RSACryptoServiceProvider(); rsa.ImportParameters(rsaParameters); var encrypted = rsa.Encrypt(data, true); return encrypted; } After reading this RSA Encryption in metro style Application I tried the following: static byte[] WinRtRsaEncrypt(string modulus, string exponent, byte[] data) { var modulusBytes = Convert.FromBase64String(modulus); var exponentBytes = Convert.FromBase64String(exponent); var keyBlob = modulusBytes.Concat(exponentBytes).ToArray().AsBuffer(); var rsa = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaOaepSha1); var key = rsa.ImportPublicKey(keyBlob, CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey); var encrypted = CryptographicEngine.Encrypt(key, data.AsBuffer(), null); return encrypted; } But it does not work. In order to get the same functionality as my desktop app... What AsymmetricAlgorithmNames should I pass to OpenAlgorithm()? What CryptographicPublicKeyBlobType should I pass to ImportPublicKey()?
Following up on user1968335's hint, this worked for me. First, in a C# application, use the following code to obtain a CspBlob from your modulus/exponent: var exponent = Encoding.Default.GetBytes(exponentStr); var modulus = Encoding.Default.GetBytes(modulusStr); var rsaParameters = new RSAParameters { Modulus = modulus, Exponent = exponent }; var rsa = new RSACryptoServiceProvider(); rsa.ImportParameters(rsaParameters); var cspBlobString = Convert.ToBase64String(rsa.ExportCspBlob(false)); Then, in a WinRT application you can use that CspBlob to sign a piece of data like this: private static string SignString(string data) { string cspBlobString = //cspBlob var keyBlob = CryptographicBuffer.DecodeFromBase64String(cspBlobString); AsymmetricKeyAlgorithmProvider rsa = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1); CryptographicKey key = rsa.ImportPublicKey(keyBlob, CryptographicPublicKeyBlobType.Capi1PublicKey); IBuffer plainBuffer = CryptographicBuffer.ConvertStringToBinary(data, BinaryStringEncoding.Utf8); IBuffer encryptedBuffer = CryptographicEngine.Encrypt(key, plainBuffer, null); byte[] encryptedBytes; CryptographicBuffer.CopyToByteArray(encryptedBuffer, out encryptedBytes); return Convert.ToBase64String(encryptedBytes); } If it matters, this is how I generated my asymmetric keys: http://43n141e.blogspot.co.uk/2008/08/rsa-encryption-openssl-to-ruby-to-c-and_27.html
According to CryptoWinRT sample, OpenAlgorithm(...) method takes these values. RSA_PKCS1 RSA_OAEP_SHA1 RSA_OAEP_SHA256 RSA_OAEP_SHA384 See also : RSA cryptography between a WinRT and a .Net app
Read RSA PrivateKey in C# and Bouncy Castle
I have successfully written to public and private key files with OpenSSL format. Files: -----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCpHCHYgawzNlxVebSKXL7vfc/i hP+dQgMxlaPEi7/vpQtV2szHjIP34MnUKelXFuIETJjOgjWAjTTJoj38MQUWc3u7 SRXaGVggqQEKH+cRi5+UcEObIfpi+cIyAm9MJqKabfJK2e5X/OS7FgAwPjgtDbZO ZxamOrWWL8KGB+lH+QIDAQAB -----END PUBLIC KEY----- -----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQCpHCHYgawzNlxVebSKXL7vfc/ihP+dQgMxlaPEi7/vpQtV2szH jIP34MnUKelXFuIETJjOgjWAjTTJoj38MQUWc3u7SRXaGVggqQEKH+cRi5+UcEOb Ifpi+cIyAm9MJqKabfJK2e5X/OS7FgAwPjgtDbZOZxamOrWWL8KGB+lH+QIDAQAB AoGBAIXtL6jFWVjdjlZrIl4JgXUtkDt21PD33IuiVKZNft4NOWLu+wp17/WZYn3S C2fbSXfaKZIycKi0K8Ab6zcUo0+QZKMoaG5GivnqqTPVAuZchkuMUSVgjGvKAC/D 12/b+w+Shs9pvqED1CxfvtePXNwL6ZNuaREFC5hF/YpMVyg5AkEA3BUCZYJ+Ec96 2cwsdY6HocW8Kn+RIqMjkNtyLA19cQV5mpIP7kAiW6drBDlraVANi+5AgK2zQ+ZT hYzs/JfRKwJBAMS1g5/B7XXnfC6VTRs8AMveZudi5wS/aGpaApybsfx1NTLLsm3l GmGTkbCr+EPzvJ5zRSIAHAA6N6NdORwzEWsCQHTli+JTD5dyNvScaDkAvbYFi06f d32IXYnBpcEUYT65A8BAOMn5ssYwBL23qf/ED431vLkcig1Ut6RGGFKKaQUCQEfa UdkSWm39/5N4f/DZyySs+YO90csfK8HlXRzdlnc0TRlf5K5VyHwqDkatmoMfzh9G 1dLknVXL7jTjQZA2az8CQG0jRSQ599zllylMPPVibW98701Mdhb1u20p1fAOkIrz +BNEdOPqPVIyqIP830nnFsJJgTG2eKB59ym+ypffRmA= -----END RSA PRIVATE KEY----- And public key contains just the public key portion of course. After encrypting my message using the public key. I want to read the private key file and decrypt it but it's not working. I'm getting exceptions trying to read the private key saying can't cast object to asymmetriccipherkey. Here is my code: public static AsymmetricKeyParameter ReadAsymmetricKeyParameter(string pemFilename) { var fileStream = System.IO.File.OpenText(pemFilename); var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(fileStream); var KeyParameter = (Org.BouncyCastle.Crypto.AsymmetricKeyParameter)pemReader.ReadObject(); return KeyParameter; } static void Encrypt2(string publicKeyFileName, string inputMessage, string encryptedFileName) { UTF8Encoding utf8enc = new UTF8Encoding(); FileStream encryptedFile = null; try { // Converting the string message to byte array byte[] inputBytes = utf8enc.GetBytes(inputMessage); // RSAKeyPairGenerator generates the RSA Key pair based on the random number and strength of key required /*RsaKeyPairGenerator rsaKeyPairGnr = new RsaKeyPairGenerator(); rsaKeyPairGnr.Init(new Org.BouncyCastle.Crypto.KeyGenerationParameters(new SecureRandom(), 512)); Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair keyPair = rsaKeyPairGnr.GenerateKeyPair(); */ AsymmetricKeyParameter publicKey = ReadAsymmetricKeyParameter(publicKeyFileName); // Creating the RSA algorithm object IAsymmetricBlockCipher cipher = new RsaEngine(); // Initializing the RSA object for Encryption with RSA public key. Remember, for encryption, public key is needed cipher.Init(true, publicKey); //Encrypting the input bytes byte[] cipheredBytes = cipher.ProcessBlock(inputBytes, 0, inputMessage.Length); //Write the encrypted message to file // Write encrypted text to file encryptedFile = File.Create(encryptedFileName); encryptedFile.Write(cipheredBytes, 0, cipheredBytes.Length); } catch (Exception ex) { // Any errors? Show them Console.WriteLine("Exception encrypting file! More info:"); Console.WriteLine(ex.Message); } finally { // Do some clean up if needed if (encryptedFile != null) { encryptedFile.Close(); } } } Here is the decrypt function. 2nd one is without using Bouncy Castle, however, I'd rather use Bouncy Castle since later I'll be also encrypting and decrypting in Java. static void Decrypt2(string privateKeyFileName, string encryptedFileName, string plainTextFileName) { UTF8Encoding utf8enc = new UTF8Encoding(); FileStream encryptedFile = null; StreamWriter plainFile = null; byte[] encryptedBytes = null; string plainText = ""; try { // Converting the string message to byte array //byte[] inputBytes = utf8enc.GetBytes(inputMessage); // RSAKeyPairGenerator generates the RSA Key pair based on the random number and strength of key required /*RsaKeyPairGenerator rsaKeyPairGnr = new RsaKeyPairGenerator(); rsaKeyPairGnr.Init(new Org.BouncyCastle.Crypto.KeyGenerationParameters(new SecureRandom(), 512)); Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair keyPair = rsaKeyPairGnr.GenerateKeyPair(); */ StreamReader sr = File.OpenText(privateKeyFileName); PemReader pr = new PemReader(sr); PemReader pemReader = new PemReader(new StringReader(privateKeyFileName)); AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair)pemReader.ReadObject(); Console.WriteLine(keyPair.ToString()); AsymmetricKeyParameter privatekey = keyPair.Private; Console.WriteLine(pr.ReadPemObject()); AsymmetricCipherKeyPair KeyPair = (AsymmetricCipherKeyPair)pr.ReadObject(); AsymmetricKeyParameter privateKey = ReadAsymmetricKeyParameter(privateKeyFileName); // Creating the RSA algorithm object IAsymmetricBlockCipher cipher = new RsaEngine(); Console.WriteLine("privateKey: " + privateKey.ToString()); // Initializing the RSA object for Decryption with RSA private key. Remember, for decryption, private key is needed //cipher.Init(false, KeyPair.Private); //cipher.Init(false, KeyPair.Private); cipher.Init(false, keyPair.Private); // Read encrypted text from file encryptedFile = File.OpenRead(encryptedFileName); encryptedBytes = new byte[encryptedFile.Length]; encryptedFile.Read(encryptedBytes, 0, (int)encryptedFile.Length); //Encrypting the input bytes //byte[] cipheredBytes = cipher.ProcessBlock(inputBytes, 0, inputMessage.Length); byte[] cipheredBytes = cipher.ProcessBlock(encryptedBytes, 0, encryptedBytes.Length); //Write the encrypted message to file // Write encrypted text to file plainFile = File.CreateText(plainTextFileName); plainText = Encoding.Unicode.GetString(cipheredBytes); plainFile.Write(plainText); } catch (Exception ex) { // Any errors? Show them Console.WriteLine("Exception encrypting file! More info:"); Console.WriteLine(ex.Message); } finally { // Do some clean up if needed if (plainFile != null) { plainFile.Close(); } if (encryptedFile != null) { encryptedFile.Close(); } } } // Decrypt a file static void Decrypt(string privateKeyFileName, string encryptedFileName, string plainFileName) { // Variables CspParameters cspParams = null; RSACryptoServiceProvider rsaProvider = null; StreamReader privateKeyFile = null; FileStream encryptedFile = null; StreamWriter plainFile = null; string privateKeyText = ""; string plainText = ""; byte[] encryptedBytes = null; byte[] plainBytes = null; try { // Select target CSP cspParams = new CspParameters(); cspParams.ProviderType = 1; // PROV_RSA_FULL //cspParams.ProviderName; // CSP name rsaProvider = new RSACryptoServiceProvider(cspParams); // Read private/public key pair from file privateKeyFile = File.OpenText(privateKeyFileName); privateKeyText = privateKeyFile.ReadToEnd(); // Import private/public key pair rsaProvider.FromXmlString(privateKeyText); // Read encrypted text from file encryptedFile = File.OpenRead(encryptedFileName); encryptedBytes = new byte[encryptedFile.Length]; encryptedFile.Read(encryptedBytes, 0, (int)encryptedFile.Length); // Decrypt text plainBytes = rsaProvider.Decrypt(encryptedBytes, false); // Write decrypted text to file plainFile = File.CreateText(plainFileName); plainText = Encoding.Unicode.GetString(plainBytes); plainFile.Write(plainText); } catch (Exception ex) { // Any errors? Show them Console.WriteLine("Exception decrypting file! More info:"); Console.WriteLine(ex.Message); } finally { // Do some clean up if needed if (privateKeyFile != null) { privateKeyFile.Close(); } if (encryptedFile != null) { encryptedFile.Close(); } if (plainFile != null) { plainFile.Close(); } } } // Decrypt
I figured this out. Basically to read a private openssl key using BouncyCastle and C# is like this: static AsymmetricKeyParameter readPrivateKey(string privateKeyFileName) { AsymmetricCipherKeyPair keyPair; using (var reader = File.OpenText(privateKeyFileName)) keyPair = (AsymmetricCipherKeyPair)new PemReader(reader).ReadObject(); return keyPair.Private; } Then this key can be used to decrypt data such as below: AsymmetricKeyParameter key = readPrivateKey(pemFilename); RsaEngine e = new RsaEngine(); e.Init(false, key); byte[] decipheredBytes = e.ProcessBlock(cipheredData, 0, cipheredData.Length);