public class CommonTools
{
private static String EncodeKey="Simon";
public static Object Copy(Object source, Object item)
{
Type t = source.GetType();
foreach (PropertyInfo property in t.GetProperties())
{
PropertyInfo pro = t.GetProperty(property.Name);
if (pro != null && property.PropertyType == pro.PropertyType && pro.CanWrite && property.CanRead)
{
pro.SetValue(item, property.GetValue(source, null), null);
}
}
return item;
}
public static String Encode(String str)
{
String result = String.Empty;
CspParameters param = new CspParameters();
param.KeyContainerName =EncodeKey;//密匙容器的名称,保持加密解密一致才能解密成功
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
{
byte[] plaindata = Encoding.Default.GetBytes(str);//将要加密的字符串转换为字节数组
byte[] encryptdata = rsa.Encrypt(plaindata, false);//将加密后的字节数据转换为新的加密字节数组
result = Convert.ToBase64String(encryptdata);//将加密后的字节数组转换为字符串
}
return result;
}
public static String Decode(String str)
{
String result = String.Empty;
CspParameters param = new CspParameters();
param.KeyContainerName = EncodeKey;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
{
byte[] encryptdata = Convert.FromBase64String(str);
byte[] decryptdata = rsa.Decrypt(encryptdata, false);
result = Encoding.Default.GetString(decryptdata);
}
return result;
}
}