使用C#中的Boncy Castel库基于密码生成RSA公共/私钥

发布于 2025-02-04 20:41:47 字数 332 浏览 4 评论 0原文

我想使用C#中的Boncy Castel库加密使用密码的私钥生成一个RSA键对,然后将它们保存到分开文件,以便我可以在需要时使用它们来加密或解密事物

。能够生成RSA键盘并将其存储在单独的PEM文件中。我相信它是PKCS1格式,但我似乎无法弄清楚如何基于密码生成Keypair。

我在官方网站上查看了 https://www.bouncycastle.org/csharp t找到任何例子。 我还通过堆栈溢出一直搜索,没有成功。

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...

I'm able to generate an RSA keypair and store them in separate PEM files. I believe it is in PKCS1 format, but I can't seem to figure out how to generate Keypair based on the password.

I looked on the official site, https://www.bouncycastle.org/csharp, but couldn't find any examples.
I also searched all the way through stack overflow with no success.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是年少的欢喜 2025-02-11 20:41:47

至少我想出了如何使用密码生成RSA Keypair来对私钥进行加密,并使用AES-256-CBC与Bouncycastle C#(1.9.2)一起加密钥匙
注意:两个密钥的均为PEM格式。
I got help from the following Link: https://csharp.hotexamples.com/site/file?

//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();

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.
I got help from the following Link: 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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文