public class Security
{
private RSACryptoServiceProvider RSAProvider;
private string m_ContainerName;
public string ContainerName
{
get
{
return m_ContainerName;
}
set
{
m_ContainerName = value;
}
}
public Security()
{
//
// TODO: 在此处添加构造函数逻辑
//
ContainerName = "Kotei";
}
public Security(string containername)
{
ContainerName = containername;
}
public string Encrypt(string encryptString)
{
CspParameters csp = new CspParameters();
csp.KeyContainerName = ContainerName;
RSAProvider = new RSACryptoServiceProvider(csp);
byte[] encryptBytes = RSAProvider.Encrypt(ASCIIEncoding.ASCII.GetBytes(encryptString), true);
string str = "";
foreach (byte b in encryptBytes)
{
str = str + string.Format("{0:x2}", b);
}
return str;
}
public string Decrypt(string decryptString)
{
CspParameters csp = new CspParameters();
csp.KeyContainerName = ContainerName;
RSAProvider = new RSACryptoServiceProvider(csp);
int length = (decryptString.Length / 2);
byte[] decryptBytes = new byte[length];
for (int index = 0; index < length; index++)
{
string substring = decryptString.Substring(index * 2, 2);
decryptBytes[index] = Convert.ToByte(substring, 16);
}
decryptBytes = RSAProvider.Decrypt(decryptBytes, true);
return ASCIIEncoding.ASCII.GetString(decryptBytes);
}
}