1. First step using OpenSSL to convert Pem key to der key//bin>openssl.exe rsa-in rsakeydec.pem-outform der-out pri.der
2, call the following program directly read der converted to C # required XML key, and then ciphertext decryption
usingSystem;usingSystem.IO;usingSystem.Security.Cryptography;usingSystem.Text;namespaceConsoleApplication3 {classProgram {Private Static intgetintegersize (BinaryReader binr) {intcount; varBT =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) { varHighbyte =Binr. ReadByte (); varLowbyte =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 byte returncount; } /// <summary> ///e.g: "d:\\pri.der"; /// </summary> /// <param name= "FilePath" ></param> /// <returns></returns> Public StaticRSACryptoServiceProvider Decodersaprivatekey (stringFilePath) { byte[] modulus, E, D, P, Q, DP, DQ, IQ; FileStream FS=NewFileStream (Filepath,filemode.open,fileaccess.read); BinaryReader Binr=NewBinaryReader (FS);//Wrap Memory Stream with BinaryReader for easy reading Try { varTwobytes =Binr. ReadUInt16 (); if(Twobytes = =0x8130)//data read as little endian order (actual data order for Sequence)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; varBT =Binr. ReadByte (); if(BT! =0x00) return NULL; varElems =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 =NewRSACryptoServiceProvider (); RSAParameters Rsaparams=NewRSAParameters (); 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); returnRSA; } Catch(Exception e) {Console.WriteLine (E.message+e.stacktrace); return NULL; } finally{Binr. Close (); } } /// <summary> ///Export private key XML decryption format/// </summary> /// <returns></returns> Public Static stringPrivatekeydecxml () {RSACryptoServiceProvider Rsaprovider= Decodersaprivatekey (@"D:\\pri.der"); varPrivatekey = rsaprovider.toxmlstring (true); returnPrivatekey; } /// <summary> ///RSA Decryption/// </summary> /// <param name= "Privatekey" ></param> /// <param name= "content" ></param> /// <returns></returns> Public Static stringRsadecrypt (stringPrivatekey,stringcontent) {RSACryptoServiceProvider RSA=NewRSACryptoServiceProvider (); byte[] cipherbytes; Rsa. Fromxmlstring (Privatekey); Cipherbytes= RSA. Decrypt (convert.frombase64string (content),false); returnEncoding.UTF8.GetString (cipherbytes); } Static voidMain (string[] args) { varStr=Privatekeydecxml (); vars =rsadecrypt (str,"jb6eko9oakc8mymlkorzmj415+s8qf1shr1mipjzcybafhrqdvb+mbz734p45lc2rlejudsy5hrh8i4lvcgsne0kju+ Ge2yy9e8xod3dwutu9/30vv7cebq5wshtjl0myvhtx51x3vrw/oqubh0h3p827gf3c+alplxrvg1ghtc="); } }}
C # RSA PEM decryption characters