Skip to content
ecdsa.com
.NET System.Security.Cryptography.NET

Error message

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')

Also printed as

  • 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.

An `ENCRYPTED PRIVATE KEY` block gives "An encrypted key was found, but no password was provided. Use ImportFromEncryptedPem to import this key."

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

What it means

`ImportFromPem` scanned the string for a PEM block whose label it can import into this algorithm object, and found none. The method takes the PEM text, not a file name, and an `ECDsa` object only accepts EC labels (`PRIVATE KEY`, `EC PRIVATE KEY`, `PUBLIC KEY` with an EC key), an `RSA` object only RSA ones. When the label fits but the key inside belongs to another algorithm, the error is "Key is not a valid public or private key." instead.

Why it happens

How to fix it

  1. 1.

    Pass the file contents to the right algorithm

    Read the file, create the object of the key's algorithm, and use the encrypted variant for encrypted keys.

    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.

    Load certificates as certificates

    Take the public key from an `X509Certificate2` instead of importing the certificate PEM as a key.

    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();

Related errors

Browse the full signature error database