2009-07-22 87 views
7

我知道這是一個類似的問題this one但在我走下Bouncey城堡路線之前,有誰知道它是否有可能從一個RSA KeyPair加載.pem文件,例如:解密使用.NET加密庫的C#中的.pem文件私鑰

-----BEGIN RSA PRIVATE KEY----- 
MIIBOgIBAAJBALKzy66nRuof8Fg0ItatyHS9RiDIKH0m5lorKzKn4y5wR6BXpVUv 
ZwnevrAJWBd6EPr/lcV3hjObxD6+q9vmN8ECAwEAAQJAGNcxWwfZrbXe3QPyS9FA 
aindU7U/G5aKssIJcTMxO0UYpGU+WArJbboKeEIE7bpNfhDOKTL7ZL6kWBR1Svlh 
WQIhAOhtx+xXuSrIot59tmXZaypBDjA4n+Xare0ObFLQxWuvAiEAxNMwm6w33bVr 
FHS9slkOh59Le2mgs0uNT6perHaRP48CIGMyRzrlDY/m5SvTtz6slgIIlceawxNU 
Sxp7J1wI4djdAiA6+BchHNjkCP2a9Fr9OydaRMSFpiDqduFQk/enbiKYSwIhANO3 
SQ51oLFtWN9gX3tfKTXflyO6BV8rgPo980d9CEsb 
-----END RSA PRIVATE KEY----- 

直接與.NET 3.5加密庫,而不必去第三方或滾動我自己?

+0

@CraigMcQueen很好的問題......錯誤我會想。 – 2015-06-16 06:46:26

回答

11

http://www.jensign.com/opensslkey/index.html

與源在http://www.jensign.com/opensslkey/opensslkey.cs

編輯:摘錄相關代碼:

首先,提取---- BEGIN ---- ----和END之間的文本 - ---部分,並且它的base64譯碼成字節數組(詳見上文鏈接),然後將其傳遞到:

//------- Parses binary ans.1 RSA private key; returns RSACryptoServiceProvider --- 
public 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) ; 

     Console.WriteLine("showing components .."); 
     if (verbose) { 
      showBytes("\nModulus", MODULUS) ; 
      showBytes("\nExponent", E); 
      showBytes("\nD", D); 
      showBytes("\nP", P); 
      showBytes("\nQ", Q); 
      showBytes("\nDP", DP); 
      showBytes("\nDQ", DQ); 
      showBytes("\nIQ", IQ); 
     } 

// ------- 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) { 
     return null; 
    } 
    finally { 
     binr.Close(); 
    } 
} 
+0

沒關係;那是另一個問題的解決方案。 – Stobor 2009-07-22 00:58:11

0

我已經創建了一個小HEL每個NuGet包創建基於公鑰和私鑰(rsa)的X509證書。

請參閱NuGetGithub-project的功能和代碼示例基於opensslkey

相關問題