首页 > 其他分享 >.net通用RSA加密工具类

.net通用RSA加密工具类

时间:2023-07-28 12:45:02浏览次数:32  
标签:return RSA ReadByte 加密 byte twobytes net binr

目前最流行的加密算法莫过于RSA了,以下是我们.net/.net core C#生成环境用的RSA加密工具类,在此分享给大家。

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

namespace Common
{
    /// <summary>
    /// RSA加密工具类
    /// </summary>
    public class RSACryptoUtil
    {
        /// <summary>
        /// rsa私钥
        /// </summary>
        private static readonly RSACryptoServiceProvider _privateKeyRsaProvider;
        /// <summary>
        /// rsa公钥
        /// </summary>
        private static readonly RSACryptoServiceProvider _publicKeyRsaProvider;

        static RSACryptoUtil()
        {
            //string root = AppDomain.CurrentDomain.RelativeSearchPath;

            string publicKey = @"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqc7Mgsct5yaU+Ihxwz28luI/0xJe/uZQPwkMyOJpz+tDi5p9V0JYaYg3SrhIqkNQFluBBSRCnp1NNzfT3qUn93eJ1g9/8wwBloiK+W0s6DuSQj93+T8ZM1+qnnBOUiNrHBmynjR1frN4CJVmdXP1+w3CqLoMaHkw46Vy5UvEf9yedetjcCwIbmjRJzE2Mv3qQz67HbgQIDAQAB";//File.ReadAllText($"公钥位置", Encoding.ASCII);
            string privateKey = @"MIIEpAIBAAKCAQEAqc7Mgsct5yaU+Ihxwz28luI/0xJe/uZQPwkMyOJpz+tDi5p9V0JYaYg3SrhIqkNQFluBBSRCnp1NNzfT3qUn93eJ1g9/8wwBloiK+W0s6DuSQj9picqP0n5bEDxPzfd7kTM1+Cf4SZDpzz7BPDGt2qQc9bE8Cmwj3PSCu0pDo3baIxiz0sWaprYEP/YvLYaJN/5+2mQ0q+yogzSt8WthlTGbMO6IRFre467s/pQbQOzSNPMQsL5lbmtNThUlg3+T8ZM1+qnnBOUiNrHBmynjR1frN4CJVmdXP1+w3CqLoMaHkw46Vy5UvEf9yedetjcCwIbmjRJzE2Mv3qQz67HbgQI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.IsNullOrWhiteSpace(publicKey))
            {
                _publicKeyRsaProvider = CreateRsaProviderFromPublicKey(publicKey);
            }
            if (!string.IsNullOrWhiteSpace(privateKey))
            {
                _privateKeyRsaProvider = CreateRsaProviderFromPrivateKey(privateKey);
            }
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="cipherText"></param>
        /// <returns></returns>
        public static string Decrypt(string cipherText)
        {
            if (_privateKeyRsaProvider == null)
            {
                LogUtil.Logger.Error("_privateKeyRsaProvider is null");
                return string.Empty;
            }

            try
            {
                return Encoding.UTF8.GetString(_privateKeyRsaProvider.Decrypt(Convert.FromBase64String(cipherText), false));
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Error(ex, $"RSA解密失败:{cipherText}");
                return string.Empty;
            }
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string Encrypt(string text)
        {
            if (_publicKeyRsaProvider == null)
            {
                LogUtil.Logger.Error("_publicKeyRsaProvider is null");
                return string.Empty;
            }

            return Convert.ToBase64String(_publicKeyRsaProvider.Encrypt(Encoding.UTF8.GetBytes(text), false));
        }

        /// <summary>
        /// 创建rsa公钥对象
        /// </summary>
        /// <param name="publicKeyString"></param>
        /// <returns></returns>
        private static RSACryptoServiceProvider CreateRsaProviderFromPublicKey(string publicKeyString)
        {
            // encoded OID sequence for  PKCS #1 rsaEncryption szOID_RSA_RSA = "1.2.840.113549.1.1.1"
            byte[] SeqOID = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };
            byte[] x509key = Convert.FromBase64String(publicKeyString);

            // ---------  Set up stream to read the asn.1 encoded SubjectPublicKeyInfo blob  ------
            using (MemoryStream mem = new MemoryStream(x509key))
            {
                using (BinaryReader binr = new BinaryReader(mem))  //wrap Memory Stream with BinaryReader for easy reading
                {
                    ushort twobytes = binr.ReadUInt16();
                    if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
                        binr.ReadByte();    //advance 1 byte
                    else if (twobytes == 0x8230)
                        binr.ReadInt16();   //advance 2 bytes
                    else return null;

                    byte[] seq = binr.ReadBytes(15);       //read the Sequence OID
                    if (!CompareBytearrays(seq, SeqOID))    //make sure Sequence for OID is correct
                        return null;

                    twobytes = binr.ReadUInt16();
                    if (twobytes == 0x8103) //data read as little endian order (actual data order for Bit String is 03 81)
                        binr.ReadByte();    //advance 1 byte
                    else if (twobytes == 0x8203) binr.ReadInt16();   //advance 2 bytes
                    else return null;

                    byte bt = binr.ReadByte();
                    if (bt != 0x00)     //expect null byte next
                        return null;

                    twobytes = binr.ReadUInt16();
                    if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
                        binr.ReadByte();    //advance 1 byte
                    else if (twobytes == 0x8230) binr.ReadInt16();   //advance 2 bytes
                    else return null;

                    twobytes = binr.ReadUInt16();
                    byte lowbyte = 0x00;
                    byte highbyte = 0x00;

                    if (twobytes == 0x8102) //data read as little endian order (actual data order for Integer is 02 81)
                        lowbyte = binr.ReadByte();  // read next bytes which is bytes in modulus
                    else if (twobytes == 0x8202)
                    {
                        highbyte = binr.ReadByte(); //advance 2 bytes
                        lowbyte = binr.ReadByte();
                    }
                    else return null;

                    byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };   //reverse byte order since asn.1 key uses big endian order
                    int modsize = BitConverter.ToInt32(modint, 0);

                    int firstbyte = binr.PeekChar();
                    if (firstbyte == 0x00)
                    {   //if first byte (highest order) of modulus is zero, don't include it
                        binr.ReadByte();    //skip this null byte
                        modsize -= 1;   //reduce modulus buffer size by 1
                    }

                    byte[] modulus = binr.ReadBytes(modsize);   //read the modulus bytes

                    if (binr.ReadByte() != 0x02)            //expect an Integer for the exponent data
                        return null;

                    int expbytes = binr.ReadByte();        // should only need one byte for actual exponent data (for all useful values)
                    byte[] exponent = binr.ReadBytes(expbytes);

                    // ------- create RSACryptoServiceProvider instance and initialize with public key -----
                    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                    RSAParameters RSAKeyInfo = new RSAParameters();
                    RSAKeyInfo.Modulus = modulus;
                    RSAKeyInfo.Exponent = exponent;
                    RSA.ImportParameters(RSAKeyInfo);

                    return RSA;
                }
            }
        }

        /// <summary>
        /// 创建rsa私钥对象
        /// </summary>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        private static RSACryptoServiceProvider CreateRsaProviderFromPrivateKey(string privateKey)
        {
            var privateKeyBits = Convert.FromBase64String(privateKey);

            var RSA = new RSACryptoServiceProvider();
            var RSAparams = new RSAParameters();

            using (BinaryReader binr = new BinaryReader(new MemoryStream(privateKeyBits)))
            {
                ushort twobytes = binr.ReadUInt16();
                if (twobytes == 0x8130) binr.ReadByte();
                else if (twobytes == 0x8230) binr.ReadInt16();
                else throw new Exception("Unexpected value read binr.ReadUInt16()");

                twobytes = binr.ReadUInt16();
                if (twobytes != 0x0102) throw new Exception("Unexpected version");

                byte bt = binr.ReadByte();
                if (bt != 0x00) throw new Exception("Unexpected value read binr.ReadByte()");

                RSAparams.Modulus = binr.ReadBytes(GetIntegerSize(binr));
                RSAparams.Exponent = binr.ReadBytes(GetIntegerSize(binr));
                RSAparams.D = binr.ReadBytes(GetIntegerSize(binr));
                RSAparams.P = binr.ReadBytes(GetIntegerSize(binr));
                RSAparams.Q = binr.ReadBytes(GetIntegerSize(binr));
                RSAparams.DP = binr.ReadBytes(GetIntegerSize(binr));
                RSAparams.DQ = binr.ReadBytes(GetIntegerSize(binr));
                RSAparams.InverseQ = binr.ReadBytes(GetIntegerSize(binr));
            }

            RSA.ImportParameters(RSAparams);
            return RSA;
        }

        private static int GetIntegerSize(BinaryReader binr)
        {
            int count;

            byte bt = binr.ReadByte();

            if (bt != 0x02) return 0;
            bt = binr.ReadByte();

            if (bt == 0x81) count = binr.ReadByte();
            else if (bt == 0x82)
            {
                byte highbyte = binr.ReadByte();
                byte lowbyte = binr.ReadByte();
                byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
                count = BitConverter.ToInt32(modint, 0);
            }
            else count = bt;

            while (binr.ReadByte() == 0x00) count -= 1;

            binr.BaseStream.Seek(-1, SeekOrigin.Current);

            return count;
        }

        private static bool CompareBytearrays(byte[] a, byte[] b)
        {
            if (a.Length != b.Length) return false;

            int i = 0;
            foreach (byte c in a)
            {
                if (c != b[i]) return false;
                i++;
            }

            return true;
        }
    }
}

以上公钥私钥不可用,如需生成请使用“支付宝开放平台密钥工具”,官方下载链接:密钥工具下载 - 支付宝文档中心 (alipay.com)

以下是工具使用步骤:

第一步:选择RSA2算法,生成密钥

 第二步:复制公钥,直接赋值给工具类变量“publicKey”,复制私钥,进行下一步使用

第三步:到“格式转换”栏,把私钥粘贴到“应用私钥”,点击“转换”,我们可以看到下边有“PKCS1”字样,这就是.net环境中RSA算法支持的私钥格式,复制“输出”,赋值给“privateKey”即可。

以上就是工具使用步骤,快来用最安全的对称加密算法加密你的内容吧!

标签:return,RSA,ReadByte,加密,byte,twobytes,net,binr
From: https://www.cnblogs.com/HelloCreator/p/17587289.html

相关文章

  • .Net6实现定时任务
    首先创建一个类Background实现代码:usingMicrosoft.Extensions.Hosting;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceSmartMedicalCare.Web{publicclassBackground:IHostedService,......
  • 论文解读|PF-Net:用于 3D 点云补全的点分形网络
    原创|文BFT机器人01 背景从激光雷达等设备中获取的点云往往有所缺失(反光、遮挡等),这给点云的后续处理带来了一定的困难,也凸显出点云补全作为点云预处理方法的重要性。点云补全(PointCloudCompletion)用于修补有所缺失的点云(PointCloud),从缺失点云出发估计完整点云,从而获得更高质......
  • apache net FTPClient 通过 Http 代理访问 FTP服务器
    前言因为特殊原因,需要在服务器上安装一个代理程序,通过代理程序才能访问到安全域内的一台指定IP服务器。该服务器上安装了FTP服务,需要用程序读写FTP上的文件。之前在不使用代理程序的服务器上测试FTP连接,没有任何问题,换到这台服务器上有问题,所以记录一下处理过程。文章目录前言问......
  • NET6 EF Error: The certificate chain was issued by an authority that is not trus
    ErrorAconnectionwassuccessfullyestablishedwiththeserver,butthenanerroroccurredduringtheloginprocess.(provider:SSLProvider,error:0-Thecertificatechainwasissuedbyanauthoritythatisnottrusted.)解决方法:在DB连接字符串后面添加......
  • Ubuntu Netplane balancing algorithm modes
    目录起因前提知识配置步骤参考文档说明起因因为机房服务器新部署,双网卡服务器。一般来说业务系统的要求配置都是双网线然后配置bond0,主从轮番使用网卡就行。这次不太一样,网络管理员要求使用boud4模式,也是链路融合,所以就查询了一下资料没发现了bond的配置方式,很奇特的是,和Centos不......
  • 概率图模型(PGM):贝叶斯网(Bayesian network)初探
    概率图模型(PGM):贝叶斯网(Bayesiannetwork)初探1.从贝叶斯方法(思想)说起-我对世界的看法随世界变化而随时变化用一句话概括贝叶斯方法创始人ThomasBayes的观点就是:任何时候,我对世界总有一个主观的先验判断,但是这个判断会随着世界的真实变化而随机修正,我对世......
  • Centos7如何配置IPADDR,NETMASK,GATEWAY?
    1、获取IPADDR、NETMASK:[root@192network-scripts]#ifconfigens33:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu1500inet192.168.85.139netmask255.255.255.0broadcast192.168.85.255inet6fe80::11df:c601:5b38:ca41prefixlen64s......
  • centos7关闭防火墙后只有22端口可以telnet的解决方法
    1、问题描述防火墙已经关闭22端口可以telnet其他端口无法telnet2、解决方法注意:下列命令要用root账号/权限执行2.1、开启防火墙systemctlstartfirewalld2.2、添加要开放的端口该命令以8081端口为示例firewall-cmd--add-port=8081/tcp2.3、重新加载防......
  • linux静态ip | 配置vmnet8的ip
    摘要目的:linux虚拟机固定ip,不要每次登录都由DHCP分配设置vmnet的子网ip本文是同时更改了vmnet8的ip,可以更好地理解虚拟机ip与vmnet8的联系,如果不想该vmnet8的话,可以参考这篇博客一、要求要求:将linux的ip地址配置为192.168.200.130二、步骤该步骤配置了vmnet8的ip信......
  • linux监控网络状态 | netstat指令
    摘要目的:介绍linux如何监控网络状态介绍linux的netstat指令指令netstat可以看到当前的网络服务,哪些服务处于监听状态,哪些连接建立,发现网络变慢了,或者有些端口很大,要小心,如果你从来没有启动过,那么可能这是一个木马程序在监听。指令功能说明选项yuminstallne......