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).
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:
The message encryption process is as follows:
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.
Encrypt the CEK for the recipient
Generate a random IV (if required for the algorithm).
Compress the Plaintext if a zip parameter was included.
Serialize the (compressed) Plaintext into a bitstring M.
Encrypt M using the CEK and IV to form the bitstring C.
Set the Encoded JWE Ciphertext equal to the base64url encoded representation of C.
Create a JWE Header containing the encryption parameters used.
Base64url encode the bytes of the UTF-8 representation of the JWE Header to create the Encoded JWE Header.
The three encoded parts, taken together, are the result of the encryption.
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));
}