You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.5 KiB
57 lines
1.5 KiB
4 years ago
|
using System.Security.Cryptography;
|
||
|
using System.Text;
|
||
|
using System.IO;
|
||
|
using System;
|
||
|
|
||
|
class PrivateData
|
||
|
{
|
||
|
private RSACryptoServiceProvider RSA;
|
||
|
|
||
|
|
||
|
public PrivateData()
|
||
|
{
|
||
|
RSA = new RSACryptoServiceProvider();
|
||
|
RSA.FromXmlString(File.ReadAllText("KeyInfo.xml"));
|
||
|
}
|
||
|
|
||
|
public string EncryptString(string input)
|
||
|
{
|
||
|
return Encryption(Encoding.UTF8.GetBytes(input), RSA.ExportParameters(false), false);
|
||
|
}
|
||
|
public string DecryptString(string input)
|
||
|
{
|
||
|
return Decryption(Convert.FromBase64String(input), RSA.ExportParameters(false), false);
|
||
|
}
|
||
|
private string Encryption(byte[] Data, RSAParameters RSAKey, bool DoOAEPPadding)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
Console.WriteLine(RSAKey.ToString());
|
||
|
byte[] encryptedData;
|
||
|
encryptedData = RSA.Encrypt(Data, DoOAEPPadding);
|
||
|
|
||
|
return Convert.ToBase64String(encryptedData);
|
||
|
}
|
||
|
catch (CryptographicException e)
|
||
|
{
|
||
|
Console.WriteLine(e.Message);
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public string Decryption(byte[] Data, RSAParameters RSAKey, bool DoOAEPPadding)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
byte[] decryptedData;
|
||
|
decryptedData = RSA.Decrypt(Data, DoOAEPPadding);
|
||
|
|
||
|
return Encoding.UTF8.GetString(decryptedData, 0, decryptedData.Length); ;
|
||
|
}
|
||
|
catch (CryptographicException e)
|
||
|
{
|
||
|
Console.WriteLine(e.ToString());
|
||
|
return "";
|
||
|
}
|
||
|
}
|
||
|
}
|