public static string Decrypt(byte[] cipherText, string cnxnString) { byte[] decryptedData; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { RSAUtilityInfo info = ReadParams(cnxnString); string publicKey; string privateKey; if (info == null) // shouldn't happen { publicKey = rsa.ToXmlString(includePrivateParameters: false); privateKey = rsa.ToXmlString(includePrivateParameters: true); WriteParams(publicKey, privateKey, cnxnString); } else { publicKey = info.PublicKey; privateKey = info.PrivateKey; } rsa.FromXmlString(privateKey); decryptedData = rsa.Decrypt(cipherText, false); return(Encoding.UTF8.GetString(decryptedData)); } }
public static byte[] Encrypt(string clearText, string cnxnString) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { string publicKey; string privateKey; RSAUtilityInfo info = ReadParams(cnxnString); if (info == null) { publicKey = rsa.ToXmlString(includePrivateParameters: false); privateKey = rsa.ToXmlString(includePrivateParameters: true); WriteParams(publicKey, privateKey, cnxnString); } else { publicKey = info.PublicKey; privateKey = info.PrivateKey; } byte[] dataToEncrypt = Encoding.UTF8.GetBytes(clearText); using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) { RSA.FromXmlString(publicKey); return(RSA.Encrypt(dataToEncrypt, false)); } } }