String - DecryptRSA
A string extension method that decrypts the string.
public static void Main() { string input = "Entity Framework"; try { string output = input.EncryptRSA("key"); //C# Extension Method: String - DecryptRSA System.Console.WriteLine(output.DecryptRSA("key")); } catch { Console.WriteLine(".NET Fiddle doesn't support KeyContainerPermission required by this method yet."); } }
View Source
using System; using System.Globalization; using System.Security.Cryptography; using System.Text; public static partial class Extensions { /// <summary> /// A string extension method that decrypt a string. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="key">The key.</param> /// <returns>The decrypted string.</returns> public static string DecryptRSA(this string @this, string key) { var cspp = new CspParameters {KeyContainerName = key}; var rsa = new RSACryptoServiceProvider(cspp) {PersistKeyInCsp = true}; string[] decryptArray = @this.Split(new[] {"-"}, StringSplitOptions.None); byte[] decryptByteArray = Array.ConvertAll(decryptArray, (s => Convert.ToByte(byte.Parse(s, NumberStyles.HexNumber)))); byte[] bytes = rsa.Decrypt(decryptByteArray, true); return Encoding.UTF8.GetString(bytes); } }