网上搜索过,但加密出来的结果 跟java的死活不一样,头都晕了,哪位大神可以翻译成C#版的,万分感谢啊!
java代码如下:
- Java code
public static String DESDecrypt(String data, String key, String encoding){ String edata = data; try { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(key.getBytes(encoding)); DESedeKeySpec dks = new DESedeKeySpec(convert16To24(digest.digest())); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE , securekey, new SecureRandom()); if (!data.endsWith( "=" )) { data = URLDecoder.decode(data, encoding); } edata = new String(cipher.doFinal(BASE64Decrypt(data)), encoding); } catch (Exception e) { e.printStackTrace(); } return edata;}/*** BASE64 解密** @param key* @return* @throws Exception*/public static byte [] BASE64Decrypt(String data){ byte [] edata = null ; try { edata = ( new BASE64Decoder()).decodeBuffer(data); } catch (Exception e) { e.printStackTrace(); } return edata;}/*** 把 16 位凑成 24 位数组。** @param okey* @return*/private static byte [] convert16To24(byte [] okey){ byte [] dkey = new byte [24]; for (int i = 0; i < dkey. length ; i++) { dkey[i] = okey[i % 16]; } return dkey;}
------解决方案--------------------
http://www.cnblogs.com/weekzero/archive/2007/12/05/983143.html
------解决方案--------------------
c/c++也可以啊
------解决方案--------------------
路过...
------解决方案--------------------
引用一楼
des是常用的对称加密解密方法,下面是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);