我想使用RSA密钥通过ssh与debian系统建立远程连接,我需要在C#中生成这些RSA密钥。
我已使用充气城堡以以下方式生成RSA密钥:
using (var rsa = new RSACryptoServiceProvider(2048))
{
try
{
RsaKeyPairGenerator r = new RsaKeyPairGenerator();
r.Init(new KeyGenerationParameters(new SecureRandom(), 2048));
AsymmetricCipherKeyPair keys = r.GenerateKeyPair();
AsymmetricKeyParameter private_key = keys.Private;
AsymmetricKeyParameter public_key = keys.Public;
TextWriter textWriter = new StringWriter();
PemWriter pemWriter = new PemWriter(textWriter);
pemWriter.WriteObject(keys.Private);
pemWriter.Writer.Flush();
privateKey = textWriter.ToString();
textWriter = new StringWriter();
pemWriter = new PemWriter(textWriter);
pemWriter.WriteObject(keys.Public);
pemWriter.Writer.Flush();
publicKey = textWriter.ToString();
Console.WriteLine("Private: \n" + privateKey);
Console.WriteLine("Public: \n" + publicKey);
}
finally
{ rsa.PersistKeyInCsp = false; }
}
打印的键如下所示:
----- BEGIN RSA私钥----- MIIEpAIBAAKCAQEAsftF1kqh6vmky + nLKdKjHQpwAGSzOSpXnKYuF5SrFYLvWax0 + KoiM7lRlfTsJFNBC2YFI09Tikk68 / ncAk3k2xM1BJzBfeas1qEvNHn2 / + A5G5cc SG5oog3laNoEVt / Ke5tpJ9iHxt4HunsUcv53d7SWX6aPQwJbC1t7rSoQXdas6Z8I 08Zm2r1xD6aoBwhaoqKRFq2lvVRmNwAIpshVfp4IdtYnpxxhrZGaafItdspzmEG8 oR4wOYRJh38wQ8Ma + F02LTugGj8fvU6YAldS7qtOt9e3EN / IuiwOW + jp2vy / ihIq NmgrR9nw3r64XKiV3KKJT6jE0peu3bMDwW1kIQIDAQABAoIBABUgMTrtYujENxZp Ks9NDEVvLrotOcTITNvwMbpk0CXeg9COEthNQYAIODHnf3wUUb0rn + KAEPdyF8hL 2RyA6HBEiF + Cx8jM76kOOs8sxu + 6Sq0Gs7m5OTw7EAbxEeDJ8XvYh5PONNebFSnC YDwCDmYsDklOOOUIS4w + kKC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CTDoA ==
----- END RSA私钥----------通用公钥----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsftF1kqh6vmky + nLKdKj HQpwAGSzOSpXnKYuF5SrFYLvWax0 + KoiM7lRlfTsJFNBC2YFI09Tikk68 / ncAk3k 2xM1BJzBfeas1qEvNHn2 / + A5G5ccSG5oog3laNoEVt / Ke5tpJ9iHxt4HunsUcv53 d7SWX6aPQwJbC1t7rSoQXdas6Z8I08Zm2r1xD6aoBwhaoqKRFq2lvVRmNwAIpshV fp4IdtYnpxxhrZGaafItdspzmEG8oR4wOYRJh38wQ8Ma + F02LTugGj8fvU6YAldS 7qtOt9e3EN / IuiwOW + jp2vy / ihIqNmgrR9nw3r64XKiV3KKJT6jE0peu3bMDwW1k IQIDAQAB
----- END公钥-----
打印的密钥似乎是有效的RSA密钥。我试图用它们在一个在线工具中对数据进行签名和验证,并且它起作用了。 但是,当我尝试使用这些密钥建立ssh连接时,它不起作用,并提示我输入密码。
然后我将私钥保存在文件中,并使用ssh-keygen打印公钥:
ssh-keygen -y -e -f privateKey.pem
输出与c#公钥不同:
---- BEGIN SSH2公共密钥----
注释:“ 2048位RSA,由xyz从OpenSSH转换而来”
AAAAB3NzaC1yc2EAAAADAQABAAABAQCx + 0XWSqHq + aTL6csp0qMdCnAAZLM5Klec pi4XlKsVgu9ZrHT4qiIzuVGV9OwkU0ELZgUjT1OKSTrz + dwCTeTbEzUEnMF95qzW
oS80efb / 4DkblxxIbmiiDeVo2gRW38p7m2kn2IfG3ge6exRy / nd3tJZfpo9DAlsL
W3utKhBd1qzpnwjTxmbavXEPpqgHCFqiopEWraW9VGY3AAimyFV + ngh21ienHGGt
kZpp8i12ynOYQbyhHjA5hEmHfzBDwxr4XTYtO6AaPx + 9TpgCV1Luq06317cQ38i6
LA5b6Ona / L + KEio2aCtH2fDevrhcqJXcoolPqMTSl67dswPBbWQh
----结束SSH2公钥----
如果我使用ssh-keygen的此输出和c#私钥建立ssh连接,则它起作用。
我假设两个公钥必须是一个公钥和同一公钥的不同表示形式,但是有人可以向我解释为什么它们不同,或者如何调整我的c#代码以获取与{{1}相同的公钥}?
或者也许总的来说:如果我想在Debian Linux计算机上使用这些密钥进行SSH登录,用C#生成RSA密钥的一种好方法是什么?