我需要使用 C# 在 ssh 隧道中生成用于身份验证的密钥对。
唯一的约束是应该是Ed25519的密码。
我能够生成有效的公钥,但不能生成有效的私钥(或者可能只是格式)。
我尝试使用BouncyCastle和NSec库来生成它们,但没有成功。
这是我的一些尝试:
#### NSec ####
var creationParameters = new KeyCreationParameters
{
ExportPolicy = KeyExportPolicies.AllowPlaintextArchiving
};
using (var key = Key.Create(SignatureAlgorithm.Ed25519, creationParameters))
{
var blob = key.Export(KeyBlobFormat.PkixPrivateKeyText);
result.PrivateKey = Convert.ToBase64String(blob);
blob = key.Export(KeyBlobFormat.PkixPublicKeyText);
result.PublicKey = string.Format("ssh-ed25519 {0} generated-key", Convert.ToBase64String(blob));
}
#### BouncyCastle ####
IAsymmetricCipherKeyPairGenerator gen;
KeyGenerationParameters param;
gen = new Ed25519KeyPairGenerator();
param = new Ed25519KeyGenerationParameters(new SecureRandom());
gen.Init(param);
AsymmetricCipherKeyPair pair = gen.GenerateKeyPair();
PrivateKeyInfo pkInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(pair.Private);
result.PrivateKey = Convert.ToBase64String(pkInfo.GetDerEncoded());
SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pair.Public);
result.PublicKey = Convert.ToBase64String(info.GetDerEncoded());
更具体地说,我希望从 cmd 获得类似以下生成的结果:
ssh-keygen -t ed25519 -f ssh-ed25519-private-key.pem
生成公钥,如:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICjTBKzEKElOtmjuYDaBsoF9UpsXUeLUmKuqiK86jv2A xxxxxx\xxxxxxx@xxxx
而私人喜欢:
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACAo0wSsxChJTrZo7mA2gbKBfVKbF1Hi1JirqoivOo79gAAAAKgLape6C2qX
ugAAAAtzc2gtZWQyNTUxOQAAACAo0wSsxChJTrZo7mA2gbKBfVKbF1Hi1JirqoivOo79gA
AAAECRX7SDurbmPZzRMqYwONep7gk4ZCFp1Uj6hTpVbGWi0CjTBKzEKElOtmjuYDaBsoF9
UpsXUeLUmKuqiK86jv2AAAAAJWNnLWNvbnRyb2xzLXJkXGFtZW5lZ2F0QFcwNTItQU1lbm
VnYXQ=
-----END OPENSSH PRIVATE KEY-----
谢谢,
安德里亚