Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

ASP.NET Core cannot create the JWT because the signing key is too short or unusable

ErrorIDX10720: Unable to create KeyedHashAlgorithm for algorithm 'HS256', the key size must be greater than: '<bits>' bits, key has '<bits>' bits.

Short answer

The error happens while the API signs the token, not while it checks one. IdentityModel builds a signature provider from your key and algorithm and refuses when the HS256 secret is too short, when an RSA key has no private part, or when the algorithm name is not a supported one. The IDX code in the message tells which.

  • Applies to .NET 9, .NET 10, Microsoft.IdentityModel.Tokens 8.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print what you are signing with: how many bits the key has and which algorithm name you pass. The IDX number in the exception tells you which of the causes below applies.

    var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret));
    Console.WriteLine($"key size: {key.KeySize} bits, algorithm: {SecurityAlgorithms.HmacSha256}");
  2. 2Match what you see, then jump to its fix

    1. 1IDX10720 or IDX10630, and the printed key size is well under 256 bits. A 16 character secret read as UTF-8 text gives only 128 bits.The HS256 secret is shorter than 32 bytesFix
    2. 2IDX10638 says key.HasPrivateKey is false.The RSA key has no private part, so nothing can be signedFix
    3. 3IDX10634 prints an Algorithm value and a SecurityKey and says that this combination is not supported.The algorithm string, or its pairing with the key, is not supportedFix

What does the error look like?

  • IDX10720: Unable to create KeyedHashAlgorithm for algorithm 'HS256', the key size must be greater than: '<bits>' bits, key has '<bits>' bits.
  • IDX10630: The '<key type>' for signing cannot be smaller than '<bits>' bits. KeySize: '<bits>'.
  • IDX10638: Cannot create the SignatureProvider, 'key.HasPrivateKey' is false, cannot create signatures.
  • IDX10634: Unable to create the SignatureProvider.

Cause 1 of 3

The HS256 secret is shorter than 32 bytes

1Check

The JWA standard requires a key at least as large as the hash output for HMAC signatures, which is 256 bits for HS256. IdentityModel also has its own minimum key size for signing, and it throws when your key is below it.

Console.WriteLine($"{secret.Length} characters, {key.KeySize} bits");

2Fix

Generate 32 or more random bytes, store them base64-encoded in your secret store or configuration, and decode them into the key. Use the same bytes wherever tokens are validated, otherwise tokens made with the old secret stop validating.

# one-time: create a 32-byte secret
openssl rand -base64 32

// C#: decode the stored value instead of encoding a phrase
var key = new SymmetricSecurityKey(Convert.FromBase64String(config["Jwt:Key"]!));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

3Verify

The printed size is 256 bits or more and the handler returns a token string.

Console.WriteLine(key.KeySize);
Console.WriteLine(new JwtSecurityTokenHandler().WriteToken(new JwtSecurityToken(signingCredentials: creds)));

Confirmed by rfc-editor.org

Did this fix it?

Cause 2 of 3

The RSA key has no private part, so nothing can be signed

1Check

Signing needs the private key. A certificate loaded from a .cer file, or an RSA key built from public material only, carries just the public half. HasPrivateKey tells you which one you have.

var cert = X509CertificateLoader.LoadPkcs12FromFile("signing.pfx", pfxPassword);
Console.WriteLine(cert.HasPrivateKey);

2Fix

Load a PFX (PKCS#12) file that contains the private key, and build the signing key from that certificate. Keep the public certificate for the services that only validate tokens.

var cert = X509CertificateLoader.LoadPkcs12FromFile("signing.pfx", pfxPassword);
var creds = new SigningCredentials(new X509SecurityKey(cert), SecurityAlgorithms.RsaSha256);

3Verify

HasPrivateKey prints True and the token is written without IDX10638.

Console.WriteLine(cert.HasPrivateKey);

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 3

The algorithm string, or its pairing with the key, is not supported

1Check

The message prints the algorithm string it received next to the key, and links to the list of supported algorithms. Compare your string with the names on that list, and check that it suits the kind of key you pass.

grep -rn "SigningCredentials(" --include=*.cs .

2Fix

Pass one of the SecurityAlgorithms constants from the supported list, for example HmacSha256 for an HS256 token and RsaSha256 for RS256.

new SigningCredentials(key, SecurityAlgorithms.HmacSha256)
// for an RSA or certificate key:
// new SigningCredentials(rsaKey, SecurityAlgorithms.RsaSha256)

3Verify

The exception no longer appears when you write the token.

var jwt = new JwtSecurityTokenHandler().WriteToken(token);

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Create signing secrets with a random generator and keep them out of source control, instead of typing a phrase.
  • Check the key size at start-up and fail fast with a clear message when it is under 256 bits.
  • Use the SecurityAlgorithms constants and never raw algorithm strings.
  • Keep the signing key and the validation key in one shared setting so they can not drift apart.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Security

A valid JWT fails to decode because its segments are base64url, not base64

Convert.FromBase64String throws FormatException on a JWT segment

A JWT is three base64url segments joined by dots, and a standard base64 decoder refuses all three differences: base64url uses - and _ instead of + and /, leaves off the = padding, and the dots are not part of any base64 alphabet.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.