ASP.NET如何实现DES和MD5加密帮助类 | NET加密解密代码

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
public static class EncryptHelper
{
    // 默认DES密钥和IV(8字节长度)
    private static readonly string DefaultDESKey = "ABCDEFGH"; // 8字符
    private static readonly string DefaultDESIV = "12345678";  // 8字符
    /// <summary>
    /// DES加密(使用默认密钥)
    /// </summary>
    public static string DESEncrypt(string plainText)
    {
        return DESEncrypt(plainText, DefaultDESKey, DefaultDESIV);
    }
    /// <summary>
    /// DES加密(自定义密钥和IV)
    /// </summary>
    public static string DESEncrypt(string plainText, string key, string iv)
    {
        if (string.IsNullOrEmpty(plainText)) return null;
        using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
        {
            byte[] inputBytes = Encoding.UTF8.GetBytes(plainText);
            des.Key = Encoding.ASCII.GetBytes(key);
            des.IV = Encoding.ASCII.GetBytes(iv);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputBytes, 0, inputBytes.Length);
                    cs.FlushFinalBlock();
                }
                return Convert.ToBase64String(ms.ToArray());
            }
        }
    }
    /// <summary>
    /// DES解密(使用默认密钥)
    /// </summary>
    public static string DESDecrypt(string cipherText)
    {
        return DESDecrypt(cipherText, DefaultDESKey, DefaultDESIV);
    }
    /// <summary>
    /// DES解密(自定义密钥和IV)
    /// </summary>
    public static string DESDecrypt(string cipherText, string key, string iv)
    {
        if (string.IsNullOrEmpty(cipherText)) return null;
        using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
        {
            byte[] inputBytes = Convert.FromBase64String(cipherText);
            des.Key = Encoding.ASCII.GetBytes(key);
            des.IV = Encoding.ASCII.GetBytes(iv);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputBytes, 0, inputBytes.Length);
                    cs.FlushFinalBlock();
                }
                return Encoding.UTF8.GetString(ms.ToArray());
            }
        }
    }
    /// <summary>
    /// MD5加密(32位大写)
    /// </summary>
    public static string MD5Encrypt(string input)
    {
        if (string.IsNullOrEmpty(input)) return null;
        using (MD5 md5 = MD5.Create())
        {
            byte[] inputBytes = Encoding.UTF8.GetBytes(input);
            byte[] hashBytes = md5.ComputeHash(inputBytes);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hashBytes.Length; i++)
            {
                sb.Append(hashBytes[i].ToString("X2")); // 转换为大写十六进制
            }
            return sb.ToString();
        }
    }
}

使用示例:

// DES加密解密
string original = "Hello, World!";
string encrypted = EncryptHelper.DESEncrypt(original);
string decrypted = EncryptHelper.DESDecrypt(encrypted);
Console.WriteLine($"原始: {original}");
Console.WriteLine($"加密: {encrypted}");
Console.WriteLine($"解密: {decrypted}");
// MD5加密
string input = "password123";
string md5Hash = EncryptHelper.MD5Encrypt(input);
Console.WriteLine($"MD5哈希: {md5Hash}"); // 输出32位大写MD5

关键点说明:

  1. DES加密/解密

    ASP.NET中DES加密与解密MD5加密帮助类的实现代码

    • 使用DESCryptoServiceProvider实现
    • 支持自定义密钥和IV(必须为8字节)
    • 默认使用Base64编码输出
    • 采用UTF-8编码处理文本
  2. MD5加密

    • 生成32位大写十六进制哈希值
    • 不可逆加密(常用于密码存储)
    • 使用MD5.Create()创建实例
  3. 注意事项

    ASP.NET中DES加密与解密MD5加密帮助类的实现代码

    • 密钥安全:实际项目中不要使用硬编码密钥,应从安全配置读取
    • DES安全性:DES已被认为不够安全,建议生产环境使用AES
    • IV使用:每次加密应使用不同IV增强安全性(示例为演示固定IV)
    • MD5局限:MD5存在碰撞漏洞,重要场景建议使用SHA256

增强建议:

// 随机生成IV的增强版DES加密
public static (string Result, string IV) DESEncryptWithRandomIV(string plainText, string key)
{
    using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
    {
        des.GenerateIV(); // 自动生成随机IV
        byte[] iv = des.IV;
        // ...加密逻辑...
        return (cipherText, Convert.ToBase64String(iv));
    }
}

提示:在.NET Core中建议使用Aes代替DES,使用System.Security.Cryptography.MD5时需注意平台兼容性。

图片来源于AI模型,如侵权请联系管理员。作者:酷小编,如若转载,请注明出处:https://www.kufanyun.com/ask/289206.html

(0)
上一篇 2026年2月9日 08:26
下一篇 2026年2月9日 08:33

相关推荐

  • CDN加速服务一般一个月费用是多少?

    “CDN一般一个月多少钱?” 这是许多网站运营者和开发者在考虑加速服务时最关心的问题之一,这个问题并没有一个固定的答案,其费用跨度可以从完全免费到每月数十万元不等,CDN的价格并非一个打包好的商品,而是像水电费一样,根据您的实际使用量和服务类型来计算,要了解具体费用,我们需要深入剖析其定价构成,影响CDN价格的……

    2025年10月16日
    01110
  • 为什么在ASP.NET中不使用GridView自带的删除功能来删除一行数据?

    在ASP.NET中,虽然GridView控件自带了删除功能,但有时候我们可能需要根据具体业务需求对删除操作进行定制,默认的删除功能可能无法满足我们的需求,比如在删除一行数据时需要执行额外的逻辑处理,下面将详细介绍如何在ASP.NET中实现不使用GridView自带删除功能,手动删除一行数据的方法,数据库连接与操……

    2025年12月14日
    0880
    • 服务器间歇性无响应是什么原因?如何排查解决?

      根源分析、排查逻辑与解决方案服务器间歇性无响应是IT运维中常见的复杂问题,指服务器在特定场景下(如高并发时段、特定操作触发时)出现短暂无响应、延迟或服务中断,而非持续性的宕机,这类问题对业务连续性、用户体验和系统稳定性构成直接威胁,需结合多维度因素深入排查与解决,常见原因分析:从硬件到软件的多维溯源服务器间歇性……

      2026年1月10日
      020
  • 京瓷P5021CDN打印机更换转印带步骤详解,哪里能找到正确教程?

    京瓷P5021CDN打印机转印带更换教程准备工作在更换转印带之前,请确保您已经准备好以下工具和材料:转印带螺丝刀垫圈棉签无水酒精步骤详解关闭打印机电源,确保打印机处于正常工作状态,打开打印机前盖,找到转印带所在位置,使用螺丝刀拧下固定转印带的螺丝,并取下垫圈,拔出旧转印带,注意观察转印带的安装方向,将新转印带按……

    2025年12月6日
    01990
  • 如何在使用ASP.NET的GridView控件时准确获取当前行的索引值?

    在ASP.NET中,使用GridView控件是一种常见的方式来展示数据,GridView不仅能够显示数据,还能够提供丰富的客户端和服务器端功能,如分页、排序和编辑等,获取当前行的索引值是一个基础且实用的功能,以下是如何在ASP.NET中使用GridView获取当前行的索引值的详细步骤和示例,GridView基本……

    2025年12月13日
    01590

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注