Skip to content

Instantly share code, notes, and snippets.

@gashupl
Created September 30, 2016 11:34
Show Gist options
  • Save gashupl/27e4de6bd8f021f3d61b3122e6bbf775 to your computer and use it in GitHub Desktop.
Save gashupl/27e4de6bd8f021f3d61b3122e6bbf775 to your computer and use it in GitHub Desktop.
RSA Encrypting & Descrypting in C# sample
using System;
using System.Security.Cryptography;
using System.Text;
namespace RsaEncryptionSample
{
class Program
{
static void Main(string[] args)
{
var cryptoServiceProvider = new RSACryptoServiceProvider(2048); //2048 - Długość klucza
var privateKey = cryptoServiceProvider.ExportParameters(true); //Generowanie klucza prywatnego
var publicKey = cryptoServiceProvider.ExportParameters(false); //Generowanie klucza publiczny
string publicKeyString = GetKeyString(publicKey);
string privateKeyString = GetKeyString(privateKey);
Console.WriteLine("KLUCZ PUBLICZNY: ");
Console.WriteLine(publicKeyString);
Console.WriteLine("-------------------------------------------");
Console.WriteLine("KLUCZ PRYWATNY: ");
Console.WriteLine(privateKeyString);
Console.WriteLine("-------------------------------------------");
string textToEncrypt = GenerateTestString();
Console.WriteLine("TEKST DO ZASZYFROWANIA: ");
Console.WriteLine(textToEncrypt);
Console.WriteLine("-------------------------------------------");
string encryptedText = Encrypt(textToEncrypt, publicKeyString); //Szyfrowanie za pomocą klucza publicznego
Console.WriteLine("ZASZYFROWANY TEXT: ");
Console.WriteLine(encryptedText);
Console.WriteLine("-------------------------------------------");
string decryptedText = Decrypt(encryptedText, privateKeyString); //Odszyfrowywanie za pomocą klucza prywatnego
Console.WriteLine("ODSZYFROWANY TEXT: ");
Console.WriteLine(decryptedText);
}
public static string GetKeyString(RSAParameters publicKey)
{
var stringWriter = new System.IO.StringWriter();
var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters));
xmlSerializer.Serialize(stringWriter, publicKey);
return stringWriter.ToString();
}
public static string Encrypt(string textToEncrypt, string publicKeyString)
{
var bytesToEncrypt = Encoding.UTF8.GetBytes(textToEncrypt);
using (var rsa = new RSACryptoServiceProvider(2048))
{
try
{
rsa.FromXmlString(publicKeyString.ToString());
var encryptedData = rsa.Encrypt(bytesToEncrypt, true);
var base64Encrypted = Convert.ToBase64String(encryptedData);
return base64Encrypted;
}
finally
{
rsa.PersistKeyInCsp = false;
}
}
}
public static string Decrypt(string textToDecrypt, string privateKeyString)
{
var bytesToDescrypt = Encoding.UTF8.GetBytes(textToDecrypt);
using (var rsa = new RSACryptoServiceProvider(2048))
{
try
{
// server decrypting data with private key
rsa.FromXmlString(privateKeyString);
var resultBytes = Convert.FromBase64String(textToDecrypt);
var decryptedBytes = rsa.Decrypt(resultBytes, true);
var decryptedData = Encoding.UTF8.GetString(decryptedBytes);
return decryptedData.ToString();
}
finally
{
rsa.PersistKeyInCsp = false;
}
}
}
private static string GenerateTestString()
{
Guid opportinityId = Guid.NewGuid();
Guid systemUserId = Guid.NewGuid();
string currentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
StringBuilder sb = new StringBuilder();
sb.AppendFormat("opportunityid={0}", opportinityId.ToString());
sb.AppendFormat("&systemuserid={0}", systemUserId.ToString());
sb.AppendFormat("&currenttime={0}", currentTime);
return sb.ToString();
}
}
}
@Xtremrules
Copy link

How will I encrypt if only the public key was sent and its in string format rather than xml
eg

KJKJHGUJHGBHJHeMA0GCDW<MNDSSD/XUOSSdfgsdgdfgsdfgsdfgsl+6N6OGDSEFSEFEFEEEFFSD9qJ10q4nW
XyK0pnlcyzxrLXXahp7oSS1dH7s8UC1asgwdasdgsdfgz1eNRLY4zN
aZvi18lppsdfgsdf4545sdgfsdgdhdfsgdrg563456/sdfgdls4j/1asdgwgqw5fgdsgSwrgwr4ghwY4my41DuLmcJ8XvRpAW
Kh8g5jsbyaBpzBA5AgMBAAE=

@anand1302
Copy link

Please share how we can use public key for decryption

@aaronhance
Copy link

Please share how we can use public key for decryption

You don't. Public keys are only for encryption.

@fabriziodb
Copy link

hi, i need to implement encryption starting from a public key in this format "-----BEGIN PUBLIC KEY-----xxxxxxxxxx"
how to change the code to set public key in that format?

thanks

@SaeedBolhasani
Copy link

SaeedBolhasani commented Apr 15, 2021

@fabrixiodb It seems you need to generate ssl certificate.
You can use OpenSSL toolkit.

hi, i need to implement encryption starting from a public key in this format "-----BEGIN PUBLIC KEY-----xxxxxxxxxx"
how to change the code to set public key in that format?

thanks

@masroore
Copy link

masroore commented May 6, 2024

There's no need for manual XML serialization. Just use ToXmlString() instead

var privateKey = cryptoServiceProvider.ToXmlString(true);
var publicKey = cryptoServiceProvider.ToXmlString(false);

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