using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; using System.Web; using System.IO; namespace XXKey { public class NewEncrypt { static byte[] deKey = Encoding.Default.GetBytes("asdfghjkasdfghjkasdfghjkasdfghjk"); static byte[] deIV = Encoding.Default.GetBytes("asdfghjkasdfghjk"); public static string Encrypt(string PlainText) { return Encrypt(PlainText, deKey, deIV); } private static string Encrypt(string PlainText, byte[] deKey, byte[] deIV) { MemoryStream ms = new MemoryStream(); //DESCryptoServiceProvider key = new DESCryptoServiceProvider(); SymmetricAlgorithm key = SymmetricAlgorithm.Create(); CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(deKey, deIV), CryptoStreamMode.Write); StreamWriter sw = new StreamWriter(encStream); sw.WriteLine(PlainText); sw.Close(); encStream.Close(); byte[] buffer = ms.ToArray(); ms.Close(); return Encoding.Default.GetString(buffer); } public static string Decrypt(string CypherText) { return Decrypt(CypherText, deKey, deIV); } private static string Decrypt(string CypherText, byte[] deKey, byte[] deIV) { MemoryStream ms = new MemoryStream(Encoding.Default.GetBytes(CypherText)); //DESCryptoServiceProvider key = new DESCryptoServiceProvider(); SymmetricAlgorithm key = SymmetricAlgorithm.Create(); CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(deKey, deIV), CryptoStreamMode.Read); StreamReader sr = new StreamReader(encStream); string val = sr.ReadLine(); sr.Close(); encStream.Close(); ms.Close(); return val; } } }
转载:http://blog.csdn.net/foreverling/article/details/32325319