首页 > 编程语言 >C# 实现RSA加解密

C# 实现RSA加解密

时间:2023-05-06 15:05:56浏览次数:49  
标签:return string C# 加解密 elems RSA new byte binr


一、支付宝工具生成公钥私钥

下载地址链接: https://pan.baidu.com/s/15L1GM8mK43tzV9XyyNEV8Q

提取码: vux3

使用方法可参考阿里文档:https://docs.open.alipay.com/291/105971/

二、加密

C# 实现RSA加解密_ide

说明:1.使用公钥加密,  2.加密后每次结果是可变的

三、解密

C# 实现RSA加解密_ide_02

说明:1.使用私钥解密

四、实现代码

利用工具可生成公私秘钥,注意(java版本与非java版本)

1.公钥  "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCO772dmiRP4HaddeZTUieS4SWrNnbRPIgxoF5wLZNK1nel3hf7uffWkZ5lzjSuLig5sgPzU/oQgophIG9+NwoHeyXPgnhNV8zPluueHFrQzrSnq9jhUS1yxTezmYr+oxAQIyGQUZO21Sg1+2lqDyatPpRfN4JSKusAw9yOcbkHKQIDAQAB"

2.私钥  "MIICXgIBAAKBgQCO772dmiRP4HaddeZTUieS4SWrNnbRPIgxoF5wLZNK1nel3hf7uffWkZ5lzjSuLig5sgPzU/oQgophIG9+NwoHeyXPgnhNV8zPluueHFrQzrSnq9jhUS1yxTezmYr+oxAQIyGQUZO21Sg1+2lqDyatPpRfN4JSKusAw9yOcbkHKQIDAQABAoGAArchNgZAnFfaSQF9X6XW5J5sVcVSGoV43OB8CsuC2dAbM8Z1VC3jPGtFxA9XxttPnlD4bD3zKS8hq9iu5YnsIdL7bt1hu2vOW2UGtIPfYtTMK49BcgYHk1zIlQh21Jt403SM0hDmbpy/R32QQ3cv5jWnVvlEqiULyOC0joe9vTUCQQDAv4dOl3v3HQ40cWoe7RUKqbn0bBM4xIx2fwMBJTMJFJFWBq97CwtvuwKnlWle5dRDLTrwt7rETfNPW898nb3jAkEAvdeci8MLDAQE9KeIyxJK1170ahwkxTcO8miI5o/u6AekWk+YuNJh871YKoxwqPtD/2cDQH3PLxwklvVJExJ0gwJBAJ7QcP1ZpcPLxfuCA21t7St3A4gYUJIyqIWuS1xzOSTfNI0MPySDyi2KijpoyoRtnEKpjunuiM3caIDX5hMIqf8CQQCfFzYoZa43RpMEl/VqAI1ZiUiYN7eU0fwjpvi7Bvm11tmjmTqqABx4Dz/4gDLVWaP1P9WY0RW0LAh5vVqcsgWTAkEAk27NKkvwGR37bcHF2QwxeQkY+5pamJhIGvEm+dbhOm9P1mmcujrfZ49Aegy3RiIBQACHRNgjC6sYZcrs/wPrXw=="

3.加密调用:RSA.RasEncrypt("公钥 ", "待加密字符串", "UTF-8");

4.解密调用:RSA.RsaDecrypt("公钥 ", "RSA加密后字符串", "UTF-8");

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class RSA
    {
        /// <summary>
        /// RSA公钥加密
        /// </summary>
        /// <param name="publickey">公钥</param>
        /// <param name="content">待加密字符串</param>
        /// <param name="input_charset">编码格式,UTF-8</param>
        /// <returns></returns>
        public static string RasEncrypt(string publickey, string content, string input_charset)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            byte[] cipherbytes;
            rsa.ImportParameters(ConvertFromPublicKey(publickey));
            cipherbytes = rsa.Encrypt(Encoding.GetEncoding(input_charset).GetBytes(content), false);
            return Convert.ToBase64String(cipherbytes);
        }

        /// <summary>
        /// RSA私钥解密
        /// </summary>
        /// <param name="resData">RSA加密后字符串</param>
        /// <param name="privateKey">私钥</param>
        /// <param name="input_charset">编码格式,UTF-8</param>
        /// <returns>明文</returns>
        public static string RsaDecrypt(string resData, string privateKey, string input_charset)
        {
            byte[] DataToDecrypt = Convert.FromBase64String(resData);
            string result = "";
            for (int j = 0; j < DataToDecrypt.Length / 128; j++)
            {
                byte[] buf = new byte[128];
                for (int i = 0; i < 128; i++)
                {

                    buf[i] = DataToDecrypt[i + 128 * j];
                }
                result += decrypt(buf, privateKey, input_charset);
            }
            return result;
        }

        #region 内部方法

        private static string decrypt(byte[] data, string privateKey, string input_charset)
        {
            string result = "";
            RSACryptoServiceProvider rsa = DecodePemPrivateKey(privateKey);
            using (var sh = SHA1.Create())
            {
                byte[] source = rsa.Decrypt(data, false);
                char[] asciiChars = new char[Encoding.GetEncoding(input_charset).GetCharCount(source, 0, source.Length)];
                Encoding.GetEncoding(input_charset).GetChars(source, 0, source.Length, asciiChars, 0);
                result = new string(asciiChars);
                return result;
            }

        }

        private static RSACryptoServiceProvider DecodePemPrivateKey(String pemstr)
        {
            RSACryptoServiceProvider rsa = DecodeRSAPrivateKey(Convert.FromBase64String(pemstr));
            return rsa;
        }

        private static RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey)
        {
            byte[] MODULUS, E, D, P, Q, DP, DQ, IQ;

            // --------- Set up stream to decode the asn.1 encoded RSA private key ------
            MemoryStream mem = new MemoryStream(privkey);
            BinaryReader binr = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading
            byte bt = 0;
            ushort twobytes = 0;
            int elems = 0;
            try
            {
                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();
                if (twobytes != 0x0102) //version number
                    return null;
                bt = binr.ReadByte();
                if (bt != 0x00)
                    return null;


                //------ all private key components are Integer sequences ----
                elems = GetIntegerSize(binr);
                MODULUS = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                E = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                D = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                P = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                Q = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                DP = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                DQ = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                IQ = binr.ReadBytes(elems);


                // ------- create RSACryptoServiceProvider instance and initialize with public key -----
                CspParameters CspParameters = new CspParameters();
                CspParameters.Flags = CspProviderFlags.UseMachineKeyStore;
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(1024, CspParameters);
                RSAParameters RSAparams = new RSAParameters();
                RSAparams.Modulus = MODULUS;
                RSAparams.Exponent = E;
                RSAparams.D = D;
                RSAparams.P = P;
                RSAparams.Q = Q;
                RSAparams.DP = DP;
                RSAparams.DQ = DQ;
                RSAparams.InverseQ = IQ;
                RSA.ImportParameters(RSAparams);
                return RSA;
            }
            catch
            {
                return null;
            }
            finally
            {
                binr.Dispose();
            }
        }

        private static int GetIntegerSize(BinaryReader binr)
        {
            byte bt = 0;
            byte lowbyte = 0x00;
            byte highbyte = 0x00;
            int count = 0;
            bt = binr.ReadByte();
            if (bt != 0x02) //expect integer
                return 0;
            bt = binr.ReadByte();

            if (bt == 0x81)
                count = binr.ReadByte(); // data size in next byte
            else
                if (bt == 0x82)
            {
                highbyte = binr.ReadByte(); // data size in next 2 bytes
                lowbyte = binr.ReadByte();
                byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
                count = BitConverter.ToInt32(modint, 0);
            }
            else
            {
                count = bt; // we already have the data size
            }

            while (binr.ReadByte() == 0x00)
            { //remove high order zeros in data
                count -= 1;
            }
            binr.BaseStream.Seek(-1, SeekOrigin.Current); //last ReadByte wasn't a removed zero, so back up a byte
            return count;
        }

        #endregion

        #region 生成的Pem
        private static RSAParameters ConvertFromPublicKey(string pemFileConent)
        {

            if (string.IsNullOrEmpty(pemFileConent))
            {
                throw new ArgumentNullException("pemFileConent", "This arg cann't be empty.");
            }
            pemFileConent = pemFileConent.Replace("-----BEGIN PUBLIC KEY-----", "").Replace("-----END PUBLIC KEY-----", "").Replace("\n", "").Replace("\r", "");
            byte[] keyData = Convert.FromBase64String(pemFileConent);
            bool keySize1024 = (keyData.Length == 162);
            bool keySize2048 = (keyData.Length == 294);
            if (!(keySize1024 || keySize2048))
            {
                throw new ArgumentException("pem file content is incorrect, Only support the key size is 1024 or 2048");
            }
            byte[] pemModulus = (keySize1024 ? new byte[128] : new byte[256]);
            byte[] pemPublicExponent = new byte[3];
            Array.Copy(keyData, (keySize1024 ? 29 : 33), pemModulus, 0, (keySize1024 ? 128 : 256));
            Array.Copy(keyData, (keySize1024 ? 159 : 291), pemPublicExponent, 0, 3);
            RSAParameters para = new RSAParameters();
            para.Modulus = pemModulus;
            para.Exponent = pemPublicExponent;
            return para;
        }

        #endregion

    }
}

C# 实现RSA加解密_ide_03

标签:return,string,C#,加解密,elems,RSA,new,byte,binr
From: https://blog.51cto.com/u_16082658/6249917

相关文章

  • C# 实现SHA1withRSA
    对称加密(DES、3DES)、非对称加密(RSA、SHA1withRSA)参考:  对称加密(DES、3DES)、非对称加密(RSA、SHA1withRSA)_预立数据科技-CSDN博客一、支付宝工具生成公钥私钥下载地址链接:https://pan.baidu.com/s/15L1GM8mK43tzV9XyyNEV8Q提取码:vux3使用方法可参考阿里文档:网页&移动......
  • Linux 系统进程守护工具 cesi + superviosr
    一、安装Supervisorpipinstallsupervisor使用echo_supervisord_conf命令生成默认配置文件echo_supervisord_conf>/etc/supervisord.conf配置文件说明位置:etc/supervisord.conf内容:#指定了socketfile的位置[unix_http_server]file=/tmp/supervisor.sock;UNIXsock......
  • Docker 入门使用 (二)
    配置国内的源>/etc/docker/daemon.json{"registry-mirrors":["https://mirror.ccs.tencentyun.com","http://registry.docker-cn.com","http://docker.mirrors.ustc.edu.cn","http://hub-mirror.c.163.com......
  • Hive表 Parquet压缩 , Gzip,Snappy,uncompressed 效果对比
     创建两张表,通过一种是parquet,一种使用parquetsnappy压缩创建表使用snappyCREATEEXTERNALTABLEIFNOTEXISTStableName(xxxstring)partitionedby(pt_xvcstring)ROWFORMATDELIMITEDFIELDSTERMINATEDBY'\001'STOREDASPARQUETTBLPROPERTIES('parquet.compre......
  • 一统天下 flutter - widget 自定义: 通过 SingleChildRenderObjectWidget 实现自定义
    源码https://github.com/webabcd/flutter_demo作者webabcd一统天下flutter-widget自定义:通过SingleChildRenderObjectWidget实现自定义组件示例如下:lib\widget\custom\single_child_render_object_widget.dart/**通过SingleChildRenderObjectWidget实现自......
  • 一统天下 flutter - widget 自定义: 通过 CustomPaint 实现自定义组件
    源码https://github.com/webabcd/flutter_demo作者webabcd一统天下flutter-widget自定义:通过CustomPaint实现自定义组件示例如下:lib\widget\custom\custom_paint.dart/**通过CustomPaint实现自定义组件**CustomPaint继承自SingleChildRenderObjectW......
  • org.apache.maven.archetypes:maven-archetype-quickstart 这个是干啥的
    org.apache.maven.archetypes:maven-archetype-quickstart是一个Maven项目的骨架(archetype),用于快速创建一个简单的Maven项目。当我们在命令行执行:bashmvnarchetype:generate-DgroupId=com.example-DartifactId=my-project-Darchetype......
  • WEB|[SUCTF 2019]Pythonginx
    源码@app.route('/getUrl',methods=['GET','POST'])defgetUrl():url=request.args.get("url")host=parse.urlparse(url).hostnameifhost=='suctf.cc':#解析主机名,不能是suctf.ccreturn......
  • WEB|[HITCON 2017]SSRFme
    源码110.244.80.206<?phpif(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){$http_x_headers=explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);$_SERVER['REMOTE_ADDR']=$http_x_headers[0];}#获取......
  • WEB|[De1CTF 2019]SSRF Me
    页面代码为python代码,题目提示为SSRF,并且flagisin./flag.txt格式化代码#!/usr/bin/envpython##encoding=utf-8fromflaskimportFlaskfromflaskimportrequestimportsocketimporthashlibimporturllibimportsysimportosimportjsonreload(sys)sys.se......