ASP.NET Core 6 数据加解密与哈希

蓝色水晶之恋 2024-03-12 ⋅ 26 阅读

引言

在现代的Web应用程序开发中,保护用户数据的安全性是至关重要的一环。ASP.NET Core 6为我们提供了一些内置的功能,帮助我们加密和解密数据,以及使用哈希算法进行加密验证。本文将介绍ASP.NET Core 6中数据加解密和哈希的用法。

数据加解密

对称加密

对称加密是一种加解密过程使用相同密钥的方式。这意味着同一个密钥用于对数据进行加密和解密。ASP.NET Core 6提供了一些对称加密算法的实现,例如AES和DES算法。

在ASP.NET Core 6中,我们可以使用System.Security.Cryptography命名空间下的类进行对称加密的操作。以下是一个使用AES算法进行对称加密的示例:

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

public static class Encryption
{
    private static readonly byte[] Key = new byte[32]
    {
        0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
        0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
        0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
        0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
    };

    public static string Encrypt(string plainText)
    {
        using (Aes aes = Aes.Create())
        {
            aes.Key = Key;
            aes.Mode = CipherMode.CBC;

            ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                {
                    byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
                    cryptoStream.Write(plainBytes, 0, plainBytes.Length);
                    cryptoStream.FlushFinalBlock();

                    byte[] cipherBytes = memoryStream.ToArray();
                    return Convert.ToBase64String(cipherBytes);
                }
            }
        }
    }

    public static string Decrypt(string cipherText)
    {
        using (Aes aes = Aes.Create())
        {
            aes.Key = Key;
            aes.Mode = CipherMode.CBC;

            byte[] cipherBytes = Convert.FromBase64String(cipherText);
            byte[] iv = new byte[aes.IV.Length];
            Array.Copy(cipherBytes, iv, iv.Length);

            ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, iv);

            using (MemoryStream memoryStream = new MemoryStream(cipherBytes, iv.Length, cipherBytes.Length - iv.Length))
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                {
                    byte[] plainBytes = new byte[cipherBytes.Length - iv.Length];
                    int decryptedByteCount = cryptoStream.Read(plainBytes, 0, plainBytes.Length);

                    return Encoding.UTF8.GetString(plainBytes, 0, decryptedByteCount);
                }
            }
        }
    }
}

非对称加密

非对称加密使用成对的密钥,其中一个用于加密数据,另一个用于解密数据。常见的非对称加密算法有RSA和DSA。与对称加密不同,非对称加密中的密钥分为公钥和私钥。

在ASP.NET Core 6中,我们可以使用System.Security.Cryptography.RSA类进行非对称加密的操作。以下是一个使用RSA算法进行非对称加密的示例:

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

public static class Encryption
{
    public static string Encrypt(string plainText)
    {
        using (RSA rsa = RSA.Create())
        {
            byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
            byte[] cipherBytes = rsa.Encrypt(plainBytes, RSAEncryptionPadding.OaepSHA256);

            return Convert.ToBase64String(cipherBytes);
        }
    }

    public static string Decrypt(string cipherText)
    {
        using (RSA rsa = RSA.Create())
        {
            byte[] cipherBytes = Convert.FromBase64String(cipherText);
            byte[] plainBytes = rsa.Decrypt(cipherBytes, RSAEncryptionPadding.OaepSHA256);

            return Encoding.UTF8.GetString(plainBytes);
        }
    }
}

哈希

哈希算法

哈希算法将输入数据转换成固定长度的唯一字符串,这个唯一字符串通常被称为哈希值或摘要。哈希算法是单向的,无法通过哈希值得到原始输入数据。

在ASP.NET Core 6中,我们可以使用System.Security.Cryptography命名空间下的类进行哈希的操作。以下是一个使用SHA256算法进行哈希的示例:

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

public static class Hash
{
    public static string ComputeHash(string input)
    {
        using (SHA256 sha256 = SHA256.Create())
        {
            byte[] inputBytes = Encoding.UTF8.GetBytes(input);
            byte[] hashBytes = sha256.ComputeHash(inputBytes);

            return Convert.ToBase64String(hashBytes);
        }
    }
}

结论

ASP.NET Core 6提供了丰富的功能来处理数据的加密和哈希。在保护用户数据的安全性方面,数据加密和哈希是非常重要的技术。通过使用对称加密、非对称加密和哈希算法,我们可以确保用户数据在传输和存储过程中的安全性。

希望本文对你了解ASP.NET Core 6的数据加解密和哈希有所帮助!

参考资料:


全部评论: 0

    我有话说: