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:
c# rsa
add a comment |
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:
c# rsa
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
add a comment |
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:
c# rsa
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:
c# rsa
c# rsa
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
add a comment |
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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