Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dziwoki/cc41b523c2bd43ee646b957f0aa91943 to your computer and use it in GitHub Desktop.
Save dziwoki/cc41b523c2bd43ee646b957f0aa91943 to your computer and use it in GitHub Desktop.
// Here you can find full description: https://thetial.com/rsa-encryption-and-decryption-net-core/
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Crypto.Parameters;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string plainText = @"Connect with me on linkedin: https://www.linkedin.com/in/dziwoki";
string cihperText = encrypt(plainText);
string decryptedCipherText = decrypt(cihperText);
Console.WriteLine("Encrypted text: {0}", cihperText);
Console.WriteLine("Decrypted text {0}. Encryption/Decryption was correct {1}",
decryptedCipherText, (plainText == decryptedCipherText).ToString());
}
static string encrypt(string plainText) {
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
PemReader pr = new PemReader(
(StreamReader)File.OpenText("./pem_public.pem")
);
RsaKeyParameters keys = (RsaKeyParameters)pr.ReadObject();
// Pure mathematical RSA implementation
// RsaEngine eng = new RsaEngine();
// PKCS1 v1.5 paddings
// Pkcs1Encoding eng = new Pkcs1Encoding(new RsaEngine());
// PKCS1 OAEP paddings
OaepEncoding eng = new OaepEncoding(new RsaEngine());
eng.Init(true, keys);
int length = plainTextBytes.Length;
int blockSize = eng.GetInputBlockSize();
List<byte> cipherTextBytes = new List<byte>();
for (int chunkPosition = 0;
chunkPosition < length;
chunkPosition += blockSize)
{
int chunkSize = Math.Min(blockSize, length - chunkPosition);
cipherTextBytes.AddRange(eng.ProcessBlock(
plainTextBytes, chunkPosition, chunkSize
));
}
return Convert.ToBase64String(cipherTextBytes.ToArray());
}
static string decrypt(string cipherText) {
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
PemReader pr = new PemReader(
(StreamReader)File.OpenText("./pem_private.pem")
);
AsymmetricCipherKeyPair keys = (AsymmetricCipherKeyPair)pr.ReadObject();
// Pure mathematical RSA implementation
// RsaEngine eng = new RsaEngine();
// PKCS1 v1.5 paddings
// Pkcs1Encoding eng = new Pkcs1Encoding(new RsaEngine());
// PKCS1 OAEP paddings
OaepEncoding eng = new OaepEncoding(new RsaEngine());
eng.Init(false, keys.Private);
int length = cipherTextBytes.Length;
int blockSize = eng.GetInputBlockSize();
List<byte> plainTextBytes = new List<byte>();
for (int chunkPosition = 0;
chunkPosition < length;
chunkPosition += blockSize)
{
int chunkSize = Math.Min(blockSize, length - chunkPosition);
plainTextBytes.AddRange(eng.ProcessBlock(
cipherTextBytes, chunkPosition, chunkSize
));
}
return Encoding.UTF8.GetString(plainTextBytes.ToArray());
}
}
}
@dziwoki
Copy link
Author

dziwoki commented Jan 29, 2017

Here you can find full description: (no longer available)

@FarrahStark
Copy link

This is great stuff! Thanks

@chetanctrimble
Copy link

If client side uses the above code for RSA Encryption, a math very similar to the one which is written above would be required for RSA Decryption right ? Or some .NET framework classes like RSACryptoServiceProvider can Decrypt as well ?

@gregberns
Copy link

Exactly what I've been trying to achieve. Thanks for posting this.

@nganslaw
Copy link

Thank you so much for this - it was the best example I have seen and helped me find where my own problem was (I was using the wrong padding).

@Nwgazzaz
Copy link

Nwgazzaz commented Feb 8, 2021

for some reason the keys always come out with a null value, any idea why? even though the path is correct for the key and all

@MegaGone
Copy link

Thanks!!!

@andreromanov
Copy link

andreromanov commented Jan 20, 2023

I tried the code to encrypt by Pem key and decrypt by RSA key with RSACryptoServiceProvider. The decrypt method was successful but the decrypted string was like ≻獕≲›愢潲慭潮䁶楣挮浯Ⱒ∠獐 The original test was in English letters. Why it happened and how to fix it?
Thank you

I've figured out this. The UTF8Encoding fixed it.

dziwoki, thank you! Your code helped very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment