.Net对称加密解密算法之三(RC2CryptoServiceProvider)
By:Roy.LiuLast updated:2008-12-24
//加密字符串
public string Encrypting(string strSource)
{
try
{
//把字符串放到byte数组中
byte[] bytIn = System.Text.Encoding.Default.GetBytes(strSource);
//建立加密对象的密钥和偏移量对TripleDES,采取24字节或192位的密钥和初始向量
byte[] key ={ 53, 68, 57, 79, 159, 168, 157, 179, 201, 40, 0, 123, 210, 189, 167, 1 };
byte[] IV ={ 5, 13, 46, 97, 69, 109, 177, 39 };
//实例 TripleDESCryptoServiceProvider类将字符串写入流中
RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
rc2.IV = IV;
rc2.Key = key;
//加密符串
ICryptoTransform encrypto = rc2.CreateEncryptor();//实义加密转换
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
byte[] bytOut = ms.ToArray();
return System.Convert.ToBase64String(bytOut);
}
catch (Exception ex)
{
throw new Exception("在文件加密的时候出现错误!错误提示: \n" + ex.Message);
}
}
//解密字符串
public string Decrypting(string Source)
{
try
{
//将解密字符串转换成字节数组
byte[] bytIn = System.Convert.FromBase64String(Source);
//建立加密对象的密钥和偏移量对TripleDES,采取24字节或192位的密钥和初始向量
byte[] key ={ 53, 68, 57, 79, 159, 168, 157, 179, 201, 40, 0, 123, 210, 189, 167, 1 };
byte[] IV ={ 5, 13, 46, 97, 69, 109, 177, 39 };
//实例 TripleDESCryptoServiceProvider类将字符串写入流中
RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
rc2.IV = IV;
rc2.Key = key;
//解密流
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);
ICryptoTransform encrypto = rc2.CreateDecryptor();//实义解密转换
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader strd = new StreamReader(cs, Encoding.Default);
return strd.ReadToEnd();
}
catch (Exception ex)
{
throw new Exception("在文件解密的时候出现错误!错误提示: \n" + ex.Message);
}
}
public string Encrypting(string strSource)
{
try
{
//把字符串放到byte数组中
byte[] bytIn = System.Text.Encoding.Default.GetBytes(strSource);
//建立加密对象的密钥和偏移量对TripleDES,采取24字节或192位的密钥和初始向量
byte[] key ={ 53, 68, 57, 79, 159, 168, 157, 179, 201, 40, 0, 123, 210, 189, 167, 1 };
byte[] IV ={ 5, 13, 46, 97, 69, 109, 177, 39 };
//实例 TripleDESCryptoServiceProvider类将字符串写入流中
RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
rc2.IV = IV;
rc2.Key = key;
//加密符串
ICryptoTransform encrypto = rc2.CreateEncryptor();//实义加密转换
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
byte[] bytOut = ms.ToArray();
return System.Convert.ToBase64String(bytOut);
}
catch (Exception ex)
{
throw new Exception("在文件加密的时候出现错误!错误提示: \n" + ex.Message);
}
}
//解密字符串
public string Decrypting(string Source)
{
try
{
//将解密字符串转换成字节数组
byte[] bytIn = System.Convert.FromBase64String(Source);
//建立加密对象的密钥和偏移量对TripleDES,采取24字节或192位的密钥和初始向量
byte[] key ={ 53, 68, 57, 79, 159, 168, 157, 179, 201, 40, 0, 123, 210, 189, 167, 1 };
byte[] IV ={ 5, 13, 46, 97, 69, 109, 177, 39 };
//实例 TripleDESCryptoServiceProvider类将字符串写入流中
RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
rc2.IV = IV;
rc2.Key = key;
//解密流
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);
ICryptoTransform encrypto = rc2.CreateDecryptor();//实义解密转换
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader strd = new StreamReader(cs, Encoding.Default);
return strd.ReadToEnd();
}
catch (Exception ex)
{
throw new Exception("在文件解密的时候出现错误!错误提示: \n" + ex.Message);
}
}
From:一号门
Next:c# .Net操作注册表问题
COMMENTS