C#下简答的加密算法

来源:互联网 发布:九天封神炼体升阶数据 编辑:程序博客网 时间:2024/06/03 07:58
using System.Security.Cryptography; //加密 public static String EncryptDES(String str_Source) { String encryptKey = "Oyea"; DESCryptoServiceProvider descsp = new DESCryptoServiceProvider(); byte[] key = Encoding.Unicode.GetBytes(encryptKey); byte[] data = Encoding.Unicode.GetBytes(str_Source); System.IO.MemoryStream MStream = new System.IO.MemoryStream(); CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write); CStream.Write(data, 0, data.Length); CStream.FlushFinalBlock(); return Convert.ToBase64String(MStream.ToArray()); } //解密 public static String DecryptDES(String str_Source) { String encryptKey = "Oyea"; DESCryptoServiceProvider descsp = new DESCryptoServiceProvider(); byte[] key = Encoding.Unicode.GetBytes(encryptKey); byte[] data = Convert.FromBase64String(str_Source); System.IO.MemoryStream MStream = new System.IO.MemoryStream(); CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(key,key), CryptoStreamMode.Write); CStream.Write(data, 0, data.Length); CStream.FlushFinalBlock(); return Encoding.Unicode.GetString(MStream.ToArray()); } 

原创粉丝点击