加密算法DES3
Posted weiweixiang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了加密算法DES3相关的知识,希望对你有一定的参考价值。
这里不讲原理,只有实现的简单封装
public class Des3Helper { private static readonly string _Key = "[email protected]!AIo#D5cx"; private static readonly string _Vector = "Q9Fvi$SO"; /// <summary> /// 3DES加密 /// 密码:默认 /// 偏移量:默认 /// 模式:CBC /// 填充:PKCS7 /// 输出:Base64 /// 编码:UTF8 /// </summary> /// <param name="plaintext">明文</param> /// <returns>大写的密文</returns> public static string Encrypt(string plaintext) { return Encrypt(plaintext, _Key, _Vector, OutputMethod.Base64, CipherMode.CBC, PaddingMode.PKCS7); } /// <summary> /// 3DES加密 /// 密码:默认 /// 偏移量:默认 /// 模式:CBC /// 填充:PKCS7 /// 输出:Base64 /// 编码:UTF8 /// </summary> /// <param name="plaintext">明文</param> /// <returns>大写的密文</returns> public static string Encrypt(string plaintext, OutputMethod method) { return Encrypt(plaintext, _Key, _Vector, method, CipherMode.CBC, PaddingMode.PKCS7); } /// <summary> /// 3DES加密 /// 密码:自定义 /// 偏移量:自定义 /// 模式:CBC /// 填充:PKCS7 /// 输出:Base64 /// 编码:UTF8 /// </summary> /// <param name="plaintext">明文</param> /// <param name="key">密码,8位,不含中文</param> /// <param name="iv">偏移量,8位,不含中文</param> /// <returns></returns> public static string Encrypt(string plaintext, string key, string iv) { return Encrypt(plaintext, key, iv, OutputMethod.Base64, CipherMode.CBC, PaddingMode.PKCS7); } /// <summary> /// /// </summary> /// <param name="plaintext"></param> /// <param name="key"></param> /// <param name="iv"></param> /// <param name="method"></param> /// <returns></returns> public static string Encrypt(string plaintext, string key, string iv, OutputMethod method) { return Encrypt(plaintext, key, iv, method, CipherMode.CBC, PaddingMode.PKCS7); } /// <summary> /// 3DES加密 /// 密码:自定义 /// 偏移量:自定义 /// 模式:自定义 /// 填充:自定义 /// 输出:Base64 /// 编码:UTF8 /// </summary> /// <param name="plaintext">明文</param> /// <returns>大写的密文</returns> public static string Encrypt(string plaintext, string key, string iv, OutputMethod method, CipherMode cipher, PaddingMode padding) { if (String.IsNullOrEmpty(plaintext)) { throw new ArgumentNullException("明文不能为空"); } if (String.IsNullOrEmpty(key) || key.Length != 24) { throw new ArgumentNullException("密钥参数错误"); } if (String.IsNullOrEmpty(iv) || key.Length != 8) { throw new ArgumentNullException("向量参数错误"); } byte[] byteArray = Encoding.UTF8.GetBytes(plaintext); TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider(); provider.Key = Encoding.ASCII.GetBytes(key); provider.IV = Encoding.ASCII.GetBytes(iv); provider.Mode = cipher; provider.Padding = padding; using (MemoryStream ms = new MemoryStream()) { CryptoStream cStream = new CryptoStream(ms, provider.CreateEncryptor(), CryptoStreamMode.Write); cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); byte[] result = ms.ToArray(); return EadUtil.Output(result, method); } } /// <summary> /// 3DES解密 /// 密码:默认 /// 偏移量:默认 /// 模式:CBC /// 填充:PKCS7 /// 编码:UTF8 /// </summary> /// <param name="ciphertext">密文</param> /// <returns>返回明文</returns> public static string Decrypt(string ciphertext) { return Decrypt(ciphertext, _Key, _Vector, OutputMethod.Base64, CipherMode.CBC, PaddingMode.PKCS7); } /// <summary> /// 3DES解密 /// 密码:默认 /// 偏移量:默认 /// 模式:CBC /// 填充:PKCS7 /// 编码:UTF8 /// </summary> /// <param name="ciphertext">密文</param> /// <returns>返回明文</returns> public static string Decrypt(string ciphertext, OutputMethod method) { return Decrypt(ciphertext, _Key, _Vector, method, CipherMode.CBC, PaddingMode.PKCS7); } /// <summary> /// 3DES解密 /// 密码:自定义 /// 偏移量:自定义 /// 模式:CBC /// 填充:PKCS7 /// 编码:UTF8 /// </summary> /// <param name="ciphertext">密文</param> /// <param name="key">密码,8位,不含中文</param> /// <param name="iv">偏移量,8位,不含中文</param> /// <returns>返回明文</returns> public static string Decrypt(string ciphertext, string key, string iv) { return Decrypt(ciphertext, key, iv, OutputMethod.Base64, CipherMode.CBC, PaddingMode.PKCS7); } /// <summary> /// 3DES解密 /// 密码:自定义 /// 偏移量:自定义 /// 模式:CBC /// 填充:PKCS7 /// 编码:UTF8 /// </summary> /// <param name="ciphertext">密文</param> /// <param name="key">密码,8位,不含中文</param> /// <param name="iv">偏移量,8位,不含中文</param> /// <returns>返回明文</returns> public static string Decrypt(string ciphertext, string key, string iv, OutputMethod method) { return Decrypt(ciphertext, key, iv, method, CipherMode.CBC, PaddingMode.PKCS7); } /// <summary> /// 3DES解密 /// 密码:自定义 /// 偏移量:自定义 /// 模式:自定义 /// 填充:自定义 /// 输出:HEX /// 编码:UTF8 /// </summary> /// <param name="ciphertext"></param> /// <param name="key"></param> /// <param name="iv"></param> /// <param name="cipher"></param> /// <param name="padding"></param> /// <returns></returns> public static string Decrypt(string ciphertext, string key, string iv, OutputMethod method, CipherMode cipher, PaddingMode padding) { if (String.IsNullOrEmpty(ciphertext)) { throw new ArgumentNullException("密文不能为空"); } if (String.IsNullOrEmpty(key) || key.Length != 24) { throw new ArgumentNullException("密钥参数错误"); } if (String.IsNullOrEmpty(iv) || key.Length != 8) { throw new ArgumentNullException("向量参数错误"); } byte[] buffer = EadUtil.Input(ciphertext, method); TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider(); provider.Key = Encoding.ASCII.GetBytes(key); provider.IV = Encoding.ASCII.GetBytes(iv); provider.Mode = cipher; provider.Padding = padding; using (MemoryStream ms = new MemoryStream()) { CryptoStream cStream = new CryptoStream(ms, provider.CreateDecryptor(), CryptoStreamMode.Write); cStream.Write(buffer, 0, buffer.Length); cStream.FlushFinalBlock(); ms.Close(); return Encoding.UTF8.GetString(ms.ToArray()); } } }
以上是关于加密算法DES3的主要内容,如果未能解决你的问题,请参考以下文章
区块链教程区块链背后的信息安全2DES3DES加密算法原理一