private void testCompatibility(BytesEncryptor left, BytesEncryptor right) throws Exception { // tests that right can decrypt what left encrypted and vice versa // and that the decypted data is the same as the original for (int size = 1; size < 2048; size++) { testData = new byte[size]; secureRandom.nextBytes(testData); byte[] leftEncrypted = left.encrypt(testData); byte[] rightEncrypted = right.encrypt(testData); byte[] leftDecrypted = left.decrypt(rightEncrypted); byte[] rightDecrypted = right.decrypt(leftEncrypted); Assert.assertArrayEquals(testData, leftDecrypted); Assert.assertArrayEquals(testData, rightDecrypted); } }
public String decrypt(String encryptedText) { return Utf8.decode(encryptor.decrypt(Hex.decode(encryptedText))); }
public String encrypt(String text) { return new String(Hex.encode(encryptor.encrypt(Utf8.encode(text)))); }
public String decrypt(String encryptedText) { return Utf8.decode(encryptor.decrypt(Hex.decode(encryptedText))); }
public String encrypt(String text) { return new String(Hex.encode(encryptor.encrypt(Utf8.encode(text)))); }
private void generatesDifferentCipherTexts(BytesEncryptor bcEncryptor) { byte[] encrypted1 = bcEncryptor.encrypt(testData); byte[] encrypted2 = bcEncryptor.encrypt(testData); Assert.assertFalse(Arrays.areEqual(encrypted1, encrypted2)); byte[] decrypted1 = bcEncryptor.decrypt(encrypted1); byte[] decrypted2 = bcEncryptor.decrypt(encrypted2); Assert.assertArrayEquals(testData, decrypted1); Assert.assertArrayEquals(testData, decrypted2); }
@Override public String decrypt(String encryptedText) { byte[] decode = Base64.getDecoder().decode(encryptedText); return new String(encryptor.decrypt(decode), StandardCharsets.UTF_8); } }
@Override public String encrypt(String text) { byte[] encrypt = encryptor.encrypt(text.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(encrypt); }
private void testEquivalence(BytesEncryptor left, BytesEncryptor right) throws Exception { for (int size = 1; size < 2048; size++) { testData = new byte[size]; secureRandom.nextBytes(testData); // tests that right and left generate the same encrypted bytes // and can decrypt back to the original input byte[] leftEncrypted = left.encrypt(testData); byte[] rightEncrypted = right.encrypt(testData); Assert.assertArrayEquals(leftEncrypted, rightEncrypted); byte[] leftDecrypted = left.decrypt(leftEncrypted); byte[] rightDecrypted = right.decrypt(rightEncrypted); Assert.assertArrayEquals(testData, leftDecrypted); Assert.assertArrayEquals(testData, rightDecrypted); } }
@Override public String decrypt(String salt, String password, String encrypted) { BytesEncryptor bytesEncryptor = Encryptors.standard(password, salt); return new String(bytesEncryptor.decrypt(Hex.decode(encrypted)), UTF_CHARSET); }
@Override public String encrypt(String salt, String password, String textToEncrypt) { BytesEncryptor bytesEncryptor = Encryptors.standard(password, salt); return new String(Hex.encode(bytesEncryptor.encrypt(textToEncrypt.getBytes(UTF_CHARSET)))); }
@Test public void standard() throws Exception { CryptoAssumptions.assumeCBCJCE(); BytesEncryptor encryptor = Encryptors.standard("password", "5c0744940b5c369b"); byte[] result = encryptor.encrypt("text".getBytes("UTF-8")); assertThat(result).isNotNull(); assertThat(new String(result).equals("text")).isFalse(); assertThat(new String(encryptor.decrypt(result))).isEqualTo("text"); assertThat(new String(result)).isNotEqualTo( new String(encryptor.encrypt("text".getBytes()))); }
/** * Recovers the data from a token, as a byte array * * @param token A token (key) obtained from this TokenService * @return The original data as a byte array * @throws NullPointerException if token is null * @throws IllegalStateException if data cannot be decrypted using this instance's secret * @throws ArrayIndexOutOfBoundsException if token is malformed */ protected byte[] decryptBytes(String token) { String[] parts = token.split("\\."); String salt = parts[0]; String encrypted = parts[1]; BytesEncryptor decryptor = new BouncyCastleAesGcmBytesEncryptor(secret, salt); return decryptor.decrypt(Base64.getUrlDecoder().decode(encrypted)); } }
public String encrypt(String text) { return new String(Hex.encode(encryptor.encrypt(Utf8.encode(text)))); }
@Test public void stronger() throws Exception { CryptoAssumptions.assumeGCMJCE(); BytesEncryptor encryptor = Encryptors.stronger("password", "5c0744940b5c369b"); byte[] result = encryptor.encrypt("text".getBytes("UTF-8")); assertThat(result).isNotNull(); assertThat(new String(result).equals("text")).isFalse(); assertThat(new String(encryptor.decrypt(result))).isEqualTo("text"); assertThat(new String(result)).isNotEqualTo( new String(encryptor.encrypt("text".getBytes()))); }
public String decrypt(String encryptedText) { return Utf8.decode(encryptor.decrypt(Hex.decode(encryptedText))); }
public String encrypt(String text) { return new String(Hex.encode(encryptor.encrypt(Utf8.encode(text)))); }
public String decrypt(String encryptedText) { return Utf8.decode(encryptor.decrypt(Hex.decode(encryptedText))); }
public String encrypt(String text) { return new String(Hex.encode(encryptor.encrypt(Utf8.encode(text)))); }
public String decrypt(String encryptedText) { return Utf8.decode(encryptor.decrypt(Hex.decode(encryptedText))); }