25 lines
946 B
C#
25 lines
946 B
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
using (RSA rsa = RSA.Create())
|
|
{
|
|
rsa.KeySize = 2048;
|
|
|
|
// Export the public key
|
|
var publicKey = rsa.ExportRSAPublicKey();
|
|
Console.WriteLine("Public Key: " + Convert.ToBase64String(publicKey));
|
|
|
|
// Export the private key
|
|
var privateKey = rsa.ExportRSAPrivateKey();
|
|
Console.WriteLine("Private Key: " + Convert.ToBase64String(privateKey));
|
|
|
|
var message = "Some secret message";
|
|
Console.WriteLine("Original message: " + message);
|
|
|
|
// There are some of the paddings, but most common is PKCS
|
|
var encryptedBytes = rsa.Encrypt(Encoding.UTF8.GetBytes(message), RSAEncryptionPadding.Pkcs1);
|
|
Console.WriteLine("Encrypted message: " + Convert.ToBase64String(encryptedBytes));
|
|
|
|
var decryptedBytes = rsa.Decrypt(encryptedBytes, RSAEncryptionPadding.Pkcs1);
|
|
Console.WriteLine("Decrypted message: " + Encoding.UTF8.GetString(decryptedBytes));
|
|
} |