title: How to generate a JWT in flutter from a key?
body: <p>I have a generate a token from a key in flutter.This token will serve as the header(authorisation) for the API call request. I am using the <a href="https://pub.dev/packages/dart_jsonwebtoken" rel="nofollow noreferrer">https://pub.dev/packages/dart_jsonwebtoken</a> plugin to generate the token</p>
  <p>I am able to generate the token using HS256 algorithm. But in my case I have to generate the token using RS256 algorithm.When I try to generate using RS256 algorithm it throws the following error
  Instance of 'JWTInvalidError'. You can refer the screenshot as well.</p>
  <p>The key is valid because in Java code the person was able to implement that.WHat is missing?</p>
  <p>This is the code I have implemented using flutter dart_jsonwebtoken package:</p>
  <pre><code>import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
  
  String token;
  
  void dartJsonWebTokenGenerator() {
    Duration delay = new Duration( minutes:3);
    final jwt = JWT(
      payload:
      {
        &quot;version&quot;: &quot;13&quot;,
      'user_id': 'XXXXXXXXX',
      },
      issuer: 'https://github.com/jonasroussel/jsonwebtoken',
      audience: 'live-tv',
    );
  
  
  // Sign it (default with HS256 algorithm)
    token = jwt.sign(PrivateKey('XXXXXXXXXXXXXXXXXXXXXXXXX'),
        algorithm: JWTAlgorithm.RS256,expiresIn: delay );
  
    print('Signed token: $token\n');
  }
  </code></pre>
  <p>The equivalent Java code which is to be implemented:</p>
  <pre><code>import android.util.Log;
  
  import java.security.GeneralSecurityException;
  import java.security.KeyFactory;
  import java.security.PrivateKey;
  import java.security.spec.PKCS8EncodedKeySpec;
  import java.util.Date;
  import java.util.concurrent.TimeUnit;
  
  
  public class RSAKeyGenerator {
      private static PrivateKey getPrivateKey() throws GeneralSecurityException {
          String pKey = &quot;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&quot;;
          KeyFactory kf = KeyFactory.getInstance(&quot;RSA&quot;);
          byte[] decode;
          decode = android.util.Base64.decode(pKey, android.util.Base64.DEFAULT);
          PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec(decode);
          return kf.generatePrivate(keySpecPKCS8);
      }
      public static String getJwtToken() {
          final long VALIDITY_MS = TimeUnit.MINUTES.toMillis(60);
          long nowMillis = System.currentTimeMillis();
          Date now = new Date(nowMillis);
          Date exp = new Date(nowMillis + VALIDITY_MS);
          PrivateKey privateKey = null;
          try {
              privateKey = getPrivateKey();
          } catch (GeneralSecurityException e) {
              e.printStackTrace();
          }
          String jws = Jwts.builder()
                  .claim(&quot;version&quot;, &quot;13&quot;)
                  .claim(&quot;user_id&quot;, &quot;xxxxxxxxxxxxxxxxxxx&quot;)
                  .setIssuedAt(now)
                  .setExpiration(exp)
                  .signWith(privateKey, SignatureAlgorithm.RS256)
                  .setAudience(&quot;live-tv&quot;)
                  .compact();
          Log.d(&quot;111__&quot;, jws);
          SpUtil.Companion.getInstance().putString(J_TOKEN, jws);
          return jws;
      }
  }
  </code></pre>
  <p>Along with this I have tried the jose library,corsac_jwt and a few more but I am not getting the proper results in the first place.So if you have any library you can recommend to me ,your are most welcome</p>
  <p><a href="https://i.stack.imgur.com/X3L3s.png" rel="nofollow noreferrer">RSA 256 token error</a></p>