我已经使用PuttyGen将公共/专用密钥对创建为.ppk文件,但是在我的代码中实际使用它时遇到了麻烦。在记事本中打开随机生成的ppk文件显示如下:

PuTTY-User-Key-File-2: ssh-rsa
Encryption: aes256-cbc
Comment: rsa-key-20151217
Public-Lines: 6
AAAAB3NzaC1yc2EAAAABJQAAAQEAl3m8PRlx/SL7EJrs+hDQbP9mp27XXRY4pztg
v8mxAthI2tMEhF0eyXqFV0/W/M13pPs1hHh3H7wIfAy/XbxF7KPeOeMkThMmF2p1
cwJOcpFuh6TCPT09ScPLCR5bsmQyzvsjPMWahcoDrLhf9MGfc9luQs7k1eMTM1iX
hq6F/ku7mNQ4mgKoOOlXKPhE6dFz9Qhe5k0TE3zIfkXUCCkm+74VqyG5l5vG6/fb
ZmVD0nGM6ErPHB/zQ0WbTm65BMmLIIfNZoVrRwcrQmjj4qJnJg7s+Ar0wfB0Y1kl
91fcxKJ4Mx2uBw7T0T+61DrXKamnqQmTz8g/QsgwwBGLqz9mXQ==
Private-Lines: 14
uvM2bpGRiAQey2PSgAdoQHWn7nv925WBHXx5uSfrUZpx9+HwMjCAy7Y7vDwZUUwT
kYb5PH8acor9aeu7xAaTdUFuYWliHA023pbac9CxdRUE7xMCH8u8rTpo6ZKRKKmS
ExNfEXnk9Veqn2Kr2vbXOd/zc2hiJNrmlMdik1v6RMmOhGfNgHKytlvJbZCF9WDM
gkXpuKBO04cFA241PvJNVC0VdHg+DiNhnqdlUBxW/trR5OieIBgqXLTqeAjGsdt9
066F0DhUCguOrSl5WUlrM3I4ylTqn1Xb9Vr1/7iTnDUaStwGWxjMj9YqgjHjuF/9
Lq0lDdIAriXb9IcvHoU7JWPpOtr5JtGRK0MDAN3YEX4hutPcmFv3F6UHOCFOSBNQ
UTu+Hg+dGzInvN63d0YaZyoEO8n8OpVI8gjrsKsHDgk7crnIyIDi8KKF4skgE4jf
kpVHwW80WBw9zizJlnBFxCnrkigEvI6X+hKTbTM4il5wt7Ixcympgl8EvZc8TaI2
Ksqeg+ONeA2H4Wgi+bzkys111/oTDsC41JfjwWS1lanuKpyYm9SDWTSQf48KV8kH
7fIHQ5O/4RV+f7k19IIWmvCcZTqq9xzjJXtYrjYKbCsptPBq+AQjvXcwkV1DpoGP
q5FKpA0QR4VYlCYqU6h5EdE+EuJpL2r0fEQ1I3pYsH+AUjCvZT1TzB5qvh+0krAw
GI786w9LbuhNUWEGtO8AIS0876bTQq4xLlTlgsZSHG0ElUIHSLlwUQL9RLiN97OZ
sokrwZ/7tFMA4ZK9eOPHXIkgWiF4vnrf1CLN/ePuLKpXsGjYKXnLyQy4cM9tGsEA
cfTBsYqriIvlehgnREdzUiGFxHoJ95WMpK/ESCSwlAAW3EMTs9bZRxyi5hwpmjc8
Private-MAC: 5403e23591ff38245d34db92ef130c86e9789b98


这是公钥文件中的内容:

---- BEGIN SSH2 PUBLIC KEY ----
Comment: "rsa-key-20151217"
AAAAB3NzaC1yc2EAAAABJQAAAQEAl3m8PRlx/SL7EJrs+hDQbP9mp27XXRY4pztg
v8mxAthI2tMEhF0eyXqFV0/W/M13pPs1hHh3H7wIfAy/XbxF7KPeOeMkThMmF2p1
cwJOcpFuh6TCPT09ScPLCR5bsmQyzvsjPMWahcoDrLhf9MGfc9luQs7k1eMTM1iX
hq6F/ku7mNQ4mgKoOOlXKPhE6dFz9Qhe5k0TE3zIfkXUCCkm+74VqyG5l5vG6/fb
ZmVD0nGM6ErPHB/zQ0WbTm65BMmLIIfNZoVrRwcrQmjj4qJnJg7s+Ar0wfB0Y1kl
91fcxKJ4Mx2uBw7T0T+61DrXKamnqQmTz8g/QsgwwBGLqz9mXQ==
---- END SSH2 PUBLIC KEY ----


显然,我不再使用此密钥了,它只是出于说明目的。但是,如何将这个文件读入C#以使用它来签名和验证字符串?

最佳答案

您在Putty中制作一个RSA密钥并尝试使用它,为什么不仅仅使用C#的RSA加密生成密钥对呢?

您可以生成一个公共/私有密钥对,并使用以下方法保存:

//Generate a public/private key pair.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//Save the public key information to an RSAParameters structure.
RSAParameters RSAKeyInfo = RSA.ExportParameters(false);


您可以查看如何使用此密钥对加密数据,示例文档here.

07-24 21:57