public String encrypt ( final String transform , final byte [ ] bytes ) { String encodedJSON = null ; if ( secretKey == null ) throw new OSecurityException ( "OSymmetricKey.encrypt() SecretKey is null" ) ; if ( transform == null ) throw new OSecurityException ( "OSymmetricKey.encrypt() Cannot determine cipher transformation" ) ; try { // Throws NoSuchAlgorithmException and NoSuchPaddingException. Cipher cipher = Cipher . getInstance ( transform ) ; // If the cipher transformation requires an initialization vector then init() will create a random one. // (Use cipher.getIV() to retrieve the IV, if it exists.) cipher . init ( Cipher . ENCRYPT_MODE , secretKey ) ; // If the cipher does not use an IV, this will be null. byte [ ] initVector = cipher . getIV ( ) ; // byte[] initVector = encCipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV(); byte [ ] encrypted = cipher . doFinal ( bytes ) ; encodedJSON = encodeJSON ( encrypted , initVector ) ; } catch ( Exception ex ) { throw OException . wrapException ( new OSecurityException ( "OSymmetricKey.encrypt() Exception: " + ex . getMessage ( ) ) , ex ) ; } return encodedJSON ; }
This method encrypts an array of bytes .