DES加密
1 DES算法是对称可逆加密算法,DES是一个分组加密算法,典型的DES以64位为分组对数据加密。
2 DES常见的有ECB模式和CBC模式。ECB模式是JAVA封装的DES算法的默认模式, CBC模式是.NET封装的DES算法的默认模式。
3.1 ECB是不推荐的方式,只要Key相同时,相同的明文在不同的时候产生相同的明文,容易遭到字典攻击;
3.2 CBC由于加入了向量参数,一定程度上抵御了字典工具,但缺点也随之而来,一旦中间一个数据出错或丢失,后面的数据将受到影响;
3.3 CFB与CBC类似,好处是明文和密文不用是8bit的整数倍,中间一个数据出错,只影响后面的几个块的数据;
3.4 OFB比CFB方式,一旦一个数据出错,不会影响后面的数据,但安全性降低;
4 PKCS7填充方式:为.NET和JAVA的默认填充方式,对加密数据字节长度对8取余为r,如r大于0,则补8-r个字节,字节为8-r的值;如果r等于0,则补8个字节8。
源码:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; namespace Cryp { public static class DES { /// <summary> /// DES加密 /// </summary> /// <param name="data">要加密的字符串</param> /// <param name="key">密钥,且Length必须为8位</param> /// <param name="keyEncoding">密钥编码方式</param> /// <param name="dataEncoding">要加密字符串加载时编码方式</param> /// <param name="cipherMode">对称算法的运算模式</param> /// <param name="paddingMode">对称算法中使用的填充模式</param> /// <returns>64位编码字符串</returns> public static string DESEncrypt(string data, string key, Encoding keyEncoding, Encoding dataEncoding, CipherMode cipherMode = CipherMode.CBC, PaddingMode paddingMode = PaddingMode.PKCS7) { return Convert.ToBase64String(DESEncrypt(dataEncoding.GetBytes(data), key, keyEncoding, cipherMode, paddingMode)); } /// <summary> /// DES加密 /// </summary> /// <param name="data">要加密字节流</param> /// <param name="key">密钥,且Length必须为8位</param> /// <param name="keyEncoding">密钥编码方式</param> /// <param name="cipherMode">对称算法的运算模式</param> /// <param name="paddingMode">对称算法中使用的填充模式</param> /// <returns></returns> public static byte[] DESEncrypt(byte[] data, string key, Encoding keyEncoding, CipherMode cipherMode, PaddingMode paddingMode = PaddingMode.PKCS7) { using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { des.Key = keyEncoding.GetBytes(key); des.IV = keyEncoding.GetBytes(key); des.Mode = cipherMode; des.Padding = paddingMode; using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(data, 0, data.Length); cs.FlushFinalBlock(); } byte[] str = ms.ToArray(); return str; } } } /// <summary> /// DES解密 /// </summary> /// <param name="data">要解密的字符串</param> /// <param name="key">密钥,且Length必须为8位</param> /// <param name="keyEncoding">密钥编码方式</param> /// <param name="dataEncoding">要解密字符串加载时编码方式</param> /// <param name="cipherMode">对称算法的运算模式</param> /// <param name="paddingMode">对称算法中使用的填充模式</param> /// <returns></returns> public static string DESUnEncrypt(string data, string key, Encoding keyEncoding, Encoding dataEncoding, CipherMode cipherMode, PaddingMode paddingMode = PaddingMode.PKCS7) { return dataEncoding.GetString(DESUnEncrypt(Convert.FromBase64String(data), key, keyEncoding, cipherMode, paddingMode)); } /// <summary> /// DES解密。 /// </summary> /// <param name="data">要解密字节流</param> /// <param name="key">密钥,且Length必须为8位</param> /// <param name="keyEncoding">密钥编码方式</param> /// <param name="cipherMode">对称算法的运算模式</param> /// <param name="paddingMode">对称算法中使用的填充模式</param> /// <returns></returns> public static byte[] DESUnEncrypt(byte[] data, string key, Encoding keyEncoding, CipherMode cipherMode, PaddingMode paddingMode = PaddingMode.PKCS7) { using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { des.Key = keyEncoding.GetBytes(key); des.IV = keyEncoding.GetBytes(key); des.Mode = cipherMode; using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(data, 0, data.Length); cs.FlushFinalBlock(); } byte[] str = ms.ToArray(); return str; } } } } }