在 VB.NET 加密示例中检索各个私钥

Retrieving the individual private keys in a VB.NET encryption example

我正在使用 Microsoft Docs 中的这个示例来创建加密应用程序: https://docs.microsoft.com/en-us/dotnet/standard/security/walkthrough-creating-a-cryptographic-application

该应用程序允许我加密文件并导出 public 密钥,这允许我加密文本文件,而且它有效。 该应用程序还有一个解密功能,它使用生成的 ASM 密钥对对其进行解密(文档有更多信息)。

该应用程序仅在 .txt 文件中导出一个 Public 密钥,但我还希望它导出一个可以共享以解密的私钥。我如何能够检索和导出私钥,以便我可以使用它单独解密文件,而无需获得完整的密钥对,仅使用私钥。

...but I would also like it to export a private key which can be shared to decrypt.

不共享私钥,只有public密钥!在加密之前,双方首先交换他们的 public 密钥。发送方使用接收方的 public 密钥对消息进行加密。接收方使用其私钥对加密消息进行解密。在这种情况下,私钥不会在任何地方交换(因此术语 private),请参阅章节 3.23.33.4RSA (cyrptosystem).

How would I be able to retrieve and export the private key...

从技术上讲,可以导出私钥。在 RSA 中,public 密钥(字段 ne)是私钥(字段 n, e, d, p, q, d mod(p-1), d mod(q-1), (q 的倒数) mod(p)),即私钥 隐式 也包含 public 密钥,请参阅 RFC8017 中的 A.1.1 RSA Public 密钥语法 A.1.2 RSA 私钥语法 章节。

linked example code, RSA.ToXmlString(Boolean) is used for the key export. RSA.ToXmlString(Boolean) allows you to control with the boolean parameter value whether the private key (i.e. all fields) is exported (true), or whether only the public key (i.e. only n, e) is exported (false). During export the fields are serialized into an XML. Both keys exported in this way can be imported with RSA.FromXmlString(String).