c# - 在 .NET 中使用 PEM 编码的 RSA 私钥

标签 c# .net cryptography rsa private-key

我有一个私钥,如下所示:

-----BEGIN RSA PRIVATE KEY----- Some private key data -----END RSA PRIVA

我需要在我的 C# 项目中使用这个 key ,但我找不到任何示例如何以这种格式使用 key 。谢谢

最佳答案

  1. 第 1 步获取“一些私钥数据”内容。删除 -----BEGIN RSA PRIVATE KEY----- 和 -----END RSA PRIVATE KEY-----, 删除所有行符号( "\n");
  2. 第 2 步。将 key 解析为 RSA。
private RSACryptoServiceProvider CreateRsaProviderFromPrivateKey(string privateKey)
{
    var privateKeyBits = System.Convert.FromBase64String(privateKey);

    var RSA = new RSACryptoServiceProvider();
    var RSAparams = new RSAParameters();

    using (BinaryReader binr = new BinaryReader(new MemoryStream(privateKeyBits)))
    {
        byte bt = 0;
        ushort twobytes = 0;
        twobytes = binr.ReadUInt16();
        if (twobytes == 0x8130)
            binr.ReadByte();
        else if (twobytes == 0x8230)
            binr.ReadInt16();
        else
            throw new Exception("Unexpected value read binr.ReadUInt16()");

        twobytes = binr.ReadUInt16();
        if (twobytes != 0x0102)
            throw new Exception("Unexpected version");

        bt = binr.ReadByte();
        if (bt != 0x00)
            throw new Exception("Unexpected value read binr.ReadByte()");

        RSAparams.Modulus = binr.ReadBytes(GetIntegerSize(binr));
        RSAparams.Exponent = binr.ReadBytes(GetIntegerSize(binr));
        RSAparams.D = binr.ReadBytes(GetIntegerSize(binr));
        RSAparams.P = binr.ReadBytes(GetIntegerSize(binr));
        RSAparams.Q = binr.ReadBytes(GetIntegerSize(binr));
        RSAparams.DP = binr.ReadBytes(GetIntegerSize(binr));
        RSAparams.DQ = binr.ReadBytes(GetIntegerSize(binr));
        RSAparams.InverseQ = binr.ReadBytes(GetIntegerSize(binr));
    }

    RSA.ImportParameters(RSAparams);
    return RSA;
}

private int GetIntegerSize(BinaryReader binr)
{
    byte bt = 0;
    byte lowbyte = 0x00;
    byte highbyte = 0x00;
    int count = 0;
    bt = binr.ReadByte();
    if (bt != 0x02)
        return 0;
    bt = binr.ReadByte();

    if (bt == 0x81)
        count = binr.ReadByte();
    else
        if (bt == 0x82)
        {
            highbyte = binr.ReadByte();
            lowbyte = binr.ReadByte();
            byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
            count = BitConverter.ToInt32(modint, 0);
        }
        else
        {
            count = bt;
        }

    while (binr.ReadByte() == 0x00)
    {
        count -= 1;
    }
    binr.BaseStream.Seek(-1, SeekOrigin.Current);
    return count;
}

关于c# - 在 .NET 中使用 PEM 编码的 RSA 私钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14644926/

相关文章:

C# WPF/WinForms - 在 WPF/WinForm 中调用异步方法永远不会完成

.net - 使用VB.Net捕获所有键盘事件

python - 我有模数和私有(private)指数。如何构造RSA私钥并对消息进行签名?

c# - Bouncy CaSTLe C# 可以与 .NET 密码学一起使用吗

c# - 错误 "Input length must be multiple of 8 when decrypting with padded cipher"

c# - Linq to SQL - 我应该如何管理数据库请求?

c# - 如何用用户输入替换文本文件中一行的一部分?

c# - 使用 C# sdk 退出 FB

c# - 如何以编程方式使用 c sharp 调用 Microsoft 报表查看器的打印按钮

algorithm - 为什么 Shamir Secret Sharing 使用拉格朗日多项式?