DES
Posted 火焰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DES相关的知识,希望对你有一定的参考价值。
public static void Generator(out string key) { TripleDESCryptoServiceProvider des = (TripleDESCryptoServiceProvider)TripleDESCryptoServiceProvider.Create(); key = ASCIIEncoding.ASCII.GetString(des.Key); } public static string EncryptDES(string data, string key) { byte[] dataArray = Encoding.UTF8.GetBytes(data); TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider(); ICryptoTransform desEncrypt = DES.CreateEncryptor(Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(key)); byte[] result = desEncrypt.TransformFinalBlock(dataArray, 0, dataArray.Length); string dbstr = null; for (int i = 0; i < result.Length; i++) { dbstr += result[i].ToString("X2"); } return dbstr; } public static string DecryptDES(string data, string key) { data = data.Replace(" ", ""); if ((data.Length % 2) != 0) data += " "; byte[] dataArray = new byte[data.Length / 2]; for (int i = 0; i < dataArray.Length; i++) { dataArray[i] = Convert.ToByte(data.Substring(i * 2, 2), 16); } TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider(); ICryptoTransform desencrypt = DES.CreateDecryptor(Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(key)); byte[] result = desencrypt.TransformFinalBlock(dataArray, 0, dataArray.Length); return Encoding.UTF8.GetString(result); }
以上是关于DES的主要内容,如果未能解决你的问题,请参考以下文章