System.out.println("Original text: \"" + textToEncrypt + "\""); String encryptedText = encryptor.encrypt(textToEncrypt); System.out.println("Encrypted text: \"" + encryptedText + "\""); String decryptedText = decryptor.decrypt(encryptedText); System.out.println("Decrypted text: \"" + decryptedText + "\"");
@RequestMapping(value = "/decrypt/{name}/{profiles}", method = RequestMethod.POST) public String decrypt(@PathVariable String name, @PathVariable String profiles, @RequestBody String data, @RequestHeader("Content-Type") MediaType type) { checkEncryptorInstalled(name, profiles); try { String input = stripFormData(this.helper.stripPrefix(data), type, true); Map<String, String> encryptorKeys = this.helper.getEncryptorKeys(name, profiles, data); TextEncryptor encryptor = this.encryptor.locate(encryptorKeys); String decrypted = encryptor.decrypt(input); logger.info("Decrypted cipher data"); return decrypted; } catch (IllegalArgumentException|IllegalStateException e) { logger.error("Cannot decrypt key:" + name + ", value:" + data, e); throw new InvalidCipherException(); } }
@RequestMapping(value = "/encrypt/{name}/{profiles}", method = RequestMethod.POST) public String encrypt(@PathVariable String name, @PathVariable String profiles, @RequestBody String data, @RequestHeader("Content-Type") MediaType type) { checkEncryptorInstalled(name, profiles); String input = stripFormData(data, type, false); Map<String, String> keys = this.helper.getEncryptorKeys(name, profiles, input); String textToEncrypt = this.helper.stripPrefix(input); String encrypted = this.helper.addPrefix(keys, this.encryptor.locate(keys).encrypt(textToEncrypt)); logger.info("Encrypted data"); return encrypted; }
this.helper.getEncryptorKeys(name, StringUtils .arrayToCommaDelimitedString(environment .getProfiles()), value)).decrypt(this.helper.stripPrefix(value));
private void checkEncryptorInstalled(String name, String profiles) { if (this.encryptor == null) { throw new KeyNotInstalledException(); } if (this.encryptor.locate(this.helper.getEncryptorKeys(name, profiles, "")) .encrypt("FOO").equals("FOO")) { throw new EncryptionTooWeakException(); } }
@Test public void preferred() { CryptoAssumptions.assumeGCMJCE(); TextEncryptor encryptor = Encryptors.delux("password", "5c0744940b5c369b"); String result = encryptor.encrypt("text"); assertThat(result).isNotNull(); assertThat(result.equals("text")).isFalse(); assertThat(encryptor.decrypt(result)).isEqualTo("text"); assertThat(result.equals(encryptor.encrypt("text"))).isFalse(); }
@Override public String decrypt(String encryptedText) { Map<String, String> keys = helper.getEncryptorKeys("configserver", "default", encryptedText); return getLocator().locate(keys).decrypt(helper.stripPrefix(encryptedText)); }
@Override public String encrypt(String text) { Map<String, String> keys = helper.getEncryptorKeys("configserver", "default", text); return getLocator().locate(keys).encrypt(helper.stripPrefix(text)); }
public String decrypt(String str) { if (StringUtils.isNotBlank(str)) { if (StringUtils.startsWith(str, CIPHER_PREFIX)) { str = StringUtils.removeStart(str, CIPHER_PREFIX); } return encryptor.decrypt(str); } else { return str; } }
public String encrypt(String str) { String encrypted = null; if (StringUtils.isNotBlank(str) && !isEncrypted(str)) { encrypted = encryptor.encrypt(str); if (!StringUtils.startsWith(encrypted, CIPHER_PREFIX)) { encrypted = CIPHER_PREFIX + encrypted; } } else { encrypted = str; } return encrypted; }
@Test public void queryableText() { CryptoAssumptions.assumeCBCJCE(); TextEncryptor encryptor = Encryptors.queryableText("password", "5c0744940b5c369b"); String result = encryptor.encrypt("text"); assertThat(result).isNotNull(); assertThat(result.equals("text")).isFalse(); assertThat(encryptor.decrypt(result)).isEqualTo("text"); assertThat(result.equals(encryptor.encrypt("text"))).isTrue(); }
private void doDecrypt(Map<String, Object> overrides) { for (String key : overrides.keySet()) { String value = overrides.get(key).toString(); if (value.startsWith("{cipher}")) { value = value.substring("{cipher}".length()); try { value = this.encryptor.decrypt(value); if (logger.isDebugEnabled()) { logger.debug("Decrypted: key=" + key); } } catch (Exception e) { String message = "Cannot decrypt: key=" + key; if (this.failOnError) { throw new IllegalStateException(message, e); } if (logger.isDebugEnabled()) { logger.warn(message, e); } else { logger.warn(message); } // Set value to empty to avoid making a password out of the // cipher text value = ""; } overrides.put(key, value); } } }
private String encrypt(String text) { return text != null ? textEncryptor.encrypt(text) : text; } }
@Test public void text() { CryptoAssumptions.assumeCBCJCE(); TextEncryptor encryptor = Encryptors.text("password", "5c0744940b5c369b"); String result = encryptor.encrypt("text"); assertThat(result).isNotNull(); assertThat(result.equals("text")).isFalse(); assertThat(encryptor.decrypt(result)).isEqualTo("text"); assertThat(result.equals(encryptor.encrypt("text"))).isFalse(); }
private String decrypt(String encryptedText) { return encryptedText != null ? textEncryptor.decrypt(encryptedText) : encryptedText; }
final String password = "A text password that you need to keep secret."; final String salt = KeyGenerators.string().generateKey(); TextEncryptor encryptor = Encryptors.text(password, salt); String cipherText = encryptor.encrypt(testToEncrypt); String decryptedText = decryptor.decrypt(cipherText);
private String decrypt(String encryptedText) { return encryptedText != null ? textEncryptor.decrypt(encryptedText) : encryptedText; }
private String encrypt(String text) { return text != null ? textEncryptor.encrypt(text) : text; } }
private String decrypt(String encryptedText) { return encryptedText != null ? textEncryptor.decrypt(encryptedText) : encryptedText; }