public byte [ ] encrypt ( byte [ ] text , PublicKey key ) throws MangooEncryptionException { Objects . requireNonNull ( text , Required . PLAIN_TEXT . toString ( ) ) ; Objects . requireNonNull ( text , Required . PUBLIC_KEY . toString ( ) ) ; byte [ ] encrypt = null ; try { Cipher cipher = Cipher . getInstance ( ENCRYPTION ) ; cipher . init ( Cipher . ENCRYPT_MODE , key ) ; encrypt = cipher . doFinal ( text ) ; } catch ( NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidKeyException e ) { throw new MangooEncryptionException ( "Failed to encrypt clear text with public key" , e ) ; } return encrypt ; }
Encrypt a text using public key