一个java的DES加解密类转换成C# (三)

2014-11-24 09:56:24 · 作者: · 浏览: 5
e64String(ms.ToArray()); ms.Close(); return str; } } /// /// des解密 /// /// 要解密的以Base64 /// 密钥,且必须为8位。默认公钥解密字符串defaultKey /// 已解密的字符串。 public static string Decrypt(string pToDecrypt, string sKey = defaultKey) { byte[] inputByteArray = Convert.FromBase64String(pToDecrypt); using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = 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.Default.GetString(ms.ToArray()); ms.Close(); return str; } }

}经测试,加密和解密的结果和java的一样。