title: How to generate Content Encryption Key for JWE implementation
body: <p>I am trying to learn and implement JWE in java for a problem statement. I am trying to understand how does the Content Encryption Key is generated using a certain algorithm(let's say RSA-PKCS1_1.5). </p>
  
  <p>I have knowledge on how to generate a pair of the key using the key generator, then using the public key for encryption and private key for decryption. Also, I am aware of how to create a simple JWT token by giving claims and also how to sign them.
  I am trying to follow the steps: </p>
  
  <p>The message encryption process is as follows:</p>
  
  <p>Generate a random Content Encryption Key (CEK). The CEK MUST have a length at least equal to that of the required encryption keys and MUST be generated randomly. </p>
  
  <p>Encrypt the CEK for the recipient</p>
  
  <p>Generate a random IV (if required for the algorithm).</p>
  
  <p>Compress the Plaintext if a zip parameter was included.</p>
  
  <p>Serialize the (compressed) Plaintext into a bitstring M.</p>
  
  <p>Encrypt M using the CEK and IV to form the bitstring C.</p>
  
  <p>Set the Encoded JWE Ciphertext equal to the base64url encoded representation of C.</p>
  
  <p>Create a JWE Header containing the encryption parameters used. </p>
  
  <p>Base64url encode the bytes of the UTF-8 representation of the JWE Header to create the Encoded JWE Header.</p>
  
  <p>The three encoded parts, taken together, are the result of the encryption.</p>
  
  <pre><code>public static void main(String[] args)
              throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
          //ASYMMETRIC ENCRYPTION
          KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
          keyPairGenerator.initialize(2048);
          KeyPair keys = keyPairGenerator.generateKeyPair();
          byte[] publicKey = keys.getPublic().getEncoded();
          byte[] privateKey = keys.getPrivate().getEncoded();
  
          System.out.println("PUBLIC KEY  ::: " + Base64.encodeBase64String(publicKey));
          System.out.println("PRIVATE KEY ::: " + Base64.encodeBase64String(privateKey));
  
          Cipher cipher = Cipher.getInstance("RSA");
          //PUBLIC KEY IS GETTING USED IN ENCRYPTING PLAIN TEXT
          cipher.init(Cipher.ENCRYPT_MODE, keys.getPublic());
          byte[] encryptedBytes = cipher.doFinal("Test String".getBytes());
          //PRIVATE KEY IS GETTING USED IN DECRYPTING CIPHER TEXT
          cipher.init(Cipher.DECRYPT_MODE, keys.getPrivate());
          byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
  
          System.out.println("encrypted data  ::: " + BaseEncoding.base64().encode(encryptedBytes));
          System.out.println("decrypted text ::: " + new String(decryptedBytes));
  }
  </code></pre>