C#DES加密与第三方软件加密数据不一样
/// <summary>/// 进行DES加密。
/// </summary>
/// <param name="pToEncrypt">要加密的字符串。</param>
/// <param name="sKey">密钥,且必须为8位。</param>
/// <returns>以Base64格式返回的加密字符串。</returns>
public string Encrypt1(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
des.Padding = PaddingMode.Zeros;
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
//byte[] aa = ms.ToArray();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
ms.Close();
return ret.ToString();
}
数据为:string data = "49121205566404565800000000000000";加密密钥为: string key = "1472583690123456";
程序加密后为:"8A4173F15E02F37F7B51DDDD81E2C581”,第三方软件为:"D9932D9DA4021B68EC91C730291EC88C"
问题是,加密之后的数据与第三方正式软件加密的数据不一样,这是为什么呢?
琢磨两天没找出问题,求大神赐教!
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
这个看着像MD5的
注意不要有空格
用C#写DES加密算法
http://www.ibcibc.com/forum.php?mod=viewthread&tid=3347&fromuid=1
(出处: C#论坛-C#教程,ASP.NET教程)
看下这个
页:
[1]