I'm using Renci.SshNet library and have it working with using a username and private key. I can get the private key to open fine when it is read from a file or file stream, but it fails during a memory stream.
This works:
var pk = new PrivateKeyFile(@"C:\myfile.ppk");
This works:
var f = new FileStream(@"C:\myfile.ppk", FileMode.Open, FileAccess.Read);
var pk = new PrivateKeyFile(f);
These fail with "Invalid Private Key".
var s = new MemoryStream(Encoding.UTF8.GetBytes(variablename));
//No carriage returns
var s = new MemoryStream(Encoding.UTF8.GetBytes(@"-----BEGIN RSA PRIVATE KEY----- <snip>"));
//Include carriage returns
var s = new MemoryStream(Encoding.UTF8.GetBytes(@"-----BEGIN RSA PRIVATE KEY----- <snip>"));
I even tried the stream position as found on SO:
private Stream GenerateStreamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
I was at first thinking the Renci library had a bug with the Stream
, but obviously the file stream works. I plan on hosting the key in string format in an azure storage table, but am open to other ideas.