C#中使用DESCryptoServiceProvider已过时的解决办法

 使用DESCryptoServiceProvider进行文件的加密和解密,升级到.NET6之后,Visual Studio 2022警告已过时,百度之后也没有很好的方法。如下图:

自己看了微软的文档之后琢磨了一下才解决,已验证OK.

只需将:

var des = new DESCryptoServiceProvider();

改为:

var des = DES.Create();

附上完整的文件加/解密,基本可以满足简单的日常使用(仅供参考):

using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace DESHelper;

public static class DESHelper
{
    private static readonly byte[] Key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
    private static readonly byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="plainText">加密的文本</param>
    /// <returns></returns>
    public static string Encrypt(string plainText)
    {
        byte[] inputBytes = Encoding.UTF8.GetBytes(plainText);
        using (var des = DES.Create())
        {
            using (var ms = new MemoryStream())
            {
                using (var cs = new CryptoStream(ms, des.CreateEncryptor(Key, IV), CryptoStreamMode.Write))
                {
                    cs.Write(inputBytes, 0, inputBytes.Length);
                    cs.FlushFinalBlock();
                    return Convert.ToBase64String(ms.ToArray());
                }
            }
        }
    }

    /// <summary>
    /// 解密
    /// </summary>
    /// <param name="cipherText">解密的文本</param>
    /// <returns></returns>
    public static string Decrypt(string cipherText)
    {
        byte[] inputBytes = Convert.FromBase64String(cipherText);
        using (var des = DES.Create())
        {
            using (var ms = new MemoryStream())
            {
                using (var cs = new CryptoStream(ms, des.CreateDecryptor(Key, IV), CryptoStreamMode.Write))
                {
                    cs.Write(inputBytes, 0, inputBytes.Length);
                    cs.FlushFinalBlock();
                    return Encoding.UTF8.GetString(ms.ToArray());
                }
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 .NET 6 ,可以使用 `DESCryptoServiceProvider` 类来执行 DES 加密和解密操作。以下是一个示例代码: ```csharp using System; using System.Security.Cryptography; class Program { static void Main() { using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { // 设置加密密钥和初始化向量 des.Key = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; des.IV = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; // 创建加密器对象 ICryptoTransform encryptor = des.CreateEncryptor(); // 加密数据 byte[] inputBytes = new byte[] { 0x01, 0x02, 0x03, 0x04 }; byte[] encryptedBytes = encryptor.TransformFinalBlock(inputBytes, 0, inputBytes.Length); // 创建解密器对象 ICryptoTransform decryptor = des.CreateDecryptor(); // 解密数据 byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length); // 打印结果 Console.WriteLine("加密前的数据:"); PrintBytes(inputBytes); Console.WriteLine("加密后的数据:"); PrintBytes(encryptedBytes); Console.WriteLine("解密后的数据:"); PrintBytes(decryptedBytes); } } static void PrintBytes(byte[] bytes) { foreach (byte b in bytes) { Console.Write($"{b:X2} "); } Console.WriteLine(); } } ``` 此示例演示了如何使用 `DESCryptoServiceProvider` 类进行 DES 加密和解密操作。在此示例,我们设置了加密密钥和初始化向量,并使用 `CreateEncryptor` 创建加密器对象和 `CreateDecryptor` 创建解密器对象。然后,我们可以使用加密器对象对数据进行加密,并使用解密器对象对加密后的数据进行解密。最后,我们打印了加密前、加密后和解密后的数据。请注意,这只是一个基本示例,实际应用需要根据具体需求进行适当的调整和安全性考虑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值