使用DES加密算法,VB6.0与C#的密文不一致 使用DES加密算法在VB6.0与C#下的加密后,密文不一致。哪位大侠,能指点! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 肯定不一样,c#/// <summary> /// 进行DES加密。 /// </summary> /// <param name="pToEncrypt">要加密的字符串。</param> /// <param name="sKey">密钥,且必须为8位。</param> /// <returns>以Base64格式返回的加密字符串。</returns> public string Encrypt(string pToEncrypt, string sKey) { using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt); des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); System.IO.MemoryStream ms = new System.IO.MemoryStream(); using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); cs.Close(); } string str = Convert.ToBase64String(ms.ToArray()); ms.Close(); return str; } } /**/ /// <summary> /// 进行DES解密。 /// </summary> /// <param name="pToDecrypt">要解密的以Base64</param> /// <param name="sKey">密钥,且必须为8位。</param> /// <returns>已解密的字符串。</returns> //public string Decrypt(string pToDecrypt, string sKey) //{ // byte[] inputByteArray = Convert.FromBase64String(pToDecrypt); // using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) // { // des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); // des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); // System.IO.MemoryStream ms = new System.IO.MemoryStream(); // using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)) // { // cs.Write(inputByteArray, 0, inputByteArray.Length); // cs.FlushFinalBlock(); // cs.Close(); // } // string str = Encoding.UTF8.GetString(ms.ToArray()); // ms.Close(); // return str; // } //} 楼主你的问题解决了没!请指点呀,我这了正需要呀vb6 和c# des加密 能否从数据库表中查询出该表某列不同取值的个数以及值分别是什么? C#自定义事件的问题 请问哪位有能根据写好的程序文件生成jude图的软件? 新手用C#写了一个产生不同随机数的代码 如何使用C#制作一个精确计时器,计时精度到底毫秒级别或者更高 在那儿看对象啊? c# 中的string [] 怎么传递到c++ dll 函数中对应的 p** 参数? VS2008新建项目出错 C#调用C++的一个方法,声明问题 谁知道CSharpEdSetup注册吗,我装过一次,删了,再装就不行了。 急求正则表达式? 请问,在List集合中如何得到元素的索引值
c#
/// <summary>
/// 进行DES加密。
/// </summary>
/// <param name="pToEncrypt">要加密的字符串。</param>
/// <param name="sKey">密钥,且必须为8位。</param>
/// <returns>以Base64格式返回的加密字符串。</returns>
public string Encrypt(string pToEncrypt, string sKey)
{
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = Convert.ToBase64String(ms.ToArray());
ms.Close();
return str;
}
} /**/
/// <summary>
/// 进行DES解密。
/// </summary>
/// <param name="pToDecrypt">要解密的以Base64</param>
/// <param name="sKey">密钥,且必须为8位。</param>
/// <returns>已解密的字符串。</returns>
//public string Decrypt(string pToDecrypt, string sKey)
//{
// byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
// using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
// {
// des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
// des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
// System.IO.MemoryStream ms = new System.IO.MemoryStream();
// using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
// {
// cs.Write(inputByteArray, 0, inputByteArray.Length);
// cs.FlushFinalBlock();
// cs.Close();
// }
// string str = Encoding.UTF8.GetString(ms.ToArray());
// ms.Close();
// return str;
// }
//}