.net 3des 解密

 

string str="XXXXXXXXXXXXXX";//需解密数据

//3des密钥

String deskey = "jlgdsgjdgdg";

//3des向量
String desiv = "sfasfsffa";

string result = Decrypt3Des(str,deskey,desiv, System.Security.Cryptography.CipherMode.CBC);//解密结果

 

#region 3des解密

/// <summary>
/// des 解密
/// </summary>
/// <param name="aStrString">加密的字符串</param>
/// <param name="aStrKey">密钥</param>
/// <param name="iv">解密矢量:只有在CBC解密模式下才适用</param>
/// <param name="mode">运算模式</param> string iv = "12345678"
/// <returns>解密的字符串</returns>
public string Decrypt3Des(string aStrString, string aStrKey, string iv, CipherMode mode = CipherMode.CBC)
{
try
{
var des = new TripleDESCryptoServiceProvider
{
// Key = Encoding.UTF8.GetBytes(aStrKey),
//Key =Convert.FromBase64String(aStrKey),
Key = Encoding.Default.GetBytes(aStrKey),
Mode = mode,
Padding = PaddingMode.PKCS7
};
if (mode == CipherMode.CBC)
{
des.IV = Encoding.UTF8.GetBytes(iv);
}
var desDecrypt = des.CreateDecryptor();
var result = "";
byte[] buffer = Convert.FromBase64String(aStrString);
result = Encoding.UTF8.GetString(desDecrypt.TransformFinalBlock(buffer, 0, buffer.Length));
return result;
}
catch (Exception e)
{
return string.Empty;
}
}
#endregion

posted on 2018-06-21 18:36  Smile向前  阅读(216)  评论(0编辑  收藏  举报