Created
March 28, 2014 19:00
-
-
Save angelobelchior/9840408 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Security.Cryptography; | |
namespace Cryptography | |
{ | |
public class RSA | |
{ | |
public static string Encrypt(string text, string key) | |
{ | |
Throw.IfIsNullOrEmpty(text); | |
Throw.IfIsNullOrEmpty(key); | |
CspParameters cspp = new CspParameters(); | |
cspp.KeyContainerName = key; | |
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspp); | |
rsa.PersistKeyInCsp = true; | |
byte[] bytes = rsa.Encrypt(System.Text.UTF8Encoding.UTF8.GetBytes(text), true); | |
return BitConverter.ToString(bytes); | |
} | |
public static string Decrypt(string text, string key) | |
{ | |
Throw.IfIsNullOrEmpty(text); | |
Throw.IfIsNullOrEmpty(key); | |
CspParameters cspp = new CspParameters(); | |
cspp.KeyContainerName = key; | |
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspp); | |
rsa.PersistKeyInCsp = true; | |
string[] decryptArray = text.Split(new string[] { "-" }, StringSplitOptions.None); | |
byte[] decryptByteArray = Array.ConvertAll<string, byte>(decryptArray, (s => Convert.ToByte(byte.Parse(s, System.Globalization.NumberStyles.HexNumber)))); | |
byte[] bytes = rsa.Decrypt(decryptByteArray, true); | |
return System.Text.UTF8Encoding.UTF8.GetString(bytes); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment