using OpenSSL.PrivateKeyDecoder;
using OpenSSL.PublicKeyDecoder;
using System.Security.Cryptography;
static void Main(string[] args)
String plaintext = "Hello World!";
Console.WriteLine(plaintext);
byte[] ciphertext = RSAEncrypt(plaintext, PublicKey);
String plaintext2 = RSADecrypt(ciphertext, PrivateKey);
Console.WriteLine(plaintext2);
public static string PublicKey
return @"-----BEGIN PUBLIC KEY-----
MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgHmkjx/uqGykst/iCgpQuOgNQFkQ
6VdRACzZA35MSkug0VmAO9pVnx4nRfuPx76FRSE8mT3RCC4Ku0Cvn4JZw1A9TE64
WYXb6mmwu9DZHjJs1MWiCrA5RB3yAVxyTqQc9cosZHQZ10vsayLnE1Ow1LiIsQtK
-----END PUBLIC KEY-----";
public static string PrivateKey
return @"-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgHmkjx/uqGykst/iCgpQuOgNQFkQ6VdRACzZA35MSkug0VmAO9pV
nx4nRfuPx76FRSE8mT3RCC4Ku0Cvn4JZw1A9TE64WYXb6mmwu9DZHjJs1MWiCrA5
RB3yAVxyTqQc9cosZHQZ10vsayLnE1Ow1LiIsQtKIi7JUcPHWppx1pk/AgMBAAEC
gYBP9WqbO958I7rszYF6DFuvNV6x7Rrk6XqrAC96qp0/n0NdiJxPRW7lfD1VQcqz
opixY0r7xlaRJT63I3o/Tc/DFCv2ZkWmHRZa3GBNMYDvEaNuZHXw/fT88Kb6zpv4
M6vvJz9WoAfzfqzTJ2WaU4dHkSA0MQaQxaxL2syGMU2UGQJBAOOaOmtQBfhcc3G6
3k25IM+/MGiAF0ExSbizajrkXAwLyNYAeP/zpf2Fc+OlVjFwA4VSC8DB/05fa9Gm
0sLRSJUCQQCI0ekGlm/tv8vuP8fiStErQeDvCxjUekiopRvq/TTtW98yQb2TVATh
PsaTCgy6R9lx1TxSYnZx7OiwxarNxmGDAkEAzNIBGot0I/IGzeUtouIjvfC+/nuu
8bMKNdX3ljHlB84fGT5ppWQ9D23mQYmLjIfSE0snWnr6ZywF2jB3YGx4FQJAZ1Ej
x464bO9lfr75IN/gfercgYZ+tIy262rq3xNf8VFsBRZxopxSpwjxiL2RCOYSxIwC
Zi/mQ3dj64EIzyTcIwJBANPg1EMcAqvi8Nz/fEeGusyGH9hqzDiLqfN7ZoExGjDo
mcensFZGLRMQV4EhG6Ut9n1+V/5AiDlnQIw0PijU5yo=
-----END RSA PRIVATE KEY-----";
public static byte[] RSAEncrypt(string plaintext, string privateKeyText)
using (RSACryptoServiceProvider cryptoServiceProvider = new OpenSSLPublicKeyDecoder().Decode(privateKeyText))
byte[] encryptedData = cryptoServiceProvider.Encrypt(Encoding.Unicode.GetBytes(plaintext), true);
public static string RSADecrypt(byte[] ciphertext, string privateKeyText)
using (RSACryptoServiceProvider cryptoServiceProvider = new OpenSSLPrivateKeyDecoder().Decode(privateKeyText)) {
byte[] decryptedData = cryptoServiceProvider.Decrypt(ciphertext, true);
return Encoding.Unicode.GetString(decryptedData);