des加密(c#实现) <script src="http://blog.csdn.net/count.aspx?ID=1504367&Type=Rank" type="text/javascript"></script>
转自:http://blog.csdn.net/tyq0319/archive/2007/02/07/1504367.aspx
private string Encrypt(string datastr, string keystr)
{
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = System.Text.Encoding.ASCII.GetBytes(keystr);
byte[] data = System.Text.Encoding.Unicode.GetBytes(datastr);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);
cs.Write(data, 0, data.Length);
cs.FlushFinalBlock();
return System.Convert.ToBase64String(ms.ToArray());
}
private string Decrypt(string datastr, string keystr)
{
byte[] inputByteArray = new Byte[datastr.Length];
inputByteArray = Convert.FromBase64String(datastr);
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = System.Text.Encoding.ASCII.GetBytes(keystr);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, desc.CreateDecryptor(key, key), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.ASCIIEncoding();
return encoding.GetString(ms.ToArray());
}
//复杂加密 解密
/// <summary>
/// 一次加密
/// </summary>
/// <param name="pToEncrypt">加密内容</param>
/// <returns></returns>
private string encrypt1(string pToEncrypt)
{
string EncryptString = "";
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(secretKey1);
des.IV = ASCIIEncoding.ASCII.GetBytes(secretKey1);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
EncryptString = encrypt2(ret.ToString());
}
catch
{
EncryptString = "EncryptFailing";
}
return EncryptString;
}
/// <summary>
/// 二次加密
/// </summary>
/// <param name="pToEncrypt">加密内容</param>
/// <returns></returns>
private string encrypt2(string pToEncrypt)
{
string EncryptString = "";
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(secretKey2);
des.IV = ASCIIEncoding.ASCII.GetBytes(secretKey2);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
EncryptString = ret.ToString();
}
catch
{
EncryptString = "EncryptFailing";
}
return EncryptString;
}
/// <summary>
/// 一次解密
/// </summary>
/// <param name="pToDecrypt">解密内容</param>
/// <returns></returns>
private string Decrypt1(string pToDecrypt)
{
string EncryptString = "";
MemoryStream ms = new MemoryStream();
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(secretKey2);
des.IV = ASCIIEncoding.ASCII.GetBytes(secretKey2);
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
EncryptString = Decrypt2(System.Text.Encoding.Default.GetString(ms.ToArray()));
}
catch
{
EncryptString = "DecryptFailing";
}
return EncryptString;
}
/// <summary>
/// 二次解密
/// </summary>
/// <param name="pToDecrypt">解密内容</param>
/// <returns></returns>
private string Decrypt2(string pToDecrypt)
{
string EncryptString = "";
MemoryStream ms = new MemoryStream();
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(secretKey1);
des.IV = ASCIIEncoding.ASCII.GetBytes(secretKey1);
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
EncryptString = System.Text.Encoding.Default.GetString(ms.ToArray());
}
catch
{
EncryptString = "DecryptFailing";
}
return EncryptString;
}
protected void encry_Click(object sender, EventArgs e)
{
string encryText = this.TextBox1.Text.Trim();
this.decry.Text = encrypt1(encryText);
}
protected void Button1_Click(object sender, EventArgs e)
{
string decryText = this.decry.Text.Trim();
this.secretKey1 = this.TextBox3.Text;
this.Label1.Text = Decrypt1(decryText);
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = createCon();
SqlCommand cmd = new SqlCommand();
con.Open();
int t1 = DateTime.Now.Minute*60*1000+DateTime.Now.Second*1000+DateTime.Now.Millisecond;
for (int i = 0; i < 500; i++)
{
string unencryText="skfjiwpa;;PDKJEFI";
string encryText = Encrypt(unencryText, "kewidgkd");
cmd.Connection = con;
cmd.CommandText = "insert into password values('" + encryText + "')";
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
int t2 = DateTime.Now.Minute * 60 * 1000 + DateTime.Now.Second * 1000 + DateTime.Now.Millisecond;
con.Close();
this.Label5.Text = Convert.ToString(t2 - t1);
}