这篇文章主要介绍了C#加解密之DES算法怎么实现的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇C#加解密之DES算法怎么实现文章都会有所收获,下面我们一起来看看吧。
使用DES加密方式加解密文本数据
开发工具:Visual Studio 2013
.NET Framework版本:4.5
public class DesUtil { /// <summary> /// DES加密 /// </summary> /// <param name="desModel"></param> /// <returns></returns> public static byte[] Encrypt(DesModel desModel) { //使用8位密钥 byte[] key8 = new byte[8]; //如果我们的密钥不是8为,则自动补全到8位 byte[] byteKey = Encoding.UTF8.GetBytes(desModel.Key.PadRight(key8.Length)); //复制密钥 Array.Copy(byteKey, key8, key8.Length); //使用8位向量 byte[] iv8 = new byte[8]; //如果我们的向量不是8为,则自动补全到8位 byte[] byteIv = Encoding.UTF8.GetBytes(desModel.IV.PadRight(iv8.Length)); //复制向量 Array.Copy(byteIv, iv8, iv8.Length); // 创建加密对象 DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider(); desProvider.Mode = desModel.Mode; desProvider.Padding = desModel.Padding; desProvider.Key = key8; desProvider.IV = iv8; byte[] result = null; try { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream EncryptStream = new CryptoStream(ms, desProvider.CreateEncryptor(), CryptoStreamMode.Write)) { EncryptStream.Write(desModel.Data, 0, desModel.Data.Length); EncryptStream.FlushFinalBlock(); result = ms.ToArray(); } } } catch { } return result; } /// <summary> /// DES解密 /// </summary> /// <param name="desModel"></param> /// <returns></returns> public static byte[] Decrypt(DesModel desModel) { //使用8位密钥 byte[] key8 = new byte[8]; //如果我们的密钥不是8为,则自动补全到8位 byte[] byteKey = Encoding.UTF8.GetBytes(desModel.Key.PadRight(key8.Length)); //复制密钥 Array.Copy(byteKey, key8, key8.Length); //使用8位向量 byte[] iv8 = new byte[8]; //如果我们的向量不是8为,则自动补全到8位 byte[] byteIv = Encoding.UTF8.GetBytes(desModel.IV.PadRight(iv8.Length)); //复制向量 Array.Copy(byteIv, iv8, iv8.Length); // 创建解密对象 DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider(); desProvider.Mode = desModel.Mode; desProvider.Padding = desModel.Padding; desProvider.Key = key8; desProvider.IV = iv8; byte[] result = null; try { using (MemoryStream ms = new MemoryStream(desModel.Data)) { using (CryptoStream DecryptStream = new CryptoStream(ms, desProvider.CreateDecryptor(), CryptoStreamMode.Read)) { using (MemoryStream msResult = new MemoryStream()) { byte[] temp = new byte[1024 * 1024]; int len = 0; while ((len = DecryptStream.Read(temp, 0, temp.Length)) > 0) { msResult.Write(temp, 0, len); } result = msResult.ToArray(); } } } } catch { } return result; } /// <summary> /// DES加密字符串 /// </summary> /// <param name="data"></param> /// <param name="key"></param> /// <param name="iv"></param> /// <returns></returns> public static string Encrypt(string data, string key, string iv = "") { byte[] bytes = Encoding.UTF8.GetBytes(data); byte[] result = Encrypt(new DesModel { Data = bytes, Key = key, IV = iv, Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 }); if (result == null) { return ""; } return Convert.ToBase64String(result); } /// <summary> /// DES解密字符串 /// </summary> /// <param name="data"></param> /// <param name="key"></param> /// <param name="iv"></param> /// <returns></returns> public static string Decrypt(string data, string key, string iv = "") { byte[] bytes = Convert.FromBase64String(data); byte[] result = Decrypt(new DesModel { Data = bytes, Key = key, IV = iv, Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 }); if (result == null) { return ""; } return Encoding.UTF8.GetString(result); } public class DesModel { /// <summary> /// 需要加密/解密的数据 /// </summary> public byte[] Data { get; set; } /// <summary> /// 密钥 /// </summary> public string Key { get; set; } /// <summary> /// 向量 /// </summary> public string IV { get; set; } /// <summary> /// 加密模式 /// </summary> public CipherMode Mode { get; set; } /// <summary> /// 填充模式 /// </summary> public PaddingMode Padding { get; set; } } }
private void btn_Des_Encrypt_Click(object sender, EventArgs e) { string result = DesUtil.Encrypt(textBox1.Text, "12345678900987654321"); textBox2.Text = result; } private void btn_Des_Decrypt_Click(object sender, EventArgs e) { string result = DesUtil.Decrypt(textBox2.Text, "12345678900987654321"); textBox1.Text = result; }
关于“C#加解密之DES算法怎么实现”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“C#加解密之DES算法怎么实现”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。