3

I have a Windows machine that needs to connect (programmatically) to a Linux machine. I've got a public key to that machine, but I cannot seem to use it in C# using RSACryptoServiceProvider. I get an exception:

Invalid syntax on line 10.

on this line:

rsa.FromXmlString(File.ReadAllText(PublicKeyFile));

My public key (that I was given and generated using openSSL on a Linux machine) looks like this:

---- BEGIN SSH2 PUBLIC KEY ----
Subject: server
Comment: "A public Key"
ModBitSize: 2048
AAAAB3NzaC1kc3MAAAEBAOcQut33liU3Z9Wji6gWzUwxxrMgAki4/UZDbpNLWQW2
Wov2zMkEo/oHb2eSnAbYhBSCSlrMrWzukJboww8PGoSmcQej/W75Ag8RytwDEIjv.......

What do I need to do for my C# libraries to use this key? It was generated by OpenSSL. I've tried using puttygen's keys too but they don't work with the Microsoft libraries. I can't ask them to use keys I generate via

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.KeySize = 2048;
rsa.ToXmlString(false)

I've scoured the forums but have not found a solution. How can I use the given public key in my C# code?

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
Joseph
  • 2,155
  • 6
  • 20
  • 32
  • So you show some code to read an XML key while the key is clearly not in XML... Maybe you should try methods that read, eh, SSH2 keys? – Maarten Bodewes May 05 '14 at 07:39
  • That's my whole Q. RSACryptoServiceProvider doesn't have any methods that read or recognize non-XML keys. – Joseph May 06 '14 at 07:58
  • It seems to me that an obvious solution is to 1) read out the SSH2 key and 2) convert it to XML. Please try to implement this and come back if it fails. Otherwise this question comes down to a code request, which is off topic. – Maarten Bodewes May 06 '14 at 08:46
  • Not really, I'm not asking anyone for a code snippet. I'm sure this has been solved by someone. I'm not asking anyone to write code, instead for a library or an approach on how this ought to be done. Read out the key and converting to XML - it's not straightforward; I'm sure there would be cases where .NET stack needs to consume keys generated by OpenSSL or in other formats – Joseph May 08 '14 at 04:46
  • OK, that's fair I guess, although I think the question is asking a bit much. Reading out/using SSH2 keys is indeed requiring a library or similar. Then again, you may not have a RSACryptoServiceProvider after you've read out the key. – Maarten Bodewes May 08 '14 at 09:21
  • Dan and @Maarten Bodewes, any solutions? – Mohamed Nuur Nov 29 '17 at 21:19
  • Not ready made. And rereading the question I see a SSH2 public key which was indicated to have been created using OpenSSL? There is the Bouncy Castle library which can read PKI compatible keys and there are certainly also libraries out there that can parse OpenSSH keys. – Maarten Bodewes Nov 30 '17 at 20:05

0 Answers0