Initialize RSA object from PEM file









up vote
0
down vote

favorite












I have the following method that is able to read in a private key that is in XML format.



private RSAParameters GetPrivateKey(string privateKeyPath)

RSAParameters privateKey;

string readContents;
using (StreamReader streamReader = new StreamReader(privateKeyPath, Encoding.UTF8))

readContents = streamReader.ReadToEnd();


using (var rsa = new RSACryptoServiceProvider(2048))

rsa.FromXmlString(readContents);
privateKey = rsa.ExportParameters(true);


return privateKey;



I want to change that method to us a private key that is in PEM format.



My private key looks like this:



-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAuc7/6TDyberFapcC2XfXKXzF2W8IxfgVXigduvE6B0IQBfxD
J1WcWrNriERUsMpKw/2zphxIXHLrsMZ0l5FcxuZBEqtjSskMqOaMKitMfuZBrcaX
e/0W9HUgPEUBNEv35UHtZbInk7K72KEYaTkoxmI8pP/g82lWnGPw1HgjtFWbL9Vx
--8<-----------8<------------8<-----------8<-----------8<-------
SQBBCQKBgQCaOvc8cmtR8vm8HWKJ93qA0OFmUNcW5Rd32q5Zy2FSFfhODnajP7fE
kqOL1+QS4uxd5e35PCkhYVZkAigKt+I+Mx+M14HLyx6gdeU1fXJGh/LltyWM9cra
Rb059rp8d/1c128t7WdCctAcV09PDVbV8T7xVtr7agBLM47LbgL1QQ==
-----END RSA PRIVATE KEY-----


I would think this would be very simple to find on the RSCryptoServiceProvider class documentation:



https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsacryptoserviceprovider?redirectedfrom=MSDN&view=netframework-4.7.2



But it is not.



The FromXmlString method is documented as "Initializes an RSA Object from XML", I would expect to a see a similar method with "Initializes an RSA Object from PEM".



[EDIT]



I tried to use this solution:



https://stackoverflow.com/a/32243171/192044



But it gave this error:



enter image description here










share|improve this question























  • I would expect to a see a similar method with "Initializes an RSA Object from PEM". Why? It's an openssl format that's has uneven support across other platforms and languages. The Bouncycastle C# library contains support for it. While parsing DER-encoded ASN.1 objects is pretty horrible in general, yours is one of the simplest around being nothing more than a sequence of DERIntegers. I believe that user @bartonjs wrote an answer here on stackoverflow that contains code to parse it. I don't have the link handy unfortunately.
    – James K Polk
    Nov 10 at 1:27















up vote
0
down vote

favorite












I have the following method that is able to read in a private key that is in XML format.



private RSAParameters GetPrivateKey(string privateKeyPath)

RSAParameters privateKey;

string readContents;
using (StreamReader streamReader = new StreamReader(privateKeyPath, Encoding.UTF8))

readContents = streamReader.ReadToEnd();


using (var rsa = new RSACryptoServiceProvider(2048))

rsa.FromXmlString(readContents);
privateKey = rsa.ExportParameters(true);


return privateKey;



I want to change that method to us a private key that is in PEM format.



My private key looks like this:



-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAuc7/6TDyberFapcC2XfXKXzF2W8IxfgVXigduvE6B0IQBfxD
J1WcWrNriERUsMpKw/2zphxIXHLrsMZ0l5FcxuZBEqtjSskMqOaMKitMfuZBrcaX
e/0W9HUgPEUBNEv35UHtZbInk7K72KEYaTkoxmI8pP/g82lWnGPw1HgjtFWbL9Vx
--8<-----------8<------------8<-----------8<-----------8<-------
SQBBCQKBgQCaOvc8cmtR8vm8HWKJ93qA0OFmUNcW5Rd32q5Zy2FSFfhODnajP7fE
kqOL1+QS4uxd5e35PCkhYVZkAigKt+I+Mx+M14HLyx6gdeU1fXJGh/LltyWM9cra
Rb059rp8d/1c128t7WdCctAcV09PDVbV8T7xVtr7agBLM47LbgL1QQ==
-----END RSA PRIVATE KEY-----


I would think this would be very simple to find on the RSCryptoServiceProvider class documentation:



https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsacryptoserviceprovider?redirectedfrom=MSDN&view=netframework-4.7.2



But it is not.



The FromXmlString method is documented as "Initializes an RSA Object from XML", I would expect to a see a similar method with "Initializes an RSA Object from PEM".



[EDIT]



I tried to use this solution:



https://stackoverflow.com/a/32243171/192044



But it gave this error:



enter image description here










share|improve this question























  • I would expect to a see a similar method with "Initializes an RSA Object from PEM". Why? It's an openssl format that's has uneven support across other platforms and languages. The Bouncycastle C# library contains support for it. While parsing DER-encoded ASN.1 objects is pretty horrible in general, yours is one of the simplest around being nothing more than a sequence of DERIntegers. I believe that user @bartonjs wrote an answer here on stackoverflow that contains code to parse it. I don't have the link handy unfortunately.
    – James K Polk
    Nov 10 at 1:27













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have the following method that is able to read in a private key that is in XML format.



private RSAParameters GetPrivateKey(string privateKeyPath)

RSAParameters privateKey;

string readContents;
using (StreamReader streamReader = new StreamReader(privateKeyPath, Encoding.UTF8))

readContents = streamReader.ReadToEnd();


using (var rsa = new RSACryptoServiceProvider(2048))

rsa.FromXmlString(readContents);
privateKey = rsa.ExportParameters(true);


return privateKey;



I want to change that method to us a private key that is in PEM format.



My private key looks like this:



-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAuc7/6TDyberFapcC2XfXKXzF2W8IxfgVXigduvE6B0IQBfxD
J1WcWrNriERUsMpKw/2zphxIXHLrsMZ0l5FcxuZBEqtjSskMqOaMKitMfuZBrcaX
e/0W9HUgPEUBNEv35UHtZbInk7K72KEYaTkoxmI8pP/g82lWnGPw1HgjtFWbL9Vx
--8<-----------8<------------8<-----------8<-----------8<-------
SQBBCQKBgQCaOvc8cmtR8vm8HWKJ93qA0OFmUNcW5Rd32q5Zy2FSFfhODnajP7fE
kqOL1+QS4uxd5e35PCkhYVZkAigKt+I+Mx+M14HLyx6gdeU1fXJGh/LltyWM9cra
Rb059rp8d/1c128t7WdCctAcV09PDVbV8T7xVtr7agBLM47LbgL1QQ==
-----END RSA PRIVATE KEY-----


I would think this would be very simple to find on the RSCryptoServiceProvider class documentation:



https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsacryptoserviceprovider?redirectedfrom=MSDN&view=netframework-4.7.2



But it is not.



The FromXmlString method is documented as "Initializes an RSA Object from XML", I would expect to a see a similar method with "Initializes an RSA Object from PEM".



[EDIT]



I tried to use this solution:



https://stackoverflow.com/a/32243171/192044



But it gave this error:



enter image description here










share|improve this question















I have the following method that is able to read in a private key that is in XML format.



private RSAParameters GetPrivateKey(string privateKeyPath)

RSAParameters privateKey;

string readContents;
using (StreamReader streamReader = new StreamReader(privateKeyPath, Encoding.UTF8))

readContents = streamReader.ReadToEnd();


using (var rsa = new RSACryptoServiceProvider(2048))

rsa.FromXmlString(readContents);
privateKey = rsa.ExportParameters(true);


return privateKey;



I want to change that method to us a private key that is in PEM format.



My private key looks like this:



-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAuc7/6TDyberFapcC2XfXKXzF2W8IxfgVXigduvE6B0IQBfxD
J1WcWrNriERUsMpKw/2zphxIXHLrsMZ0l5FcxuZBEqtjSskMqOaMKitMfuZBrcaX
e/0W9HUgPEUBNEv35UHtZbInk7K72KEYaTkoxmI8pP/g82lWnGPw1HgjtFWbL9Vx
--8<-----------8<------------8<-----------8<-----------8<-------
SQBBCQKBgQCaOvc8cmtR8vm8HWKJ93qA0OFmUNcW5Rd32q5Zy2FSFfhODnajP7fE
kqOL1+QS4uxd5e35PCkhYVZkAigKt+I+Mx+M14HLyx6gdeU1fXJGh/LltyWM9cra
Rb059rp8d/1c128t7WdCctAcV09PDVbV8T7xVtr7agBLM47LbgL1QQ==
-----END RSA PRIVATE KEY-----


I would think this would be very simple to find on the RSCryptoServiceProvider class documentation:



https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsacryptoserviceprovider?redirectedfrom=MSDN&view=netframework-4.7.2



But it is not.



The FromXmlString method is documented as "Initializes an RSA Object from XML", I would expect to a see a similar method with "Initializes an RSA Object from PEM".



[EDIT]



I tried to use this solution:



https://stackoverflow.com/a/32243171/192044



But it gave this error:



enter image description here







c# rsa






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 3:11

























asked Nov 9 at 23:29









Michael Potter

3,71353663




3,71353663











  • I would expect to a see a similar method with "Initializes an RSA Object from PEM". Why? It's an openssl format that's has uneven support across other platforms and languages. The Bouncycastle C# library contains support for it. While parsing DER-encoded ASN.1 objects is pretty horrible in general, yours is one of the simplest around being nothing more than a sequence of DERIntegers. I believe that user @bartonjs wrote an answer here on stackoverflow that contains code to parse it. I don't have the link handy unfortunately.
    – James K Polk
    Nov 10 at 1:27

















  • I would expect to a see a similar method with "Initializes an RSA Object from PEM". Why? It's an openssl format that's has uneven support across other platforms and languages. The Bouncycastle C# library contains support for it. While parsing DER-encoded ASN.1 objects is pretty horrible in general, yours is one of the simplest around being nothing more than a sequence of DERIntegers. I believe that user @bartonjs wrote an answer here on stackoverflow that contains code to parse it. I don't have the link handy unfortunately.
    – James K Polk
    Nov 10 at 1:27
















I would expect to a see a similar method with "Initializes an RSA Object from PEM". Why? It's an openssl format that's has uneven support across other platforms and languages. The Bouncycastle C# library contains support for it. While parsing DER-encoded ASN.1 objects is pretty horrible in general, yours is one of the simplest around being nothing more than a sequence of DERIntegers. I believe that user @bartonjs wrote an answer here on stackoverflow that contains code to parse it. I don't have the link handy unfortunately.
– James K Polk
Nov 10 at 1:27





I would expect to a see a similar method with "Initializes an RSA Object from PEM". Why? It's an openssl format that's has uneven support across other platforms and languages. The Bouncycastle C# library contains support for it. While parsing DER-encoded ASN.1 objects is pretty horrible in general, yours is one of the simplest around being nothing more than a sequence of DERIntegers. I believe that user @bartonjs wrote an answer here on stackoverflow that contains code to parse it. I don't have the link handy unfortunately.
– James K Polk
Nov 10 at 1:27


















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234555%2finitialize-rsa-object-from-pem-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234555%2finitialize-rsa-object-from-pem-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo