c#


Does SSH.NET accept only OpenSSH format of private key? If not, what are the restrictions?


The manual doesn't indicate such, but some Googling returns pages from the turn of the decade (such as this) suggesting that this at least used to be the case. We have code like this:
var privateKeyAuthenticationMethod =
new PrivateKeyAuthenticationMethod(userName, new PrivateKeyFile(privateKeyLocation));
var connInfo =
new ConnectionInfo(
ftpSettings.HostAddress, ftpSettings.UserName, privateKeyAuthenticationMethod);
using (var client = new SftpClient(connInfo))
{
client.Connect();
client.UploadFile(memStreamData, destination);
}
This hooks into a private key that we have used successfully via SCP2 (Secure Copy 2 as detailed here) - it is 2048 DSA, which is listed as valid in the SSH.NET documentation. The key uses an ssh.com format like:
---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----
Subject: <FID name>
Comment: "2048-bit dsa, <FID name>#<Server name>, Mon Apr 24 201\
7 15:49:36 +0100"
<ENCRYPTED KEY>
---- END SSH2 ENCRYPTED PRIVATE KEY ----
However, there is a Regex within the private key handling portion of SSH.NET that fails to match our private key, and so throws an exception:
ERROR 2017-05-10 15:31:43 UTC [T: 474] – Invalid private key file.
ERROR 2017-05-10 15:31:43 UTC [T: 474] – at Renci.SshNet.PrivateKeyFile.Open(Stream privateKey, String passPhrase)
It does however match if we convert the key to OpenSSH format, but this is not necessarily something that we can use in production due to business constraints.
Does anyone know exactly what the restrictions are on Private Key format for SSH.NET please? We are not sure if this time if we need to abandon SSH.NET for some other SFTP wrapper library, write our own, or what.
As the in-code documentation for PrivateKeyFile type says:
Supports RSA and DSA private key in both OpenSSH and ssh.com format.
I have generated new ssh.com private key:
---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----
Comment: "dsa-key-20170511"
P2/56wAAA4IAAAAmZGwtbW9kcHtzaWdue2RzYS1uaXN0LXNoYTF9LGRoe3BsYWlufX0AAA
AEbm9uZQAAA0QAAANAAAAAAAAACACOT7SBNaNYmT13gDCCFyE/3yEs6oVbmmGVM6dIesTC
iwY26oHL2JKNLh2gHYfwa9iwDESAH6CIUR+Jf19KXLyNMLQ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KeyFile can load it without any problem.
One thing with your key, that the PrivateKeyFile cannot handle, is the Subject: header. Remove it.
If you need the key file intact, just remove it on-the-fly, while reading:
string key = File.ReadAllText(#"C:\path\key");
Regex removeSubjectRegex = new Regex("Subject:.*[\r\n]+", RegexOptions.IgnoreCase);
key = removeSubjectRegex.Replace(key, "");
MemoryStream buf = new MemoryStream(Encoding.UTF8.GetBytes(key));
PrivateKeyFile privateKeyFile = new PrivateKeyFile(buf);

Related Links

Why my code isn't displaying last writeline
Visual Studio C# library building improperly
How to query ADO.Net C# Connection Properties
Counting words in each sentence using C#
App Resuming event not firing when the app is resumed in WP 8.1 store app
Identify from which page come to this page in windows phone 8.1
Post on Facebook Page from a Worklow c#
How to bind to a collection inside my ViewModel
Excel Add in Ribbon Gallery control
ASP.NET / Linq - How do you escape HTML characters in a linq query?
MVC model regex validation disallow or exclude a certain character
how to consolidate the different columns into single column from different tables
ServiceStack: Any easy way or option to sanitize string values?
Get value of prefer-32bit flag from assembly
Deserialize JSON string in c# [closed]
Create a simple button in Unity3D 2D mode

Categories

HOME
actionscript-3
postgresql
virtual-machine
sip
python-requests
ibm
esxi
sms
mingw
multipartform-data
symfony-3.2
python-3.4
handlebars.js
ejb-3.0
apache-httpclient-4.x
fetch
mamp
osrm
web-worker
multibinding
ringcentral
indri
gomobile
monad-transformers
coccinelle
lotusscript
connection-pooling
onclicklistener
devexpress-mvc
aspxgridview
aws-certificate-manager
deployment-descriptor
orgchart
angular-schema-form
login-required
slim-lang
qt-designer
zimbra
respect-validation
const
latency
ngrok
blueimp
tapply
apiman
libzip
smooth-streaming
risc
easyquery
import.io
pyopenssl
sharpssh
totalview
blackberry-webworks
computed-properties
gevent
optional-parameters
jigsaw
csc
qtruby
sprockets
dcast
gtkwave
ruby-1.8.7
serial-communication
self-join
first-order-logic
syncano
joomla3.3
strcpy
chrome-mobile
jad
crystal-reports-7
growl
yosemite
breeze-sharp
agent-based-modeling
django-tests
object-oriented-database
infor-eam
mixradio
autofilter
fade
sygic
pudb
string-length
django-tinymce
yii-components
configurationsection
filedialog
flatten
bash4
eeprom
inserthtml
nsundomanager
httppostedfilebase
sessiontracking
meego-harmattan
azure-appfabric
webdb
dynamic-scope
pkcs#7
qlibrary

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile