由于平台需要3des加密,所以碰到了以下问题,此问题我已经弄了好几天了,并且在网上下了源码,但结果还有问题,与合作伙伴的java的3des加密的结果不同,希望大家帮忙,给我看看有什么问题没。具体要求:3des加密,编码需用gbk
代码如下:(如果哪位朋友更好的代码,请帮忙提供,小生将感激不尽)
using System;
using System.Text;
using System.Security;
using System.Security.Cryptography;namespace gdEpg
{
class test3DES
{
public static void Main()
{
string str = "12345678$http://www.tvportal.com/request.jsp";
string key = "54C1DF3BCD9BEFEA19012379D9493208346708F79B46E308";
Encoding encoding = Encoding.GetEncoding(936);
test3DES d = new test3DES();
string desStr = d.Encrypt3DES(str, key, encoding);
string strDes = d.Decrypt3DES(desStr, key);
Console.WriteLine(desStr);
Console.WriteLine(strDes);
}
/// <summary>
/// 加密
/// </summary>
/// <param name="strString">要加密字符串</param>
/// <param name="strKey">密钥</param>
/// <param name="encoding">编码方式</param>
/// <returns></returns>
public string Encrypt3DES(string strString, string strKey, Encoding encoding)
{
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
DES.Key = hashMD5.ComputeHash(encoding.GetBytes(strKey));
DES.Mode = CipherMode.ECB;
ICryptoTransform DESEncrypt = DES.CreateEncryptor();
byte[] Buffer = encoding.GetBytes(strString);
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}
/// <summary>
/// 解密
/// </summary>
/// <param name="strString">要解密字符串</param>
/// <param name="strKey">密钥</param>
/// <returns></returns>
public string Decrypt3DES(string strString, string strKey)
{
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strKey));
DES.Mode = CipherMode.ECB;
ICryptoTransform DESDecrypt = DES.CreateDecryptor();
string result = "";
try
{
byte[] Buffer = Convert.FromBase64String(strString);
result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}
catch(System.Exception e)
{
throw(new System.Exception("null", e)) ;
}
return result ;
}
}
}
代码如下:(如果哪位朋友更好的代码,请帮忙提供,小生将感激不尽)
using System;
using System.Text;
using System.Security;
using System.Security.Cryptography;namespace gdEpg
{
class test3DES
{
public static void Main()
{
string str = "12345678$http://www.tvportal.com/request.jsp";
string key = "54C1DF3BCD9BEFEA19012379D9493208346708F79B46E308";
Encoding encoding = Encoding.GetEncoding(936);
test3DES d = new test3DES();
string desStr = d.Encrypt3DES(str, key, encoding);
string strDes = d.Decrypt3DES(desStr, key);
Console.WriteLine(desStr);
Console.WriteLine(strDes);
}
/// <summary>
/// 加密
/// </summary>
/// <param name="strString">要加密字符串</param>
/// <param name="strKey">密钥</param>
/// <param name="encoding">编码方式</param>
/// <returns></returns>
public string Encrypt3DES(string strString, string strKey, Encoding encoding)
{
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
DES.Key = hashMD5.ComputeHash(encoding.GetBytes(strKey));
DES.Mode = CipherMode.ECB;
ICryptoTransform DESEncrypt = DES.CreateEncryptor();
byte[] Buffer = encoding.GetBytes(strString);
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}
/// <summary>
/// 解密
/// </summary>
/// <param name="strString">要解密字符串</param>
/// <param name="strKey">密钥</param>
/// <returns></returns>
public string Decrypt3DES(string strString, string strKey)
{
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strKey));
DES.Mode = CipherMode.ECB;
ICryptoTransform DESDecrypt = DES.CreateDecryptor();
string result = "";
try
{
byte[] Buffer = Convert.FromBase64String(strString);
result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}
catch(System.Exception e)
{
throw(new System.Exception("null", e)) ;
}
return result ;
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货