当前位置: 首页 > 编程语言 > C#

C#LearningTutorial-Usethe.NETEncryptionLibrarytodecryptinc#usingtheprivatekeyinthe.pemfileShare

时间:2023-04-10 22:29:21 C#

Usethe.NETEncryptionLibrarytodecryptinc#usingtheprivatekeyinthe.pemfile此类似的问题,但在我走下BounceyCastle路线之前,是否有人知道是否可以从.pem文件加载RSAKeyPair,例如:-----BEGINRSAPRIVATEKEY-----MIIBOgIBAAJBALKzy66nRuof8Fg0ItatyHS9RiDIKH0m5lorKzKn4y5wR6BXpVUvZwnevrAJWBd6EPr/lcV3hjObxD6+q9vmN8ECAwEAAQJAGNcxWwfZrbXe3QPyS9FAaindU7U/G5aKssIJcTMxO0UYpGU+WArJbboKeEIE7bpNfhDOKTL7ZL6kWBR1SvlhWQIhAOhtx+xXuSrIot59tmXZaypBDjA4n+Xare0ObFLQxWuvAiEAxNMwm6w33bVrFHS9slkOh59Le2mgs0uNT6perHaRP48CIGMyRzrlDY/m5SvTtz6slgIIlceawxNUSxp7J1wI4djdAiA6+BchHNjkCP2a9Fr9OydaRMSFpiDqduFQk/enbiKYSwIhANO3SQ51oLFtWN9gX3tfKTXflyO6BV8rgPo980d9CEsb-----ENDRSAPRIVATEKEY-----直接使用.NET3.5加密库而无需参加第三方或自行滚动?http://www.jensign.com/opensslkey/index.htmlSourcehttp://www.jensign.com/opensslkey/opensslkey.csEDIT:Excerptofrelevantcode:First,in--BEGIN---and---END---Extracttextbetweensectionsandbase64decodeitintoabytearray(seelinkabovefordetails),thenpassitto://-------Parsesbinaryans.1RSAprivatekey;returnsRSACryptoServiceProvider---publicstaticRSACryptoServiceProviderDecodeRSAPrivateKey(byte[]privkey){byte[]MODULUS,E,D,P,Q,DP,DQ,IQ;//----------设置流来解码asn.1编码的RSA私钥------MemoryStreammem=newMemoryStream(privkey);BinaryReaderbinr=newBinaryReader(mem);//用BinaryReader包装MemoryStream,方便读取bytebt=0;ushorttwobytes=0;整数元素=0;尝试{twobytes=binr.ReadUInt16();if(twobytes==0x8130)//按小端顺序读取数据(Sequence的实际数据顺序为3081)binr.ReadByte();//提前1个字节elseif(twobytes==0x8230)binr.ReadInt16();//提前2个字节否则返回null;twobytes=binr.ReadUInt16();if(twobytes!=0x0102)//版本号returnnull;bt=binr.ReadByte();如果(bt!=0x00)返回空;//------所有的私钥分量都是Integer序列----elems=GetIntegerSize(binr);MODULUS=binr.ReadBytes(elems);elems=GetIntegerSize(binr);E=binr.ReadBytes(元素);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);Console.WriteLine("显示组件..");if(verbose){showBytes("nModulus",MODULUS);showBytes("n指数",E);showBytes("nD",D);showBytes("nP",P);showBytes("nQ",Q);showBytes("nDP",DP);showBytes("nDQ",DQ);showBytes("nIQ",智商);}//------创建RSACryptoServiceProvider实例并用公钥初始化------RSACryptoServiceProviderRSA=newRSACryptoServiceProvider();RSAParametersRSAparams=newRSAParameters();RSAparams.Modulus=模数;RSAparams.Exponent=E;RSAparams.D=D;RSAparams.P=P;RSAparams.Q=Q;RSAparams.DP=DP;RSAparams.DQ=DQ;RSA参数.InverseQ=智商;RSA.ImportParameters(RSAparams);返回RSA;}赶上(异常){返回空;}最后{binr.Close();我已经创建了一个小的帮助程序NuGet包来为基于opensslkey的功能和代码示例创建rsa)密钥,请参阅NuGet和Github项目。以上就是C#学习教程:使用.NET加密库使用.pem文件中的私钥解密c#中的所有共享内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多加关注——本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:

最新推荐
猜你喜欢