Created
January 29, 2017 16:15
-
-
Save dziwoki/cc41b523c2bd43ee646b957f0aa91943 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); | |
} | |
} | |
} |
Thanks!!!
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
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