/** * Encrypts the provided byte array. * * @param input * Byte array to be encrypted * @return The encrypted version of the input byte array (in base 64). */ public byte[] encrypt(final byte[] input) { if (input == null || input.length == 0) { return input; } try { BufferedBlockCipher cipher = getCipher(true); byte[] output = new byte[cipher.getOutputSize(input.length)]; int length = cipher.processBytes(input, 0, input.length, output, 0); length += cipher.doFinal(output, length); return Base64.getEncoder().encode(Arrays.copyOfRange(output, 0, length)); } catch (Exception e) { throw new LocalizableValidationException(e, "Encryption error!", "common.ecryption.error"); } }
/** * Encrypts the provided byte array. * @param input * Byte array to be encrypted * @return The encrypted version of the input byte array (in base 64). */ public byte[] encrypt(final byte[] input) { if (input == null || input.length == 0) { return input; } try { BufferedBlockCipher cipher = getCipher(true); byte[] output = new byte[cipher.getOutputSize(input.length)]; int length = cipher.processBytes(input, 0, input.length, output, 0); length += cipher.doFinal(output, length); return Base64.getEncoder().encode(Arrays.copyOfRange(output, 0, length)); } catch (Exception e) { throw new LocalizableValidationException(e, "Encryption error!", "common.ecryption.error"); } }
/** * Decrypts the provided byte array. * @param input * Byte array (in base 64) to be decrypted * @return The decrypted version of the input byte array. */ public byte[] decrypt(final byte[] input) { if (input == null || input.length == 0) { return input; } try { BufferedBlockCipher cipher = getCipher(false); byte[] bytes = Base64.getDecoder().decode(input); byte[] output = new byte[cipher.getOutputSize(bytes.length)]; int length = cipher.processBytes(bytes, 0, bytes.length, output, 0); length += cipher.doFinal(output, length); return Arrays.copyOfRange(output, 0, length); } catch (Exception e) { throw new LocalizableValidationException(e, "Decryption error!", "common.dercyption.error"); } }
/** * Decrypts the provided byte array. * * @param input * Byte array (in base 64) to be decrypted * @return The decrypted version of the input byte array. */ public byte[] decrypt(final byte[] input) { if (input == null || input.length == 0) { return input; } try { BufferedBlockCipher cipher = getCipher(false); byte[] bytes = Base64.getDecoder().decode(input); byte[] output = new byte[cipher.getOutputSize(bytes.length)]; int length = cipher.processBytes(bytes, 0, bytes.length, output, 0); length += cipher.doFinal(output, length); return Arrays.copyOfRange(output, 0, length); } catch (Exception e) { throw new LocalizableValidationException(e, "Decryption error!", "common.dercyption.error"); } }