跳到主要内容
ecdsa.com
.NET System.Security.Cryptography.NET

错误信息

No supported key formats were found. Check that the input represents the contents of a PEM-encoded key file, not the path to such a file. (Parameter 'input')

也可能输出为

  • An encrypted key was found, but no password was provided. Use ImportFromEncryptedPem to import this key. (Parameter 'input')
  • Key is not a valid public or private key.

`ENCRYPTED PRIVATE KEY` 块会产生“An encrypted key was found, but no password was provided. Use ImportFromEncryptedPem to import this key.”。

.NET System.Security.Cryptography — ArgumentException from AsymmetricAlgorithm.ImportFromPem

含义

`ImportFromPem` 在字符串中查找标签可以导入到该算法对象的 PEM 块,但一个也没有找到。该方法接收的是 PEM 文本,而不是文件名;`ECDsa` 对象只接受 EC 标签(`PRIVATE KEY`、`EC PRIVATE KEY`,以及包含 EC 密钥的 `PUBLIC KEY`),`RSA` 对象只接受 RSA 标签。如果标签符合,但其中的密钥属于另一种算法,错误则会变为“Key is not a valid public or private key.”。

原因

修复方法

  1. 1.

    把文件内容传给对应算法的对象

    读取文件,创建与密钥算法对应的对象;对于加密的密钥,使用对应的加密版本方法。

    csharp
    using var ecdsa = ECDsa.Create();
    ecdsa.ImportFromPem(File.ReadAllText("/run/secrets/jwt-signing-key.pem")); // contents, not the path
    
    // encrypted PKCS#8:
    ecdsa.ImportFromEncryptedPem(File.ReadAllText(path), Environment.GetEnvironmentVariable("KEY_PASSPHRASE"));
  2. 2.

    按证书的方式加载证书

    从 `X509Certificate2` 中取出公钥,而不是把证书 PEM 当作密钥导入。

    csharp
    // .NET 9+: certificates are loaded with X509CertificateLoader, then the key is taken from them
    var cert = X509CertificateLoader.LoadCertificate(File.ReadAllBytes("cert.der"));
    using ECDsa? publicKey = cert.GetECDsaPublicKey();

相关错误

浏览完整的签名错误库