1.如果只需要简单得md5加密,这样: System.Web.Security.FormAuthentication.HashPasswordForStoringInConfigFile("你要加密得字符串","md5"); 2.如果比较复杂得加密,这里提供一个Rijndael加密算法得加密demo using System; using System.IO; using System.Text; using System.Security.Cryptography;namespace RijndaelManaged_Examples { class MyMainClass { public static void Main() { string original = "This is a much longer string of data than a public/private key algorithm will accept."; string roundtrip; ASCIIEncoding textConverter = new ASCIIEncoding(); RijndaelManaged myRijndael = new RijndaelManaged(); byte[] fromEncrypt; byte[] encrypted; byte[] toEncrypt; byte[] key; byte[] IV; //Create a new key and initialization vector. myRijndael.GenerateKey(); myRijndael.GenerateIV(); //Get the key and IV. key = myRijndael.Key; IV = myRijndael.IV; //Get an encryptor. ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV);
//Encrypt the data. MemoryStream msEncrypt = new MemoryStream(); CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write); //Convert the data to a byte array. toEncrypt = textConverter.GetBytes(original); //Write all data to the crypto stream and flush it. csEncrypt.Write(toEncrypt, 0, toEncrypt.Length); csEncrypt.FlushFinalBlock(); //Get encrypted array of bytes. encrypted = msEncrypt.ToArray(); //This is where the message would be transmitted to a recipient // who already knows your secret key. Optionally, you can // also encrypt your secret key using a public key algorithm // and pass it to the mesage recipient along with the RijnDael // encrypted message. //Get a decryptor that uses the same key and IV as the encryptor. ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, IV); //Now decrypt the previously encrypted message using the decryptor // obtained in the above step. MemoryStream msDecrypt = new MemoryStream(encrypted); CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read); fromEncrypt = new byte[encrypted.Length]; //Read the data out of the crypto stream. csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length); //Convert the byte array back into a string. roundtrip = textConverter.GetString(fromEncrypt); //Display the original data and the decrypted data. Console.WriteLine("Original: {0}", original); Console.WriteLine("Round Trip: {0}", roundtrip); } } }
System.Web.Security.FormAuthentication.HashPasswordForStoringInConfigFile("你要加密得字符串","md5");
2.如果比较复杂得加密,这里提供一个Rijndael加密算法得加密demo
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;namespace RijndaelManaged_Examples
{
class MyMainClass
{
public static void Main()
{
string original = "This is a much longer string of data than a public/private key algorithm will accept.";
string roundtrip;
ASCIIEncoding textConverter = new ASCIIEncoding();
RijndaelManaged myRijndael = new RijndaelManaged();
byte[] fromEncrypt;
byte[] encrypted;
byte[] toEncrypt;
byte[] key;
byte[] IV; //Create a new key and initialization vector.
myRijndael.GenerateKey();
myRijndael.GenerateIV(); //Get the key and IV.
key = myRijndael.Key;
IV = myRijndael.IV; //Get an encryptor.
ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV);
//Encrypt the data.
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write); //Convert the data to a byte array.
toEncrypt = textConverter.GetBytes(original); //Write all data to the crypto stream and flush it.
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock(); //Get encrypted array of bytes.
encrypted = msEncrypt.ToArray(); //This is where the message would be transmitted to a recipient
// who already knows your secret key. Optionally, you can
// also encrypt your secret key using a public key algorithm
// and pass it to the mesage recipient along with the RijnDael
// encrypted message. //Get a decryptor that uses the same key and IV as the encryptor.
ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, IV); //Now decrypt the previously encrypted message using the decryptor
// obtained in the above step.
MemoryStream msDecrypt = new MemoryStream(encrypted);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read); fromEncrypt = new byte[encrypted.Length]; //Read the data out of the crypto stream.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length); //Convert the byte array back into a string.
roundtrip = textConverter.GetString(fromEncrypt); //Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", original);
Console.WriteLine("Round Trip: {0}", roundtrip);
}
}
}
demo
public stataic string ConvertToBase64(string text)
{
Encoding textConverter=new UnicodeEncoding();
byte[] resultBuffer=textConverter.GetBytes(text);
text=Convert.ToBase64String(resultBuffer);//转换成base64字符串!
}
http://dotnet.aspx.cc/ShowDetail.aspx?id=7AE7D20A-A5DA-4303-AC2D-32046BE4D086