Created
December 17, 2017 15:17
-
-
Save arman-hpp/94462a2f0bd4064f0cbb71556366d260 to your computer and use it in GitHub Desktop.
Encyptor
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
public static class JEncyptor | |
{ | |
public static Tuple<string, string> CreateKeyPair() | |
{ | |
var cspParams = new CspParameters { ProviderType = 1 }; | |
var rsaProvider = new RSACryptoServiceProvider(1024, cspParams); | |
var publicKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(false)); | |
var privateKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(true)); | |
return new Tuple<string, string>(privateKey, publicKey); | |
} | |
public static byte[] Encrypt(string publicKey, string data) | |
{ | |
var cspParams = new CspParameters { ProviderType = 1 }; | |
var rsaProvider = new RSACryptoServiceProvider(cspParams); | |
rsaProvider.ImportCspBlob(Convert.FromBase64String(publicKey)); | |
var plainBytes = Encoding.UTF8.GetBytes(data); | |
var encryptedBytes = rsaProvider.Encrypt(plainBytes, false); | |
return encryptedBytes; | |
} | |
public static string Decrypt(string privateKey, byte[] encryptedBytes) | |
{ | |
var cspParams = new CspParameters { ProviderType = 1 }; | |
var rsaProvider = new RSACryptoServiceProvider(cspParams); | |
rsaProvider.ImportCspBlob(Convert.FromBase64String(privateKey)); | |
var plainBytes = rsaProvider.Decrypt(encryptedBytes, false); | |
var plainText = Encoding.UTF8.GetString(plainBytes, 0, plainBytes.Length); | |
return plainText; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment