简体   繁体   English

在 c# 中使用 bouncy Castel 库根据密码生成 RSA 公钥/私钥

[英]generate RSA public/private key based on a passphrase using bouncy Castel library in c#

I'd like to generate an RSA key pair with a private key encrypted with passphrase using the Bouncy Castel Library in C# and save them to separate files so that I can use them to encrypt or decrypt things whenever I need to...我想使用 C# 中的 Bouncy Castel 库生成带有密码短语加密的私钥的 RSA 密钥对,并将它们保存到单独的文件中,以便我可以在需要时使用它们来加密或解密事物...

I'm able to generate an RSA keypair and store them in separate PEM files.我能够生成一个 RSA 密钥对并将它们存储在单独的 PEM 文件中。 I believe it is in PKCS1 format, but I can't seem to figure out how to generate Keypair based on the password.我相信它是 PKCS1 格式,但我似乎无法弄清楚如何根据密码生成密钥对。

I looked on the official site, https://www.bouncycastle.org/csharp , but couldn't find any examples.我查看了官方网站https://www.bouncycastle.org/csharp ,但找不到任何示例。 I also searched all the way through stack overflow with no success.我还一直搜索堆栈溢出,但没有成功。

At least I figured out How to generate RSA Keypair with passphrase to encrypt the private key, using AES-256-CBC with BouncyCastle C# (1.9.2) Note: Both the key's are in PEM format.至少我想出了如何使用带有 BouncyCastle C# (1.9.2) 的 AES-256-CBC 生成带有密码的 RSA 密钥对来加密私钥注意:两个密钥都是 PEM 格式。 I got help from the following Link: https://csharp.hotexamples.com/site/file?hash=0xdc447ca00ad5a1a69649f92339871f7caeada0c6c47b8b39ccad91e46cf75d74&fullName=Certificate.cs&project=nicholaspaun/Kalkulator1我从以下链接获得帮助: https ://csharp.hotexamples.com/site/file?hash=0xdc447ca00ad5a1a69649f92339871f7caeada0c6c47b8b39ccad91e46cf75d74&fullName=Certificate.cs&project=nicholaspaun/Kalkulator1

//Create Random
CryptoApiRandomGenerator randomGenerator = new CryptoApiRandomGenerator();

//RSAKeyPairGenerator generates the RSA keypair based on the random number and strength of the key required
RsaKeyPairGenerator rsaKeyPairGen = new RsaKeyPairGenerator();
rsaKeyPairGen.Init(new KeyGenerationParameters(new SecureRandom(randomGenerator), 2048));
AsymmetricCipherKeyPair keyPair = rsaKeyPairGen.GenerateKeyPair();

//Extracting the private key from the pair
RsaKeyParameters Privatekey = (RsaKeyParameters)keyPair.Private;

//Extracting the public key from the pair
RsaKeyParameters Publickey = (RsaKeyParameters)keyPair.Public;

//Creating public key in pem format\
TextWriter pubtxtWriter = new StringWriter();
PemWriter pubpemWriter = new PemWriter(pubtxtWriter);
pubpemWriter.WriteObject(Publickey);
pubpemWriter.Writer.Flush();
//now save the follwing string variable into a file. that's our public key
string print_publicKey = pubtxtWriter.ToString();

//encrypted Private Key
string password = "xxxxx";  //give desired password, with good strength
AsymmetricKeyParameter privateKey = keyPair.Private;
StringWriter sw = new StringWriter();
PemWriter pw = new PemWriter(sw);
pw.WriteObject(privateKey, "AES-256-CBC", password.ToCharArray(), new SecureRandom());
pw.Writer.Close();
pw.Writer.Flush();
//now save the follwing string variable into a file. that's our "Encrypted private key"
string encprvKey = sw.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM