@Test(groups = "fast") public void failOnECB() throws Exception { String input1 = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; String encrypted1 = CryptoUtil.encrypt(input1); String encrypted2 = CryptoUtil.encrypt(input1); for (int i = 0; i < encrypted1.length() - 4; i++) { Assert.assertFalse( encrypted2.contains(encrypted1.substring(i, i + 4)), "Predictable ECB detected: " + encrypted1 + " " + encrypted2); } }
public static String encrypt(String input) { if (input == null) { input = ""; } Configuration configuration = StripesFilter.getConfiguration(); if (configuration != null && configuration.isDebugMode()) { return input; } try { byte[] inbytes = input.getBytes(); final int inputLength = inbytes.length; byte[] output = new byte[calculateCipherbytes(inputLength) + CIPHER_HMAC_LENGTH]; SecretKey key = getSecretKey(); byte[] iv = generateInitializationVector(); System.arraycopy(iv, 0, output, 0, CIPHER_BLOCK_LENGTH); Cipher cipher = getCipher(key, Cipher.ENCRYPT_MODE, iv, 0, CIPHER_BLOCK_LENGTH); cipher.doFinal(inbytes, 0, inbytes.length, output, CIPHER_BLOCK_LENGTH); hmac(key, output, 0, output.length - CIPHER_HMAC_LENGTH, output, output.length - CIPHER_HMAC_LENGTH); return Base64.encodeBytes(output, BASE64_OPTIONS); } catch (Exception e) { throw new StripesRuntimeException("Could not encrypt value.", e); } }
CryptoUtil { public static String encrypt(String input) { if (input == null) { input = ""; } Configuration configuration = StripesFilter.getConfiguration(); if (configuration != null && configuration.isDebugMode()) { return input; } try { byte[] inbytes = input.getBytes(); final int inputLength = inbytes.length; byte[] output = new byte[calculateCipherbytes(inputLength) + CIPHER_HMAC_LENGTH]; SecretKey key = getSecretKey(); byte[] iv = generateInitializationVector(); System.arraycopy(iv, 0, output, 0, CIPHER_BLOCK_LENGTH); Cipher cipher = getCipher(key, Cipher.ENCRYPT_MODE, iv, 0, CIPHER_BLOCK_LENGTH); cipher.doFinal(inbytes, 0, inbytes.length, output, CIPHER_BLOCK_LENGTH); hmac(key, output, 0, output.length - CIPHER_HMAC_LENGTH, output, output.length - CIPHER_HMAC_LENGTH); return Base64.encodeBytes(output, BASE64_OPTIONS); } catch (Exception e) { throw new StripesRuntimeException("Could not encrypt value.", e); } } }
CryptoUtil { public static String encrypt(String input) { if (input == null) { input = ""; } Configuration configuration = StripesFilter.getConfiguration(); if (configuration != null && configuration.isDebugMode()) { return input; } try { byte[] inbytes = input.getBytes(); final int inputLength = inbytes.length; byte[] output = new byte[calculateCipherbytes(inputLength) + CIPHER_HMAC_LENGTH]; SecretKey key = getSecretKey(); byte[] iv = generateInitializationVector(); System.arraycopy(iv, 0, output, 0, CIPHER_BLOCK_LENGTH); Cipher cipher = getCipher(key, Cipher.ENCRYPT_MODE, iv, 0, CIPHER_BLOCK_LENGTH); cipher.doFinal(inbytes, 0, inbytes.length, output, CIPHER_BLOCK_LENGTH); hmac(key, output, 0, output.length - CIPHER_HMAC_LENGTH, output, output.length - CIPHER_HMAC_LENGTH); return Base64.encodeBytes(output, BASE64_OPTIONS); } catch (Exception e) { throw new StripesRuntimeException("Could not encrypt value.", e); } }  }
CryptoUtil { public static String encrypt(String input) { if (input == null) { input = ""; } Configuration configuration = StripesFilter.getConfiguration(); if (configuration != null && configuration.isDebugMode()) { return input; } try { byte[] inbytes = input.getBytes(); final int inputLength = inbytes.length; byte[] output = new byte[calculateCipherbytes(inputLength) + CIPHER_HMAC_LENGTH]; SecretKey key = getSecretKey(); byte[] iv = generateInitializationVector(); System.arraycopy(iv, 0, output, 0, CIPHER_BLOCK_LENGTH); Cipher cipher = getCipher(key, Cipher.ENCRYPT_MODE, iv, 0, CIPHER_BLOCK_LENGTH); cipher.doFinal(inbytes, 0, inbytes.length, output, CIPHER_BLOCK_LENGTH); hmac(key, output, 0, output.length - CIPHER_HMAC_LENGTH, output, output.length - CIPHER_HMAC_LENGTH); return Base64.encodeBytes(output, BASE64_OPTIONS); } catch (Exception e) { throw new StripesRuntimeException("Could not encrypt value.", e); } }  static String encrypt(String input); static String decrypt(String input); static synchronized void setSecretKey(SecretKey key); }
CryptoUtil { public static String encrypt(String input) { if (input == null) { input = ""; } Configuration configuration = StripesFilter.getConfiguration(); if (configuration != null && configuration.isDebugMode()) { return input; } try { byte[] inbytes = input.getBytes(); final int inputLength = inbytes.length; byte[] output = new byte[calculateCipherbytes(inputLength) + CIPHER_HMAC_LENGTH]; SecretKey key = getSecretKey(); byte[] iv = generateInitializationVector(); System.arraycopy(iv, 0, output, 0, CIPHER_BLOCK_LENGTH); Cipher cipher = getCipher(key, Cipher.ENCRYPT_MODE, iv, 0, CIPHER_BLOCK_LENGTH); cipher.doFinal(inbytes, 0, inbytes.length, output, CIPHER_BLOCK_LENGTH); hmac(key, output, 0, output.length - CIPHER_HMAC_LENGTH, output, output.length - CIPHER_HMAC_LENGTH); return Base64.encodeBytes(output, BASE64_OPTIONS); } catch (Exception e) { throw new StripesRuntimeException("Could not encrypt value.", e); } }  static String encrypt(String input); static String decrypt(String input); static synchronized void setSecretKey(SecretKey key); static final String CONFIG_ENCRYPTION_KEY; }