/** * Creates a text encryptor that uses "stronger" password-based encryption. Encrypted * text is hex-encoded. * * @param password the password used to generate the encryptor's secret key; should * not be shared * @see Encryptors#stronger(CharSequence, CharSequence) */ public static TextEncryptor delux(CharSequence password, CharSequence salt) { return new HexEncodingTextEncryptor(stronger(password, salt)); }
/** * Creates a text encryptor that uses "stronger" password-based encryption. Encrypted * text is hex-encoded. * * @param password the password used to generate the encryptor's secret key; should * not be shared * @see Encryptors#stronger(CharSequence, CharSequence) */ public static TextEncryptor delux(CharSequence password, CharSequence salt) { return new HexEncodingTextEncryptor(stronger(password, salt)); }
@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()))); }
/** * Creates a text encryptor that uses "stronger" password-based encryption. Encrypted * text is hex-encoded. * * @param password the password used to generate the encryptor's secret key; should * not be shared * @see Encryptors#stronger(CharSequence, CharSequence) */ public static TextEncryptor delux(CharSequence password, CharSequence salt) { return new HexEncodingTextEncryptor(stronger(password, salt)); }
/** * Creates a text encryptor that uses "stronger" password-based encryption. Encrypted * text is hex-encoded. * * @param password the password used to generate the encryptor's secret key; should * not be shared * @see Encryptors#stronger(CharSequence, CharSequence) */ public static TextEncryptor delux(CharSequence password, CharSequence salt) { return new HexEncodingTextEncryptor(stronger(password, salt)); }
/** * Creates a text encryptor that uses "stronger" password-based encryption. Encrypted * text is hex-encoded. * * @param password the password used to generate the encryptor's secret key; should * not be shared * @see Encryptors#stronger(CharSequence, CharSequence) */ public static TextEncryptor delux(CharSequence password, CharSequence salt) { return new HexEncodingTextEncryptor(stronger(password, salt)); }
private static byte[] decrypt(byte[] text, PrivateKey key, RsaAlgorithm alg, String salt, boolean gcm) { ByteArrayInputStream input = new ByteArrayInputStream(text); ByteArrayOutputStream output = new ByteArrayOutputStream(text.length); try { int length = readInt(input); byte[] random = new byte[length]; input.read(random); final Cipher cipher = Cipher.getInstance(alg.getJceName()); cipher.init(Cipher.DECRYPT_MODE, key); String secret = new String(Hex.encode(cipher.doFinal(random))); byte[] buffer = new byte[text.length - random.length - 2]; input.read(buffer); BytesEncryptor aes = gcm ? Encryptors.stronger(secret, salt) : Encryptors .standard(secret, salt); output.write(aes.decrypt(buffer)); return output.toByteArray(); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new IllegalStateException("Cannot decrypt", e); } }
private static byte[] encrypt(byte[] text, PublicKey key, RsaAlgorithm alg, String salt, boolean gcm) { byte[] random = KeyGenerators.secureRandom(16).generateKey(); BytesEncryptor aes = gcm ? Encryptors.stronger(new String(Hex.encode(random)), salt) : Encryptors.standard(new String(Hex.encode(random)), salt); try { final Cipher cipher = Cipher.getInstance(alg.getJceName()); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] secret = cipher.doFinal(random); ByteArrayOutputStream result = new ByteArrayOutputStream(text.length + 20); writeInt(result, secret.length); result.write(secret); result.write(aes.encrypt(text)); return result.toByteArray(); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new IllegalStateException("Cannot encrypt", e); } }