.net对称加密解密算法之四(TripleDESCryptoServiceProvider)
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 ={ 42, 16, 93, 156, 78, 4, 218, 32, 15, 167, 44, 80, 26, 20, 155, 112, 2, 94, 11, 204, 119, 35, 184, 197 };
byte[] IV ={ 55, 103, 246, 79, 36, 99, 167, 3 };
// ,42,5,62,83,184,7,209,13};
// ,145,23,200,58,173,10,121,22};
//,123,2,1,25,35,24,26,35,35,123,214,125,128,129,32,219,159,168,147,158,169};
//实例 TripleDESCryptoServiceProvider类将字符串写入流中
TripleDESCryptoServiceProvider TripleDES = new TripleDESCryptoServiceProvider();
TripleDES.IV = IV;
TripleDES.Key = key;
//加密符串
ICryptoTransform encrypto = TripleDES.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 ={ 42, 16, 93, 156, 78, 4, 218, 32, 15, 167, 44, 80, 26, 20, 155, 112, 2, 94, 11, 204, 119, 35, 184, 197 };
byte[] IV ={ 55, 103, 246, 79, 36, 99, 167, 3 };
//实例 TripleDESCryptoServiceProvider类将字符串写入流中
TripleDESCryptoServiceProvider TripleDES = new TripleDESCryptoServiceProvider();
TripleDES.IV = IV;
TripleDES.Key = key;
//解密流
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);
ICryptoTransform encrypto = TripleDES.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 ={ 42, 16, 93, 156, 78, 4, 218, 32, 15, 167, 44, 80, 26, 20, 155, 112, 2, 94, 11, 204, 119, 35, 184, 197 };
byte[] IV ={ 55, 103, 246, 79, 36, 99, 167, 3 };
// ,42,5,62,83,184,7,209,13};
// ,145,23,200,58,173,10,121,22};
//,123,2,1,25,35,24,26,35,35,123,214,125,128,129,32,219,159,168,147,158,169};
//实例 TripleDESCryptoServiceProvider类将字符串写入流中
TripleDESCryptoServiceProvider TripleDES = new TripleDESCryptoServiceProvider();
TripleDES.IV = IV;
TripleDES.Key = key;
//加密符串
ICryptoTransform encrypto = TripleDES.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 ={ 42, 16, 93, 156, 78, 4, 218, 32, 15, 167, 44, 80, 26, 20, 155, 112, 2, 94, 11, 204, 119, 35, 184, 197 };
byte[] IV ={ 55, 103, 246, 79, 36, 99, 167, 3 };
//实例 TripleDESCryptoServiceProvider类将字符串写入流中
TripleDESCryptoServiceProvider TripleDES = new TripleDESCryptoServiceProvider();
TripleDES.IV = IV;
TripleDES.Key = key;
//解密流
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);
ICryptoTransform encrypto = TripleDES.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:一号门
Previous:.Net对称加密解密算法之二(RijndaelManaged类实现)
COMMENTS