(无效的 RSA 私钥格式)我似乎在密码学中遇到了某种错误

(Invalid RSA Private key format) I seem to be getting some sort of error within cryptography

提问人:neva 提问时间:10/20/2023 最后编辑:neva 更新时间:10/20/2023 访问量:64

问:

当它读取私钥并将其编码为 Base64 时出现一个问题,当它包含非 base64 字符时,即“----BEGIN”和----END“行,我删除了这些字符,它似乎已经修复了该错误,但现在我在标题中出现错误,即”无效的 RSA 私钥格式”

(这可能不需要,但我的私钥以“MIIC”开头并有换行符)

我是密码学和 C# 的新手,我不断收到一个奇怪的错误,有人知道这个问题的解决方案吗?谢谢。

下面是一段失败的代码:

namespace Backend1618.Controllers
{
    public class SignatureFramework : ControllerBase
    {
        private static RSACryptoServiceProvider? _rsaCsp;
        private static SHA1? _shaCsp;

        public static void Setup()
        {
            try
            {
                string privateKeyPem = System.IO.File.ReadAllText("PrivateKey.pem");
                byte[] privateKeyBytes = Convert.FromBase64String(privateKeyPem);
                RSAParameters rsaParams = DecodeRSAPrivateKey(privateKeyBytes);

                _shaCsp = SHA1.Create();
                _rsaCsp = new RSACryptoServiceProvider();
                _rsaCsp.ImportParameters(rsaParams);

                Console.WriteLine("[SignatureFramework] Successfully initialized SignatureFramework!");
            }
            catch (Exception ex)
            {
                throw new Exception("Error setting up SignatureFramework: " + ex.Message);
            }
        }

        private static RSAParameters DecodeRSAPrivateKey(byte[] privateKeyBytes)
        {
            using (MemoryStream stream = new MemoryStream(privateKeyBytes))
            using (var reader = new BinaryReader(stream))
            {
                ushort version = reader.ReadUInt16();

                if (version != 0x300)
                    throw new ArgumentException("Invalid RSA private key format.");

                ushort modulusLength = reader.ReadUInt16();
                byte[] modulusBytes = reader.ReadBytes(modulusLength);

                ushort exponentLength = reader.ReadUInt16();
                byte[] exponentBytes = reader.ReadBytes(exponentLength);
                string privateKeyPem = System.IO.File.ReadAllText("PrivateKey.pem");

                while (reader.PeekChar() != -1)
                {
                    int v = reader.ReadInt32();
                    privateKeyPem = v.ToString();
                }

                ushort dLength = reader.ReadUInt16();
                byte[] dBytes = reader.ReadBytes(dLength);
                ushort pLength = reader.ReadUInt16();
                byte[] pBytes = reader.ReadBytes(pLength);
                ushort qLength = reader.ReadUInt16();
                byte[] qBytes = reader.ReadBytes(qLength);
                ushort dpLength = reader.ReadUInt16();
                byte[] dpBytes = reader.ReadBytes(dpLength);
                ushort dqLength = reader.ReadUInt16();
                byte[] dqBytes = reader.ReadBytes(dqLength);
                ushort inverseQLength = reader.ReadUInt16();
                byte[] inverseQBytes = reader.ReadBytes(inverseQLength);
                RSAParameters rsaParams = new RSAParameters
                {
                    Modulus = modulusBytes,
                    Exponent = exponentBytes,
                    D = dBytes,
                    P = pBytes,
                    Q = qBytes,
                    DP = dpBytes,
                    DQ = dqBytes,
                    InverseQ = inverseQBytes
                };
                return rsaParams;
            }
        }

更改获取私钥的方法,它仍然报告相同的错误。

C# 加密 签名

评论

0赞 Maarten Bodewes 10/20/2023
幸运的猜测:您使用的是 2048 位私钥:P
1赞 Topaco 10/20/2023
从 .NET 5 (ImportFromPem()) 开始支持导入 PEM 密钥,从 .NET Core 3.0 开始导入 DER 编码密钥,例如 ImportPkcs8Private()。

答:

0赞 Maarten Bodewes 10/20/2023 #1

您正在使用代码读取 Microsoft 专有格式来读取标准化格式。它是 PEM 格式的 PKCS#1 私钥 () 或 PKCS#8 私钥(只是)。RSA PRIVATE KEYPRIVATE KEY