博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# RSA PEM解密字符
阅读量:5908 次
发布时间:2019-06-19

本文共 4866 字,大约阅读时间需要 16 分钟。

1、第一步先用openssl将pem的key转换为der的key //bin>openssl.exe rsa -in rsakeydec.pem -outform der -out pri.der 

2、调用下面的程序直接读取der转换为c#所需要的xml Key,之后进行密文解密 

 

using System;using System.IO;using System.Security.Cryptography;using System.Text;namespace ConsoleApplication3 {    class Program {        private static int GetIntegerSize(BinaryReader binr) {            int count;            var 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) {                var highbyte = binr.ReadByte();                var 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;        }        ///         /// e.g:"D:\\pri.der";        ///         ///         /// 
public static RSACryptoServiceProvider DecodeRsaPrivateKey(string filePath) { byte[] MODULUS, E, D, P, Q, DP, DQ, IQ; FileStream fs = new FileStream(filePath,FileMode.Open,FileAccess.Read); BinaryReader binr = new BinaryReader(fs); //wrap Memory Stream with BinaryReader for easy reading try { var 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; var bt = binr.ReadByte(); if(bt != 0x00) return null; var 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 ----- RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 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(Exception e) { Console.WriteLine(e.Message + e.StackTrace); return null; } finally { binr.Close(); } } /// /// 导出私钥XML解密格式 /// ///
public static string PrivateKeyDecXml() { RSACryptoServiceProvider rsaProvider = DecodeRsaPrivateKey(@"D:\\pri.der"); var privateKey = rsaProvider.ToXmlString(true); return privateKey; } /// /// RSA解密 /// /// /// ///
public static string RsaDecrypt(string privatekey,string content) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); byte[] cipherbytes; rsa.FromXmlString(privatekey); cipherbytes = rsa.Decrypt(Convert.FromBase64String(content),false); return Encoding.UTF8.GetString(cipherbytes); } static void Main(string[] args) { var str= PrivateKeyDecXml(); var s = RsaDecrypt(str, "jB6EKo9Oakc8MYMlKorZmj415+s8Qf1sHr1MIPjZcybaFhRQDVb+MBZ734p45lc2RlEJUDsy5HRH8I4LvcGSNE0kJu+ge2yy9E8xOD3dWUTU9/30vv7cEbQ5WSHtjl0MyvhTX51x3vrW/oqubH0H3p827GF3c+ALPlxrvG1gHTc="); } }}

 

转载地址:http://iivpx.baihongyu.com/

你可能感兴趣的文章
简单的利用JS来判断页面是在手机端还是在PC端打开的方法
查看>>
maven报错非法字符:\65279 错误
查看>>
Spider爬虫 の 事
查看>>
运行时runtime
查看>>
System.exit(0)
查看>>
Django之程
查看>>
nginx配置虚拟主机相关教程
查看>>
[HackerRank]Choosing White Balls
查看>>
WebTable 扩展
查看>>
【Perl】perl正则表达式中的元字符、转义字符、量词及匹配方式
查看>>
线性单链表的操作
查看>>
dependcy walker依赖缺失定位工具
查看>>
Html Table表格编辑(添加删除行,单元格)
查看>>
37.scrapy解决翻页及采集杭州造价网站材料数据
查看>>
学习路径,转自他人
查看>>
PAT乙级-1036.跟奥巴马一起编程(15)
查看>>
EF操作MySql
查看>>
python学习——开启python
查看>>
因子和阶乘
查看>>
JS提示框
查看>>