【加密与解密】C#如何读取pem的KEY文件

【加密与解密】C#如何读取pem的KEY文件

分类:Windows DevelopNetwork Security

rsadem-xml

1、第一步先用openssl将pem的key转换为der的key//E:\01Doc\bin>openssl.exe rsa -in rsakeydec.pem -outform der -out pri.der 2、调用下面的程序直接读取der转换为c#所需要的xml Key,之后进行密文解密 3、openssl下载地址

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Security.Cryptography;using System.ComponentModel;using System.Runtime.InteropServices;/*refer: */namespace ConsoleApplication1{class Program{(BinaryReader binr){byte bt = 0;byte lowbyte = 0x00;byte highbyte = 0x00;int count = 0;bt = binr.ReadByte();;bt = binr.ReadByte();if (bt == 0x81)count = binr.ReadByte(); (bt == 0x82){highbyte = binr.ReadByte(); // data size in next 2 byteslowbyte = 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 datacount -= 1;}binr.BaseStream.Seek(-1, SeekOrigin.Current);//last ReadByte wasn’t a removed zero, so back up a bytereturn count;}RSACryptoServiceProvider DecodeRSAPrivateKey(string priKey){//var privkey = Convert.FromBase64String(priKey);byte[] MODULUS, E, D, P, Q, DP, DQ, IQ;path = @”D:\\project\\ConsoleApplication1\\li_pri.der”;FileStream fs = new FileStream(path, FileMode.Open,FileAccess.Read);BinaryReader binr = new BinaryReader(fs); //wrap Memory Stream with BinaryReader for easy readingbyte 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();(twobytes == 0x8230)binr.ReadInt16();;twobytes = binr.ReadUInt16();;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 —–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();}}/************************************************************************//* 你的是pem key,需要下转换为c# rsa provider认识的key。首先,干掉头部和尾部的无用字符,然后利用如下方法转换:*/(){string priKey = @”—–BEGIN RSA PRIVATE KEY—–MIICWwIBAAKBgQCf1a4LQyipBqeUCZ9kKsfasQzkEFCBmGsM21Sakb5BO0sY07GDcproJHF2xNQrV0cM7+liE3pBUFsarui2WaHZhAibpLbl9z4FSfoN5hSg6sEgbB17SvKe3ZN/75GoEsQiQtYW4gUJgzrBovVZ+TeTnN+NHHBqUqBKhNIgPFVapQIDAQABAoGAG0OMs5kaF3LuJN9bU+/ENXab908dHG4OXJwRG2ie5muhzLNXhU+IQu7sd9DtTBNQKFHIIpWl9fwp/iw1v90cMUQGj0zhSXHAz7Vak/ryQLTyeIIciL8MQWvnbAaNlIoFq2wBl7SYs3n71B4MlvvTysaG0krsjiPh5LVgnBvzjGECQQDcAwe4XnF7SHWOnfljrG29soKNiUhYKtDGcV9fvam9u50Ek882wvFmsJP+tk+1CXjMRSNlOi40bxKCuaBa1JOtAkEAufq9FmZHfBFf3e6n57wLiAj5C1MeyHAtt6qdAF49OZJBGZh1pePnjDGNezFvy7U5bMp7/updisLCFueS5eKB2QJAF84QIMe/OZqedZ7sI/e9LABLlerbtAZ17nLH4gEQg6HwHFWt3vv6yKSkbrPlLe5nbpqweLxx0WSPOSvCiPFlRQJAPAfFNQ+6jz+EdDxukgxOpJBQ4ujnjMc42ooFt3KzzHt66+ocP3m66bOs+VDRxy0t5gHN2FCJ9Ro8T+xbrDxasQJAARHpcG6tE0F+lmUthtep1U8OrF+AQvqDhBq8MYK+/pF/LRZkFHkqTsj89OyWDlSH3LeYkOWsr9mAFxsvHZ9BSA==—–END RSA PRIVATE KEY—–“;priKey = priKey.Replace(“—–BEGIN RSA PRIVATE KEY—–“, “”).Replace(“—–END RSA PRIVATE KEY—–“, “”);RSACryptoServiceProvider rsaProvider = DecodeRSAPrivateKey(priKey);//RSACryptoServiceProvider rsaProvider = DecodeRSAPrivateKey();//rsaProvider.FromXmlString();String PrivateKey = rsaProvider.ToXmlString(true);//将RSA算法的私钥导出到字符串PrivateKey中,参数为true表示导出私钥Console.WriteLine(PrivateKey);/************************************************************************//* 程序运行结果如下:* <RSAKeyValue><Modulus>n9WuC0MoqQanlAmfZCrH2rEM5BBQgZhrDNtUmpG+QTtLGNOxg3Ka6CRxdsTUK1dHDO/pYhN6QVBbGq7otlmh2YQIm6S25fc+BUn6DeYUoOrBIGwde0rynt2Tf++RqBLEIkLWFuIFCYM6waL1Wfk3k5zfjRxwalKgSoTSIDxVWqU=</Modulus><Exponent>AQAB</Exponent><P>3AMHuF5xe0h1jp35Y6xtvbKCjYlIWCrQxnFfX72pvbudBJPPNsLxZrCT/rZPtQl4zEUjZTouNG8SgrmgWtSTrQ==</P><Q>ufq9FmZHfBFf3e6n57wLiAj5C1MeyHAtt6qdAF49OZJBGZh1pePnjDGNezFvy7U5bMp7/updisLCFueS5eKB2Q==</Q><DP>F84QIMe/OZqedZ7sI/e9LABLlerbtAZ17nLH4gEQg6HwHFWt3vv6yKSkbrPlLe5nbpqweLxx0WSPOSvCiPFlRQ==</DP><DQ>PAfFNQ+6jz+EdDxukgxOpJBQ4ujnjMc42ooFt3KzzHt66+ocP3m66bOs+VDRxy0t5gHN2FCJ9Ro8T+xbrDxasQ==</DQ><InverseQ>ARHpcG6tE0F+lmUthtep1U8OrF+AQvqDhBq8MYK+/pF/LRZkFHkqTsj89OyWDlSH3LeYkOWsr9mAFxsvHZ9BSA==</InverseQ><D>G0OMs5kaF3LuJN9bU+/ENXab908dHG4OXJwRG2ie5muhzLNXhU+IQu7sd9DtTBNQKFHIIpWl9fwp/iw1v90cMUQGj0zhSXHAz7Vak/ryQLTyeIIciL8MQWvnbAaNlIoFq2wBl7SYs3n71B4MlvvTysaG0krsjiPh5LVgnBvzjGE=</D></RSAKeyValue>请按任意键继续. . .*//************************************************************************/}(){RSACryptoServiceProvider rsaProvider = DecodeRSAPrivateKey(null);String PrivateKey = rsaProvider.ToXmlString(true);//将RSA算法的私钥导出到字符串PrivateKey中,参数为true表示导出私钥Console.WriteLine(PrivateKey);}static void Main(string[] args){PrivateKeyDecFun();PrivateKeyDecFun1();return;}}}

宁愿停歇在你门前的那棵树上,看着你,守护你。

【加密与解密】C#如何读取pem的KEY文件

相关文章:

你感兴趣的文章:

标签云: