日期:2014-05-19  浏览次数:20748 次

求加密解密算法
如题.

------解决方案--------------------
需要8位关键码,希望对你有用:


/// <summary>
/// Encrypt the string
/// Attention:key must be 8 bits
/// </summary>
/// <param name= "strText "> string </param>
/// <param name= "strEncrKey "> key </param>
/// <returns> </returns>
public string DesEncrypt(string strText, string strEncrKey)
{
byte[] byKey = null;
byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());


}
catch (System.Exception error)
{
// log.Error(error.Message);
throw error;
}
}
/// <summary>
/// Decrypt string
/// Attention:key must be 8 bits
/// </summary>
/// <param name= "strText "> Decrypt string </param>
/// <param name= "sDecrKey "> key </param>
/// <returns> output string </returns>
public string DesDecrypt(string strText, string sDecrKey)
{
byte[] byKey = null;
byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] inputByteArray = new Byte[strText.Length];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetString(ms.ToArray());
}
catch (System.Exception error)
{
//log.Error(error.Message);
throw error;
}
}
------解决方案--------------------
using System.Security.Cryptography

see msdn