1、DES一共就有4个参数参与运作:明文、密文、密钥、向量。其中这4者的关系可以理解为:
密文=明文+密钥+向量;
明文=密文-密钥-向量;
为什么要向量这个参数呢?因为如果有一篇文章,有几个词重复,那么这个词加上密钥形成的密文,仍然会重复,这给破解者有机可乘,破解者可以根据重复的内容,猜出是什么词,然而一旦猜对这个词,那么,他就能算出密钥,整篇文章就被破解了!加上向量这个参数以后,每块文字段都会依次加上一段值,这样,即使相同的文字,加密出来的密文,也是不一样的,算法的安全性大大提高!
2、AES是美国联邦*采用的商业及*数据加密标准,预计将在未来几十年里代替DES在各个领域中得到广泛应用。AES提供128位密钥,因此,128位AES的加密强度是56位DES加密强度的1021倍还多。假设可以制造一部可以在1秒内破解DES密码的机器,那么使用这台机器破解一个128位AES密码需要大约149亿万年的时间。
3、MD5和SHA1散列通常被用于密码中,很多人称其为散列算法。散列是不可逆的,也就是没有了"解密"这个说法。
des加解密参考:http://www.faceye.net/search/110023.html
加解密只是参考:http://blog.csdn.net/z1x1c1v1/article/details/8755032
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO; namespace WinForms_Crypt
{
public partial class Form3 : Form
{
/// <summary>
/// 加密解密方式
/// </summary>
private enum CryptType
{
MD5,DES,AES
} /// <summary>
/// 当前使用的加解密方式
/// </summary>
CryptType type = CryptType.MD5; public Form3()
{
InitializeComponent(); if (type == CryptType.MD5)
{
button_Decrypt.Enabled = false;
button_Encrypt.Enabled = true;
}
else if (type == CryptType.DES)
{
button_Decrypt.Enabled = true;
button_Encrypt.Enabled = true;
}
else if (type == CryptType.AES)
{
button_Decrypt.Enabled = true;
button_Encrypt.Enabled = true;
}
} /// <summary>
/// 加密
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_Encrypt_Click(object sender, EventArgs e)
{
//明文
string MingWen = textBox_DecryptText.Text.Trim();
//加密得到的密文
string MiWen = "";
if (MingWen != "")
{
if (type == CryptType.MD5)
{
MiWen = "MD5Base64:"+MD5_Crypt.GetHashStr(MingWen);
MiWen += "\r\n MD5:" + MD5_Crypt2.MD5(MiWen);
MiWen += "\r\n SHA128:" + MD5_Crypt2.SHA128(MiWen);
MiWen += "\r\n SHA256:" + MD5_Crypt2.SHA256(MiWen);
MiWen += "\r\n SHA384:" + MD5_Crypt2.SHA384(MiWen);
MiWen += "\r\n SHA512:" + MD5_Crypt2.SHA512(MiWen);
}
else if (type == CryptType.DES)
{
MiWen = DES_Crypt.Encrypt(MingWen);
}
else if (type == CryptType.AES)
{
MiWen = AeS_Crypt.Encrypt(MingWen);
}
}
textBox_EncyptText.Text = MiWen;
} /// <summary>
/// 解密
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_Decrypt_Click(object sender, EventArgs e)
{
//密文
string MiWen = textBox_EncyptText.Text.Trim();
//解密得到的明文
string MingWen = "";
//无法解析哈希值
if (MiWen != "")
{
if (type == CryptType.MD5)
{
MessageBox.Show("MD5无法解密");
}
else if (type == CryptType.DES)
{
MingWen = DES_Crypt.Decrypt(MiWen);
}
else if (type == CryptType.AES)
{
MingWen = AeS_Crypt.Decrypt(MiWen); }
}
textBox_DecryptText.Text = MingWen;
}
} /// <summary>
/// MD5加密
/// </summary>
public class MD5_Crypt
{
/// <summary>
/// MD5加密
/// </summary>
/// <param name="inputByt">原文</param>
/// <returns>加密后的字节数组</returns>
public static byte[] GetHash(byte[] inputByt)
{
HashAlgorithm algorithm = HashAlgorithm.Create("MD5");
return algorithm.ComputeHash(inputByt);
}
/// <summary>
/// MD5加密
/// </summary>
/// <param name="inputStr">原文</param>
/// <returns>加密后的字节数组</returns>
public static byte[] GetHash(string inputStr)
{
byte[] inputByt = new UnicodeEncoding().GetBytes(inputStr); return GetHash(inputByt);
}
/// <summary>
/// MD5加密
/// </summary>
/// <param name="inputStr">原文</param>
/// <returns>BASE64编码字符串</returns>
public static string GetHashStr(string inputStr)
{
return Convert.ToBase64String(GetHash(inputStr));
} /// <summary>
/// MD5加密
/// </summary>
/// <param name="inputStr">原文</param>
/// <returns>32位编码</returns>
public static string GetHashString32(string inputStr)
{
string hash = BitConverter.ToString(GetHash(inputStr));
return hash.Replace("-", "");
} /// <summary>
/// MD5加密
/// </summary>
/// <param name="inputStr">原文</param>
/// <returns>32位编码</returns>
public static string GetHashString(byte[] inputStr)
{
string hash = BitConverter.ToString(GetHash(inputStr));
return hash.Replace("-", "");
}
} /// <summary>
/// DES加解密
/// </summary>
public class DES_Crypt
{
/// <summary>
/// 获取密钥
/// </summary>
private static string Key
{
get { return @"P@+#wG%A"; }
} /// <summary>
/// 获取向量
/// </summary>
private static string IV
{
get { return @"L*n67}G\Mk@k%:~Y"; }
} /// <summary>
/// DES加密
/// </summary>
/// <param name="plainStr">明文字符串</param>
/// <returns>密文</returns>
public static string Encrypt(string plainStr)
{
byte[] bKey = Encoding.UTF8.GetBytes(Key);
byte[] bIV = Encoding.UTF8.GetBytes(IV);
byte[] byteArray = Encoding.UTF8.GetBytes(plainStr); string encrypt = null;
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
try
{
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
{
cStream.Write(byteArray, , byteArray.Length);
cStream.FlushFinalBlock();
encrypt = Convert.ToBase64String(mStream.ToArray());
}
}
}
catch { }
des.Clear();
return encrypt;
} /// <summary>
/// DES解密
/// </summary>
/// <param name="encryptStr">密文字符串</param>
/// <returns>明文</returns>
public static string Decrypt(string encryptStr)
{
byte[] bKey = Encoding.UTF8.GetBytes(Key);
byte[] bIV = Encoding.UTF8.GetBytes(IV);
byte[] byteArray = Convert.FromBase64String(encryptStr); string decrypt = null;
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
try
{
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, des.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
{
cStream.Write(byteArray, , byteArray.Length);
cStream.FlushFinalBlock();
decrypt = Encoding.UTF8.GetString(mStream.ToArray());
}
}
}
catch { }
des.Clear();
return decrypt;
}
} /// <summary>
/// AES加解密
/// </summary>
public class AeS_Crypt
{
/// <summary>
/// 获取密钥
/// </summary>
private static string Key
{
get { return @"qO[NB]6,YF}gefcaj{+oESb9d8>Z'e9M"; }
} /// <summary>
/// 获取向量
/// </summary>
private static string IV
{
get { return @"L+\~f4.Ir)b$=pkf"; }
} /// <summary>
/// AES加密
/// </summary>
/// <param name="plainStr">明文字符串</param>
/// <returns>密文</returns>
public static string Encrypt(string plainStr)
{
byte[] bKey = Encoding.UTF8.GetBytes(Key);
byte[] bIV = Encoding.UTF8.GetBytes(IV);
byte[] byteArray = Encoding.UTF8.GetBytes(plainStr); string encrypt = null;
Rijndael aes = Rijndael.Create();
try
{
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
{
cStream.Write(byteArray, , byteArray.Length);
cStream.FlushFinalBlock();
encrypt = Convert.ToBase64String(mStream.ToArray());
}
}
}
catch { }
aes.Clear();
return encrypt;
} /// <summary>
/// AES解密
/// </summary>
/// <param name="encryptStr">密文字符串</param>
/// <returns>明文</returns>
public static string Decrypt(string encryptStr)
{
byte[] bKey = Encoding.UTF8.GetBytes(Key);
byte[] bIV = Encoding.UTF8.GetBytes(IV);
byte[] byteArray = Convert.FromBase64String(encryptStr); string decrypt = null;
Rijndael aes = Rijndael.Create();
try
{
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
{
cStream.Write(byteArray, , byteArray.Length);
cStream.FlushFinalBlock();
decrypt = Encoding.UTF8.GetString(mStream.ToArray());
}
}
}
catch { }
aes.Clear();
return decrypt;
}
} public class MD5_Crypt2
{
//16字节,128位
public static string MD5(string str)
{
byte[] buffer = Encoding.UTF8.GetBytes(str);
MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
byte[] byteArr = MD5.ComputeHash(buffer);
return BitConverter.ToString(byteArr);
} //20字节,160位
public static string SHA128(string str)
{
byte[] buffer = Encoding.UTF8.GetBytes(str);
SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();
byte[] byteArr = SHA1.ComputeHash(buffer);
return BitConverter.ToString(byteArr);
} //32字节,256位
public static string SHA256(string str)
{
byte[] buffer = Encoding.UTF8.GetBytes(str);
SHA256CryptoServiceProvider SHA256 = new SHA256CryptoServiceProvider();
byte[] byteArr = SHA256.ComputeHash(buffer);
return BitConverter.ToString(byteArr);
} //48字节,384位
public static string SHA384(string str)
{
byte[] buffer = Encoding.UTF8.GetBytes(str);
SHA384CryptoServiceProvider SHA384 = new SHA384CryptoServiceProvider();
byte[] byteArr = SHA384.ComputeHash(buffer);
return BitConverter.ToString(byteArr);
} //64字节,512位
public static string SHA512(string str)
{
byte[] buffer = Encoding.UTF8.GetBytes(str);
SHA512CryptoServiceProvider SHA512 = new SHA512CryptoServiceProvider();
byte[] byteArr = SHA512.ComputeHash(buffer);
return BitConverter.ToString(byteArr);
}
}
}