2009-07-22 2 views
7

이 질문은 this one과 비슷한 질문이지만 Bouncey Castle 경로로 향하기 전에 누군가가 해당 URL에서 RSA KeyPair를로드 할 수 있는지 알고 있습니다. .PEM 파일, 예 : 제 3 자에 가서 또는 내 자신의 롤 필요없이 .NET 3.5 암호화 라이브러리를 직접.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----- 

?

http://www.jensign.com/opensslkey/opensslkey.cs에서

+0

@CraigMcQueen 좋은 질문 ... 오류로 생각할 것입니다. –

답변

11

http://www.jensign.com/opensslkey/index.html 소스와

편집 : 발췌 관련 코드 :

먼저 ---- ---- BEGIN ----과 END 사이의 텍스트를 추출 - I 작은 HEL을 만든

//------- 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

+0

나는 이것을 줄 것이다 ... 다른 질문을위한 해결책은 무엇을 의미 하는가? 내가 언급 한 질문에 대한 대답은 Bouncy Castle 라이브러리를 사용했다. 가능한 경우 제 3 자 라이브러리에 대한 의존도를 최소화하고자합니다. 매우 관대 한 라이센싱을 가진 사람들조차도. –

+0

@Tim Jarvis : 다른 질문에 대한 가장 큰 대답은 "JavaScience의 OpenSSLKey 소스를 살펴볼 수 있습니다."입니다. 그것은 내가 위에 링크하고 복사 한 것과 같습니다. (BouncyCastle은 질문자가 답을하는 것이 아니라 스스로에게 대답하는 방법입니다 ...) – Stobor

0

--- 섹션 및 바이트 배열로 64 비트로 디코드 그러므로로 전달 (자세한 내용은 위 링크 참조) NuGet 패키지별로 공개 키 및 개인 (rsa) 키를 기반으로 X509 인증서를 만듭니다.

opensslkey을 기반으로 한 기능 및 코드 예제는 NuGetGithub-project을 참조하십시오.

관련 문제