美文网首页
C# Sha256 及 RSA 加密算法工具类

C# Sha256 及 RSA 加密算法工具类

作者: Messix_1102 | 来源:发表于2023-12-27 17:34 被阅读0次

    SHA256是一个Hash算法,跟MD5的作用是一样一样的

    RSA 就是传说中著名的非对称加密算法,因为RSA加密算法有长度限制,所以一般做签名是先用SHA256算法提取固定长度的字符串,再加密Hash字符串

    C# RSA算法用公钥加密数据,然后用私钥解密。不支持私钥加密,公钥解密。但是可以用私钥加密,再用私钥解密(脑壳有问题才会这么搞)

    二话不说贴代码

    using System.Security.Cryptography;
    using System.Text;
    
    namespace Utility
    {
        /// <summary>
        /// 数据加密器
        /// </summary>
        public class DataEncryptor
        {
            /*
             RSA 加密算法
             */
    
            /// <summary>
            /// 获取RSA 密钥
            /// 下标 0 为 私钥
            /// 下标 1 为 公钥
            /// </summary>
            /// <returns></returns>
            public static string[] GetRSAKey()
            {
                RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
                string privatekey = provider.ToXmlString(true);
                string publickey = provider.ToXmlString(false);
                return new string[] { privatekey, publickey };
            }
    
            /// <summary>
            /// RSA 加密
            /// </summary>
            /// <param name="plainText"></param>
            /// <param name="publicKey"></param>
            /// <returns></returns>
            public static string RSAEncrypt(string plainText, string publicKey)
            {
                byte[] buffer = Encoding.UTF8.GetBytes(plainText);
                RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
                provider.FromXmlString(publicKey);
                byte[] output = provider.Encrypt(buffer, false);
                return Convert.ToBase64String(output);
            }
    
            /// <summary>
            /// RSA 解密
            /// </summary>
            /// <param name="encryptedText"></param>
            /// <param name="privateKey"></param>
            /// <returns></returns>
            public static string RSADecrypt(string encryptedText, string privateKey)
            {
                byte[] buffer = Convert.FromBase64String(encryptedText);
                RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
                provider.FromXmlString(privateKey);
                byte[] output = provider.Decrypt(buffer, false);
                return Encoding.UTF8.GetString(output);
            }
    
            /// <summary>
            /// SHA256 提取字符串Hash
            /// </summary>
            /// <returns></returns>
            public static string SHA256Encryptor(string message)
            {
                byte[] bytes = Encoding.UTF8.GetBytes(message);
                byte[] hash = SHA256.Create().ComputeHash(bytes);
    
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < hash.Length; i++)
                {
                    builder.Append(hash[i].ToString("x2"));
                }
                return builder.ToString();
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:C# Sha256 及 RSA 加密算法工具类

          本文链接:https://www.haomeiwen.com/subject/bqarndtx.html