利用DES,C#加密,Java解密代码
//C#加密 /// <summary> /// 进行DES加密。 /// </summary> /// <param name="pToEncrypt">要加密的字符串。</param> /// <param name="sKey">密钥,且必须为8位。</param> /// <returns>以Base64格式返回的加密字符串。</returns> public static string Encode(string str) { string keyXml = CommonMethor.GetEncrptKeyByRpt(Contant.contantUrl+"api/sys/getEncrptKey"); XmlDocument doc = new XmlDocument(); doc.LoadXml(keyXml); XmlNode root = doc.SelectSingleNode("string");//当节点Workflow带有属性是,使用SelectSingleNode无法读取 string key = root.LastChild.InnerText; DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8)); provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8)); byte[] bytes = Encoding.GetEncoding("UTF-8").GetBytes(str); MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write); stream2.Write(bytes, 0, bytes.Length); stream2.FlushFinalBlock(); StringBuilder builder = new StringBuilder(); foreach (byte num in stream.ToArray()) { builder.AppendFormat("{0:X2}", num); } stream.Close(); return builder.ToString(); } java解密 public static void main(String[] args) { // String decryptToken = null; // String key = "JM@XBCRM"; // String token = "C36B90D474184911EE21D4330730847E50F34F57DB92D38AA083AA66064F658C"; // try // { // decryptToken = ClientUtils.decrypt(token,key); // decryptToken = java.net.URLDecoder.decode(decryptToken, "utf-8"); // System.out.println("加密串为[" + token + "],解密后为[" + decryptToken + "]"); // } catch (Exception e) // { // e.printStackTrace(); // } }