private JWEObject protectPayload ( String payload ) throws IOException { try { JWEHeader jweHeader = new JWEHeader ( "RSA-OAEP" , serverEncryptionKey . kid ( ) , "A128CBC-HS256" ) ; byte [ ] aesKeyBytes = generateAesKey ( ) ; SymmetricKey aesKey = new SymmetricKey ( UUID . randomUUID ( ) . toString ( ) , aesKeyBytes ) ; byte [ ] iv = generateAesIv ( ) ; RsaKey serverEncryptionRsaKey = new RsaKey ( serverEncryptionKey . kid ( ) , serverEncryptionKey . toRSA ( false ) ) ; Triple < byte [ ] , byte [ ] , String > encryptedKey = serverEncryptionRsaKey . encryptAsync ( aesKeyBytes , null , null , "RSA-OAEP" ) . get ( ) ; Triple < byte [ ] , byte [ ] , String > cipher = aesKey . encryptAsync ( payload . getBytes ( MESSAGE_ENCODING ) , iv , MessageSecurityHelper . stringToBase64Url ( jweHeader . serialize ( ) ) . getBytes ( MESSAGE_ENCODING ) , "A128CBC-HS256" ) . get ( ) ; JWEObject jweObject = new JWEObject ( jweHeader , MessageSecurityHelper . bytesToBase64Url ( ( ! testMode ) ? encryptedKey . getLeft ( ) : "key" . getBytes ( MESSAGE_ENCODING ) ) , MessageSecurityHelper . bytesToBase64Url ( iv ) , MessageSecurityHelper . bytesToBase64Url ( cipher . getLeft ( ) ) , MessageSecurityHelper . bytesToBase64Url ( cipher . getMiddle ( ) ) ) ; return jweObject ; } catch ( ExecutionException e ) { // unexpected; return null ; } catch ( InterruptedException e ) { // unexpected; return null ; } catch ( NoSuchAlgorithmException e ) { // unexpected; return null ; } }
Encrypt provided payload and return proper JWEObject .