DES 全称数据加密标准 (data encrypt standard) ;DES可以对流执行对称加密,速度很快,是常用的对称加密方法,那什么叫对称加密呢?有人曾很形象的将它比喻为:用同一把钥匙锁门,再用这把钥匙开门.也就是说用同样的密钥加解密码.如下面的我就是对"breaker"作为密钥的.
using
System.IO;
using System.Security.Cryptography;
using System.Text;
namespace ConsoleApplication1
... {
/**//// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
...{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
...{
string strTemp = Encrypt("breaker", "breakere");
Console.WriteLine(strTemp);
string strDecTemp = Decrypt("breaker", strTemp);
Console.WriteLine(strDecTemp);
}
public static String Encrypt(String publickey ,String strSource)
...{
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
PasswordDeriveBytes db = new PasswordDeriveBytes(publickey, null);// key
byte[] key = db.GetBytes(8);
MemoryStream ms = new MemoryStream();//以流方式存储数据
CryptoStream cs = new CryptoStream(ms,desc.CreateEncryptor(key, key),CryptoStreamMode.Write);
byte[] data = Encoding.UTF8.GetBytes(pwd);//取到密码的字节流
cs.Write(data, 0, data.Length);
cs.FlushFinalBlock();
byte[] res = ms.ToArray();
return Convert.ToBase64String(res);//加密后的数据
}
public static String Decrypt(String publickey, String data)
...{
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
PasswordDeriveBytes db = new PasswordDeriveBytes(publickey, null);
byte[] key = db.GetBytes(8);
MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms,desc.CreateDecryptor(key, key),CryptoStreamMode.Write);
byte[] inputByteArray = new byte[data.Length];
inputByteArray = Convert.FromBase64String(data);
cs.Write(inputByteArray, 0, inputByteArray.Length);//解密 cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
}
}
using System.Security.Cryptography;
using System.Text;
namespace ConsoleApplication1
... {
/**//// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
...{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
...{
string strTemp = Encrypt("breaker", "breakere");
Console.WriteLine(strTemp);
string strDecTemp = Decrypt("breaker", strTemp);
Console.WriteLine(strDecTemp);
}
public static String Encrypt(String publickey ,String strSource)
...{
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
PasswordDeriveBytes db = new PasswordDeriveBytes(publickey, null);// key
byte[] key = db.GetBytes(8);
MemoryStream ms = new MemoryStream();//以流方式存储数据
CryptoStream cs = new CryptoStream(ms,desc.CreateEncryptor(key, key),CryptoStreamMode.Write);
byte[] data = Encoding.UTF8.GetBytes(pwd);//取到密码的字节流
cs.Write(data, 0, data.Length);
cs.FlushFinalBlock();
byte[] res = ms.ToArray();
return Convert.ToBase64String(res);//加密后的数据
}
public static String Decrypt(String publickey, String data)
...{
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
PasswordDeriveBytes db = new PasswordDeriveBytes(publickey, null);
byte[] key = db.GetBytes(8);
MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms,desc.CreateDecryptor(key, key),CryptoStreamMode.Write);
byte[] inputByteArray = new byte[data.Length];
inputByteArray = Convert.FromBase64String(data);
cs.Write(inputByteArray, 0, inputByteArray.Length);//解密 cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
}
}