@Test public void testUnsupportedAlgoOnDecrypt() throws Exception { String encryptedString = "{$xxx$xoWODkBtRXnSuqIBdosUFA==}"; String decryptedString = new String(crypto.decrypt(encryptedString)); assertEquals("Decryption must fail silently", decryptedString, encryptedString); encryptedString = "{$REVTL0VDQS0NTNsdVBhZGRpbmc=$w/wCDLXoG5VFkNaEpMt0wg==}"; decryptedString = new String(crypto.decrypt(encryptedString)); assertEquals("Decryption must fail silently", decryptedString, encryptedString); }
public byte[] decrypt(String strToDecrypt) { Matcher matcher = CRYPTO_PATTERN.matcher(strToDecrypt); if (!matcher.matches()) { return strToDecrypt.getBytes(); } Cipher decipher; try { String algorithm = new String(Base64.decodeBase64(matcher.group("algo"))); if (StringUtils.isBlank(algorithm)) { algorithm = DEFAULT_ALGO; } decipher = Cipher.getInstance(algorithm); decipher.init(Cipher.DECRYPT_MODE, getSecretKey(algorithm, secretKey)); return decipher.doFinal(Base64.decodeBase64(matcher.group("value"))); } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { log.trace("Available algorithms: " + Security.getAlgorithms("Cipher")); log.trace("Available security providers: " + Arrays.asList(Security.getProviders())); log.debug(e, e); } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | IllegalArgumentException e) { log.debug(e, e); } return strToDecrypt.getBytes(); }
Crypto { public byte[] decrypt(String strToDecrypt) { Matcher matcher = CRYPTO_PATTERN.matcher(strToDecrypt); if (!matcher.matches()) { return strToDecrypt.getBytes(); } Cipher decipher; try { String algorithm = new String(Base64.decodeBase64(matcher.group("algo"))); if (StringUtils.isBlank(algorithm)) { algorithm = DEFAULT_ALGO; } decipher = Cipher.getInstance(algorithm); decipher.init(Cipher.DECRYPT_MODE, getSecretKey(algorithm, secretKey)); return decipher.doFinal(Base64.decodeBase64(matcher.group("value"))); } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { log.trace("Available algorithms: " + Security.getAlgorithms("Cipher")); log.trace("Available security providers: " + Arrays.asList(Security.getProviders())); log.debug(e, e); } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | IllegalArgumentException e) { log.debug(e, e); } return strToDecrypt.getBytes(); } }
Crypto { public byte[] decrypt(String strToDecrypt) { Matcher matcher = CRYPTO_PATTERN.matcher(strToDecrypt); if (!matcher.matches()) { return strToDecrypt.getBytes(); } Cipher decipher; try { String algorithm = new String(Base64.decodeBase64(matcher.group("algo"))); if (StringUtils.isBlank(algorithm)) { algorithm = DEFAULT_ALGO; } decipher = Cipher.getInstance(algorithm); decipher.init(Cipher.DECRYPT_MODE, getSecretKey(algorithm, secretKey)); return decipher.doFinal(Base64.decodeBase64(matcher.group("value"))); } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { log.trace("Available algorithms: " + Security.getAlgorithms("Cipher")); log.trace("Available security providers: " + Arrays.asList(Security.getProviders())); log.debug(e, e); } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | IllegalArgumentException e) { log.debug(e, e); } return strToDecrypt.getBytes(); } Crypto(byte[] secretKey);  Crypto(Map<String, SecretKey> secretKeys);  Crypto(Map<String, SecretKey> secretKeys, char[] digest);  Crypto(String keystorePath, char[] keystorePass, String keyAlias, char[] keyPass); }
Crypto { public byte[] decrypt(String strToDecrypt) { Matcher matcher = CRYPTO_PATTERN.matcher(strToDecrypt); if (!matcher.matches()) { return strToDecrypt.getBytes(); } Cipher decipher; try { String algorithm = new String(Base64.decodeBase64(matcher.group("algo"))); if (StringUtils.isBlank(algorithm)) { algorithm = DEFAULT_ALGO; } decipher = Cipher.getInstance(algorithm); decipher.init(Cipher.DECRYPT_MODE, getSecretKey(algorithm, secretKey)); return decipher.doFinal(Base64.decodeBase64(matcher.group("value"))); } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { log.trace("Available algorithms: " + Security.getAlgorithms("Cipher")); log.trace("Available security providers: " + Arrays.asList(Security.getProviders())); log.debug(e, e); } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | IllegalArgumentException e) { log.debug(e, e); } return strToDecrypt.getBytes(); } Crypto(byte[] secretKey);  Crypto(Map<String, SecretKey> secretKeys);  Crypto(Map<String, SecretKey> secretKeys, char[] digest);  Crypto(String keystorePath, char[] keystorePass, String keyAlias, char[] keyPass); byte[] getSHA1Digest(final byte[] key); byte[] getSHA1DigestOrEmpty(final byte[] bytes); String encrypt(byte[] bytesToEncrypt); String encrypt(String algorithm, byte[] bytesToEncrypt); byte[] decrypt(String strToDecrypt); void clear(); boolean verifyKey(byte[] candidateDigest); boolean verifyKey(char[] candidateDigest); static byte[] getBytes(char[] chars); static char[] getChars(byte[] bytes); static boolean isEncrypted(String value); static Map<String, SecretKey> getKeysFromKeyStore(String keystorePath, char[] keystorePass, String keyAlias,
            char[] keyPass); static void setKeyInKeyStore(String keystorePath, char[] keystorePass, String keyAlias, char[] keyPass,
            SecretKey key); }
Crypto { public byte[] decrypt(String strToDecrypt) { Matcher matcher = CRYPTO_PATTERN.matcher(strToDecrypt); if (!matcher.matches()) { return strToDecrypt.getBytes(); } Cipher decipher; try { String algorithm = new String(Base64.decodeBase64(matcher.group("algo"))); if (StringUtils.isBlank(algorithm)) { algorithm = DEFAULT_ALGO; } decipher = Cipher.getInstance(algorithm); decipher.init(Cipher.DECRYPT_MODE, getSecretKey(algorithm, secretKey)); return decipher.doFinal(Base64.decodeBase64(matcher.group("value"))); } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { log.trace("Available algorithms: " + Security.getAlgorithms("Cipher")); log.trace("Available security providers: " + Arrays.asList(Security.getProviders())); log.debug(e, e); } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | IllegalArgumentException e) { log.debug(e, e); } return strToDecrypt.getBytes(); } Crypto(byte[] secretKey);  Crypto(Map<String, SecretKey> secretKeys);  Crypto(Map<String, SecretKey> secretKeys, char[] digest);  Crypto(String keystorePath, char[] keystorePass, String keyAlias, char[] keyPass); byte[] getSHA1Digest(final byte[] key); byte[] getSHA1DigestOrEmpty(final byte[] bytes); String encrypt(byte[] bytesToEncrypt); String encrypt(String algorithm, byte[] bytesToEncrypt); byte[] decrypt(String strToDecrypt); void clear(); boolean verifyKey(byte[] candidateDigest); boolean verifyKey(char[] candidateDigest); static byte[] getBytes(char[] chars); static char[] getChars(byte[] bytes); static boolean isEncrypted(String value); static Map<String, SecretKey> getKeysFromKeyStore(String keystorePath, char[] keystorePass, String keyAlias,
            char[] keyPass); static void setKeyInKeyStore(String keystorePath, char[] keystorePass, String keyAlias, char[] keyPass,
            SecretKey key); static final String AES; static final String AES_ECB_PKCS5PADDING; static final String DES; static final String DES_ECB_PKCS5PADDING; static final String[] IMPLEMENTED_ALGOS; static final String DEFAULT_ALGO; }