无废话,直接上工具类

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

namespace DZCloudServer.Core.Util
{
    public class EncryptUtil
    {
        /*
          RSA 加密算法
          */

        /// <summary>
        /// 获取RSA 密钥
        /// 下标 0 为 私钥
        /// 下标 1 为 公钥
        /// </summary>
        /// <returns></returns>
        public static string[] GetRSAKey()
        {
            RSACryptoServiceProvider provider = new();
            string privatekey = provider.ToXmlString(true);
            string publickey = provider.ToXmlString(false);
            return [privatekey, publickey];
        }

        /// <summary>
        /// 私钥加密
        /// </summary>
        /// <param name="plaintext"></param>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        public static string RSAEncrypt(string plaintext, string privateKey)
        {
            using RSACryptoServiceProvider provider = new();
            provider.FromXmlString(privateKey);

            byte[] PlaintextData = Encoding.UTF8.GetBytes(plaintext);
            int MaxBlockSize = provider.KeySize / 8 - 11;    //加密块最大长度限制

            if (PlaintextData.Length <= MaxBlockSize)
                return Convert.ToBase64String(provider.Encrypt(PlaintextData, false));

            using MemoryStream PlaiStream = new(PlaintextData);
            using MemoryStream CrypStream = new();
            byte[] Buffer = new byte[MaxBlockSize];
            int BlockSize = PlaiStream.Read(Buffer, 0, MaxBlockSize);

            while (BlockSize > 0)
            {
                byte[] ToEncrypt = new byte[BlockSize];
                Array.Copy(Buffer, 0, ToEncrypt, 0, BlockSize);

                byte[] Cryptograph = provider.Encrypt(ToEncrypt, false);
                CrypStream.Write(Cryptograph, 0, Cryptograph.Length);

                BlockSize = PlaiStream.Read(Buffer, 0, MaxBlockSize);
            }
            return Convert.ToBase64String(CrypStream.ToArray(), Base64FormattingOptions.None);
        }

        /// <summary>
        /// 公钥解密
        /// </summary>
        /// <param name="ciphertext"></param>
        /// <param name="publicKey"></param>
        /// <returns></returns>
        public static string RSADecrypt(string ciphertext, string publicKey)
        {
            using RSACryptoServiceProvider provider = new();
            provider.FromXmlString(publicKey);

            byte[] CiphertextData = Convert.FromBase64String(ciphertext);
            int MaxBlockSize = provider.KeySize / 8;    //解密块最大长度限制

            if (CiphertextData.Length <= MaxBlockSize)
                return Encoding.UTF8.GetString(provider.Decrypt(CiphertextData, false));

            using MemoryStream CrypStream = new(CiphertextData);
            using MemoryStream PlaiStream = new();
            byte[] Buffer = new byte[MaxBlockSize];
            int BlockSize = CrypStream.Read(Buffer, 0, MaxBlockSize);

            while (BlockSize > 0)
            {
                byte[] ToDecrypt = new byte[BlockSize];
                Array.Copy(Buffer, 0, ToDecrypt, 0, BlockSize);

                byte[] Plaintext = provider.Decrypt(ToDecrypt, false);
                PlaiStream.Write(Plaintext, 0, Plaintext.Length);

                BlockSize = CrypStream.Read(Buffer, 0, MaxBlockSize);
            }
            return Encoding.UTF8.GetString(PlaiStream.ToArray());
        }

        /// <summary>
        /// SHA256 提取字符串Hash
        /// </summary>
        /// <returns></returns>
        public static string SHA256Encryptor(string message)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(message);
            byte[] hash = SHA256.HashData(bytes);

            var builder = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                builder.Append(hash[i].ToString("x2"));
            }
            return builder.ToString();
        }
    }
}

 

 posted on 2024-12-11 09:48  Lucien.Bao  阅读(4)  评论(0编辑  收藏  举报