title: Creating signature in Java with existing public key for JWT
body: <p>I need your help once again...</p>
  
  <p>I want to access the Box API and authorize using a JWT (Json Web Token). To do so, I need to create a Assertion:<br>
  <em>"Every JWT assertion is composed of three components, the header, the claims, and the signature.<br>
   - The header specifies the algorithm used for the JWT signature.<br>
   - The claims contain the information necessary to authenticate and provide the correct token.<br>
   - The signature is used to verify the identify of the application and is verified using the public key.<br><br>
  Once encoded and concatenated, the JWT assertion will look like this:
  eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9eyJpc3MiOiJ2Z3.
  B2bWFvaDJjZ2ZjNGRuMzFnMWx0cmlhbmdlZCIsInN1YiI.
  6IjE2ODczOTQzIiwiZXhwIjoxNDI5MDM3ODYwLCJqdGkiOiJ"</em></p>
  
  <p>So, an RSA Keypair I had to create before and had to deposit the public key in the Box devolper application.</p>
  
  <p>Now, I don't know how to create the signature.
  I found a solution with creating the Keypair, but since I already have this, I don't know how to modify the code.</p>
  
  <pre><code>import java.security.KeyPair;
  import java.security.KeyPairGenerator;
  import java.security.Signature;
  
  import sun.misc.BASE64Encoder;
  
  public class MainClass {
    public static void main(String[] args) throws Exception {
      KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
      kpg.initialize(1024);
      KeyPair keyPair = kpg.genKeyPair();
  
      byte[] data = "test".getBytes("UTF8");
  
      Signature sig = Signature.getInstance("MD5WithRSA");
      sig.initSign(keyPair.getPrivate());
      sig.update(data);
      byte[] signatureBytes = sig.sign();
      System.out.println("Singature:" + new  
             BASE64Encoder().encode(signatureBytes));
  
      sig.initVerify(keyPair.getPublic());
      sig.update(data);
  
      System.out.println(sig.verify(signatureBytes));
    }
  }
  </code></pre>