2 using System.Security.Cryptography;
5 namespace SpeedportHybridControl.Implementations
7 public static class Cryptography
9 private static string GetKeyFromContainer()
11 // store key in keycontainer, this generates a new key if none exist
12 CspParameters cp = new CspParameters();
13 cp.KeyContainerName = "SpeedportHybridControl";
14 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048, cp);
15 string result = rsa.ToXmlString(true);
21 public static string Encrypt(string clearText)
23 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
24 byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
25 rsa.FromXmlString(GetKeyFromContainer());
26 string result = Convert.ToBase64String(rsa.Encrypt(clearBytes, true));
32 public static string Decrypt(string cipherText)
34 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
35 byte[] cipherBytes = Convert.FromBase64String(cipherText);
36 rsa.FromXmlString(GetKeyFromContainer());
37 string result = Encoding.Unicode.GetString(rsa.Decrypt(cipherBytes, true));