随笔- 79  文章- 0  评论- 19  阅读- 11万 

无废话,直接上工具类

复制代码
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   Lucien.Bao  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示