【问题标题】:How to read der file and convert to AsymmetricCipherKeyPair?如何读取 der 文件并转换为 AsymmetricCipherKeyPair?
【发布时间】:2012-01-12 08:36:16
【问题描述】:

我有一个包含 DER 编码密钥的 der 文件“text.der”。我想阅读它并从 Bouncycastle C# 库(here are the javadocs for the Java version) 转换为 AsymmetricCipherKeyPair 的实例。

例如对于 pem 文件,我们在 bouncycastle 中有 PemReader/Writer,我们可以做到。 如何从文件中的编码密钥转到 AsymmetricCipherKeyPair

【问题讨论】:

    标签: c# rsa bouncycastle


    【解决方案1】:

    假设它是通常的二进制格式 DER 公钥文件,对 SubjectPublicKeyInfo 结构使用二进制 DER 编码(我认为 OpenSSL 使用它作为它的 DER 输出格式),你可以这样做:

    byte[] derKeyBytes = File.ReadAllBytes("text.der"); // read in the binary file
    
    // Decode the public key component
    AsymmetricKeyParameter publicKey =
        PublicKeyFactory.CreateKey(derKeyBytes);
    

    你最好只使用 AsymmetricKeyParameter(这是密钥的公共部分),但如果你绝对希望它在 AsymmetricCipherKeyPair 中,你可以这样做:

    // Put the public key into a keyPair, leave the Private key uninitialized.
    AsymmetricCipherKeyPair keyPair =
        new AsymmetricCipherKeyPair(
            publicKey,
            new AsymmetricKeyParameter(true));
    

    【讨论】:

      猜你喜欢
      • 2019-08-17
      • 2022-01-10
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      • 2019-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多