title: Error Creating JWT Token using RSA Security Key with key size less than 2048
body: <p>I'm facing an exception when trying to create a <a href="https://jwt.io/" rel="nofollow noreferrer">JWT</a> token in C# Web API application.</p>
  
  <p><strong>Test environment</strong></p>
  
  <ul>
  <li>Platform: Windows 10 x64 with .net framework: 4.6.1</li>
  <li>jwt NuGet package: <a href="https://www.nuget.org/packages/System.IdentityModel.Tokens.Jwt/4.0.2.206221351" rel="nofollow noreferrer">System.IdentityModel.Tokens.Jwt version: 4.0.2.206221351</a></li>
  </ul>
  
  <p>Here is the code responsible for generating RSA keys:</p>
  
  <pre><code>    public SignatureInformation CreateNewSignatureInformation(int length = 0)
      {
          try
          {
              var signatureInformation = new SignatureInformation();
  
              var rsaProvider = new RSACryptoServiceProvider(length);
  
              var publicKey = rsaProvider.ToXmlString(false);
              signatureInformation.Public = publicKey;
  
              var privateKey = rsaProvider.ToXmlString(true);
              signatureInformation.Private = privateKey;
  
              return signatureInformation;
          }
          catch (Exception)
          {
              return null;
          }
      }
  </code></pre>
  
  <p>And here is the code responsible for generating a JTW token signed with a RSA key pair generated with the above code:</p>
  
  <pre><code>    private string GenerateToken(string privateKeyInXml)
      {
          var cryptoServiceProvider = new RSACryptoServiceProvider();
          cryptoServiceProvider.FromXmlString(privateKeyInXml);
  
          var creds = new SigningCredentials(new RsaSecurityKey(cryptoServiceProvider),
              SecurityAlgorithms.RsaSha256Signature,
              SecurityAlgorithms.Sha256Digest);
  
          var nbf = DateTime.UtcNow;
          var exp = nbf.AddMinutes(10);
          var jwtToken = new JwtSecurityToken("SAMPLE_ISSUER",
              "SAMPLE_AUDIENCE",
              null, //null is given as claims list in this sample
              nbf,
              exp,
              creds);
  
          var tokenHandler = new JwtSecurityTokenHandler();
          var token = tokenHandler.WriteToken(jwtToken); //the exception is thrown here
          return token;
      }
  </code></pre>
  
  <p>the exception thrown is of type <code>System.ArgumentOutOfRangeException</code> and the message is:</p>
  
  <p><code>"IDX10630: The 'System.IdentityModel.Tokens.RsaSecurityKey' for signing cannot be smaller than '2048' bits.\r\nParameter name: key.KeySize\r\nActual value was 512."</code></p>
  
  <p><strong>Question</strong></p>
  
  <p>How can I generate a JWT Token using an RSA Security Key with key size less than 2048?</p>