Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Security.Cryptography;
- using System.Text;
- namespace ConsoleApp1
- {
- class Program
- {
- private static RSAParameters PublicKey { get; set; }
- private static RSAParameters PrivateKey { get; set; }
- static void Main(string[] args)
- {
- GenerateKey();
- var message = "this is a test";
- byte[] encrypted = Encrypt(Encoding.UTF8.GetBytes(message));
- byte[] decrypted = Decrypt(encrypted);
- Console.WriteLine("Original: " + message + "\n");
- Console.WriteLine("Encrypted: " + BitConverter.ToString(encrypted) + "\n");
- Console.WriteLine("Decrypted: " + Encoding.UTF8.GetString(decrypted));
- Console.ReadLine();
- }
- public static void GenerateKey()
- {
- using (var rsa = new RSACryptoServiceProvider(2048))
- {
- rsa.PersistKeyInCsp = false;
- PublicKey = rsa.ExportParameters(false);
- PrivateKey = rsa.ExportParameters(true);
- }
- }
- public static byte[] Encrypt(byte[] input)
- {
- byte[] encrypted;
- using (var rsa = new RSACryptoServiceProvider(2048))
- {
- rsa.PersistKeyInCsp = false;
- rsa.ImportParameters(PublicKey);
- encrypted = rsa.Encrypt(input, true);
- }
- return encrypted;
- }
- public static byte[] Decrypt(byte[] input)
- {
- byte[] decrypted;
- using (var rsa = new RSACryptoServiceProvider(2048))
- {
- rsa.PersistKeyInCsp = false;
- rsa.ImportParameters(PrivateKey);
- decrypted = rsa.Decrypt(input, true);
- }
- return decrypted;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement