@Test(groups = "fast") public void decryptBogusInputTest() throws Exception { String input = "_sipApTvfAXjncUGTRUf4OwZJBdz4Mbp2ZxqVyzkKio="; String decrypted = CryptoUtil.decrypt(input); Assert.assertNull(decrypted, "Decrypting a bogus input should give back null."); }
public static String decrypt(String input) { if (input == null) { return null; } Configuration configuration = StripesFilter.getConfiguration(); if (configuration != null && configuration.isDebugMode()) { return input; } byte[] bytes = Base64.decode(input, BASE64_OPTIONS); if (bytes == null || bytes.length < 1) { log.warn("Input is not Base64 encoded: ", input); return null; } if (bytes.length < CIPHER_BLOCK_LENGTH * 2 + CIPHER_HMAC_LENGTH) { log.warn("Input is too short: ", input); return null; } SecretKey key = getSecretKey(); byte[] mac = new byte[CIPHER_HMAC_LENGTH]; try { hmac(key, bytes, 0, bytes.length - CIPHER_HMAC_LENGTH, mac, 0); } catch (Exception e1) { log.warn("Unexpected error performing hmac on: ", input); return null; } boolean validCiphertext; try { validCiphertext = hmacEquals(key, bytes, bytes.length - CIPHER_HMAC_LENGTH, mac, 0); } catch (Exception e1) { log.warn("Unexpected error validating hmac of: ", input); return null; } if (!validCiphertext) { log.warn("Input was not encrypted with the current encryption key (bad HMAC): ", input); return null; } Cipher cipher = getCipher(key, Cipher.DECRYPT_MODE, bytes, 0, CIPHER_BLOCK_LENGTH); byte[] output; try { output = cipher.doFinal(bytes, CIPHER_BLOCK_LENGTH, bytes.length - CIPHER_HMAC_LENGTH - CIPHER_BLOCK_LENGTH); } catch (IllegalBlockSizeException e) { log.warn("Unexpected IllegalBlockSizeException on: ", input); return null; } catch (BadPaddingException e) { log.warn("Unexpected BadPaddingException on: ", input); return null; } return new String(output); }
CryptoUtil { public static String decrypt(String input) { if (input == null) { return null; } Configuration configuration = StripesFilter.getConfiguration(); if (configuration != null && configuration.isDebugMode()) { return input; } byte[] bytes = Base64.decode(input, BASE64_OPTIONS); if (bytes == null || bytes.length < 1) { log.warn("Input is not Base64 encoded: ", input); return null; } if (bytes.length < CIPHER_BLOCK_LENGTH * 2 + CIPHER_HMAC_LENGTH) { log.warn("Input is too short: ", input); return null; } SecretKey key = getSecretKey(); byte[] mac = new byte[CIPHER_HMAC_LENGTH]; try { hmac(key, bytes, 0, bytes.length - CIPHER_HMAC_LENGTH, mac, 0); } catch (Exception e1) { log.warn("Unexpected error performing hmac on: ", input); return null; } boolean validCiphertext; try { validCiphertext = hmacEquals(key, bytes, bytes.length - CIPHER_HMAC_LENGTH, mac, 0); } catch (Exception e1) { log.warn("Unexpected error validating hmac of: ", input); return null; } if (!validCiphertext) { log.warn("Input was not encrypted with the current encryption key (bad HMAC): ", input); return null; } Cipher cipher = getCipher(key, Cipher.DECRYPT_MODE, bytes, 0, CIPHER_BLOCK_LENGTH); byte[] output; try { output = cipher.doFinal(bytes, CIPHER_BLOCK_LENGTH, bytes.length - CIPHER_HMAC_LENGTH - CIPHER_BLOCK_LENGTH); } catch (IllegalBlockSizeException e) { log.warn("Unexpected IllegalBlockSizeException on: ", input); return null; } catch (BadPaddingException e) { log.warn("Unexpected BadPaddingException on: ", input); return null; } return new String(output); } }
CryptoUtil { public static String decrypt(String input) { if (input == null) { return null; } Configuration configuration = StripesFilter.getConfiguration(); if (configuration != null && configuration.isDebugMode()) { return input; } byte[] bytes = Base64.decode(input, BASE64_OPTIONS); if (bytes == null || bytes.length < 1) { log.warn("Input is not Base64 encoded: ", input); return null; } if (bytes.length < CIPHER_BLOCK_LENGTH * 2 + CIPHER_HMAC_LENGTH) { log.warn("Input is too short: ", input); return null; } SecretKey key = getSecretKey(); byte[] mac = new byte[CIPHER_HMAC_LENGTH]; try { hmac(key, bytes, 0, bytes.length - CIPHER_HMAC_LENGTH, mac, 0); } catch (Exception e1) { log.warn("Unexpected error performing hmac on: ", input); return null; } boolean validCiphertext; try { validCiphertext = hmacEquals(key, bytes, bytes.length - CIPHER_HMAC_LENGTH, mac, 0); } catch (Exception e1) { log.warn("Unexpected error validating hmac of: ", input); return null; } if (!validCiphertext) { log.warn("Input was not encrypted with the current encryption key (bad HMAC): ", input); return null; } Cipher cipher = getCipher(key, Cipher.DECRYPT_MODE, bytes, 0, CIPHER_BLOCK_LENGTH); byte[] output; try { output = cipher.doFinal(bytes, CIPHER_BLOCK_LENGTH, bytes.length - CIPHER_HMAC_LENGTH - CIPHER_BLOCK_LENGTH); } catch (IllegalBlockSizeException e) { log.warn("Unexpected IllegalBlockSizeException on: ", input); return null; } catch (BadPaddingException e) { log.warn("Unexpected BadPaddingException on: ", input); return null; } return new String(output); }  }
CryptoUtil { public static String decrypt(String input) { if (input == null) { return null; } Configuration configuration = StripesFilter.getConfiguration(); if (configuration != null && configuration.isDebugMode()) { return input; } byte[] bytes = Base64.decode(input, BASE64_OPTIONS); if (bytes == null || bytes.length < 1) { log.warn("Input is not Base64 encoded: ", input); return null; } if (bytes.length < CIPHER_BLOCK_LENGTH * 2 + CIPHER_HMAC_LENGTH) { log.warn("Input is too short: ", input); return null; } SecretKey key = getSecretKey(); byte[] mac = new byte[CIPHER_HMAC_LENGTH]; try { hmac(key, bytes, 0, bytes.length - CIPHER_HMAC_LENGTH, mac, 0); } catch (Exception e1) { log.warn("Unexpected error performing hmac on: ", input); return null; } boolean validCiphertext; try { validCiphertext = hmacEquals(key, bytes, bytes.length - CIPHER_HMAC_LENGTH, mac, 0); } catch (Exception e1) { log.warn("Unexpected error validating hmac of: ", input); return null; } if (!validCiphertext) { log.warn("Input was not encrypted with the current encryption key (bad HMAC): ", input); return null; } Cipher cipher = getCipher(key, Cipher.DECRYPT_MODE, bytes, 0, CIPHER_BLOCK_LENGTH); byte[] output; try { output = cipher.doFinal(bytes, CIPHER_BLOCK_LENGTH, bytes.length - CIPHER_HMAC_LENGTH - CIPHER_BLOCK_LENGTH); } catch (IllegalBlockSizeException e) { log.warn("Unexpected IllegalBlockSizeException on: ", input); return null; } catch (BadPaddingException e) { log.warn("Unexpected BadPaddingException on: ", input); return null; } return new String(output); }  static String encrypt(String input); static String decrypt(String input); static synchronized void setSecretKey(SecretKey key); }
CryptoUtil { public static String decrypt(String input) { if (input == null) { return null; } Configuration configuration = StripesFilter.getConfiguration(); if (configuration != null && configuration.isDebugMode()) { return input; } byte[] bytes = Base64.decode(input, BASE64_OPTIONS); if (bytes == null || bytes.length < 1) { log.warn("Input is not Base64 encoded: ", input); return null; } if (bytes.length < CIPHER_BLOCK_LENGTH * 2 + CIPHER_HMAC_LENGTH) { log.warn("Input is too short: ", input); return null; } SecretKey key = getSecretKey(); byte[] mac = new byte[CIPHER_HMAC_LENGTH]; try { hmac(key, bytes, 0, bytes.length - CIPHER_HMAC_LENGTH, mac, 0); } catch (Exception e1) { log.warn("Unexpected error performing hmac on: ", input); return null; } boolean validCiphertext; try { validCiphertext = hmacEquals(key, bytes, bytes.length - CIPHER_HMAC_LENGTH, mac, 0); } catch (Exception e1) { log.warn("Unexpected error validating hmac of: ", input); return null; } if (!validCiphertext) { log.warn("Input was not encrypted with the current encryption key (bad HMAC): ", input); return null; } Cipher cipher = getCipher(key, Cipher.DECRYPT_MODE, bytes, 0, CIPHER_BLOCK_LENGTH); byte[] output; try { output = cipher.doFinal(bytes, CIPHER_BLOCK_LENGTH, bytes.length - CIPHER_HMAC_LENGTH - CIPHER_BLOCK_LENGTH); } catch (IllegalBlockSizeException e) { log.warn("Unexpected IllegalBlockSizeException on: ", input); return null; } catch (BadPaddingException e) { log.warn("Unexpected BadPaddingException on: ", input); return null; } return new String(output); }  static String encrypt(String input); static String decrypt(String input); static synchronized void setSecretKey(SecretKey key); static final String CONFIG_ENCRYPTION_KEY; }