public byte[] encrypt(byte[] bytes) { synchronized (this.encryptor) { byte[] iv = this.ivGenerator.generateKey(); initCipher(this.encryptor, Cipher.ENCRYPT_MODE, this.secretKey, this.alg.getParameterSpec(iv)); byte[] encrypted = doFinal(this.encryptor, bytes); return this.ivGenerator != NULL_IV_GENERATOR ? concatenate(iv, encrypted) : encrypted; } }
public Cipher createCipher() { return newCipher(this.toString()); }
/** * Generates a SecretKey. */ public static SecretKey newSecretKey(String algorithm, String password) { return newSecretKey(algorithm, new PBEKeySpec(password.toCharArray())); }
/** * Initializes the Cipher for use. */ public static void initCipher(Cipher cipher, int mode, SecretKey secretKey) { initCipher(cipher, mode, secretKey, null); }
public AesBytesEncryptor(String password, CharSequence salt, BytesKeyGenerator ivGenerator) { PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), Hex.decode(salt), 1024, 256); SecretKey secretKey = newSecretKey("PBKDF2WithHmacSHA1", keySpec); this.secretKey = new SecretKeySpec(secretKey.getEncoded(), "AES"); encryptor = newCipher(AES_ALGORITHM); decryptor = newCipher(AES_ALGORITHM); this.ivGenerator = ivGenerator != null ? ivGenerator : NULL_IV_GENERATOR; }
/** * Initializes the Cipher for use. */ public static void initCipher(Cipher cipher, int mode, SecretKey secretKey) { initCipher(cipher, mode, secretKey, null); }
public byte[] decrypt(byte[] encryptedBytes) { synchronized (this.decryptor) { byte[] iv = iv(encryptedBytes); initCipher(this.decryptor, Cipher.DECRYPT_MODE, this.secretKey, this.alg.getParameterSpec(iv)); return doFinal( this.decryptor, this.ivGenerator != NULL_IV_GENERATOR ? encrypted(encryptedBytes, iv.length) : encryptedBytes); } }
/** * Initializes the Cipher for use. */ public static void initCipher(Cipher cipher, int mode, SecretKey secretKey, byte[] salt, int iterationCount) { initCipher(cipher, mode, secretKey, new PBEParameterSpec(salt, iterationCount)); }
/** * Generates a SecretKey. */ public static SecretKey newSecretKey(String algorithm, String password) { return newSecretKey(algorithm, new PBEKeySpec(password.toCharArray())); }
public Cipher createCipher() { return newCipher(this.toString()); }
public byte[] decrypt(byte[] encryptedBytes) { synchronized (this.decryptor) { byte[] iv = iv(encryptedBytes); initCipher(this.decryptor, Cipher.DECRYPT_MODE, this.secretKey, this.alg.getParameterSpec(iv)); return doFinal( this.decryptor, this.ivGenerator != NULL_IV_GENERATOR ? encrypted(encryptedBytes, iv.length) : encryptedBytes); } }
/** * Initializes the Cipher for use. */ public static void initCipher(Cipher cipher, int mode, SecretKey secretKey, byte[] salt, int iterationCount) { initCipher(cipher, mode, secretKey, new PBEParameterSpec(salt, iterationCount)); }
public AesBytesEncryptor(String password, CharSequence salt, BytesKeyGenerator ivGenerator, CipherAlgorithm alg) { PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), Hex.decode(salt), 1024, 256); SecretKey secretKey = newSecretKey("PBKDF2WithHmacSHA1", keySpec); this.secretKey = new SecretKeySpec(secretKey.getEncoded(), "AES"); this.alg = alg; this.encryptor = alg.createCipher(); this.decryptor = alg.createCipher(); this.ivGenerator = ivGenerator != null ? ivGenerator : alg.defaultIvGenerator(); }
public Cipher createCipher() { return newCipher(this.toString()); }
public byte[] encrypt(byte[] bytes) { synchronized (this.encryptor) { byte[] iv = this.ivGenerator.generateKey(); initCipher(this.encryptor, Cipher.ENCRYPT_MODE, this.secretKey, this.alg.getParameterSpec(iv)); byte[] encrypted = doFinal(this.encryptor, bytes); return this.ivGenerator != NULL_IV_GENERATOR ? concatenate(iv, encrypted) : encrypted; } }
/** * Initializes the Cipher for use. */ public static void initCipher(Cipher cipher, int mode, SecretKey secretKey) { initCipher(cipher, mode, secretKey, null); }
public AesBytesEncryptor(String password, CharSequence salt, BytesKeyGenerator ivGenerator, CipherAlgorithm alg) { PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), Hex.decode(salt), 1024, 256); SecretKey secretKey = newSecretKey("PBKDF2WithHmacSHA1", keySpec); this.secretKey = new SecretKeySpec(secretKey.getEncoded(), "AES"); this.alg = alg; this.encryptor = alg.createCipher(); this.decryptor = alg.createCipher(); this.ivGenerator = ivGenerator != null ? ivGenerator : alg.defaultIvGenerator(); }
public Cipher createCipher() { return newCipher(this.toString()); }
public byte[] encrypt(byte[] bytes) { synchronized (encryptor) { byte[] iv = ivGenerator.generateKey(); initCipher(encryptor, Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv)); byte[] encrypted = doFinal(encryptor, bytes); return ivGenerator != NULL_IV_GENERATOR ? concatenate(iv, encrypted) : encrypted; } }
/** * Initializes the Cipher for use. */ public static void initCipher(Cipher cipher, int mode, SecretKey secretKey) { initCipher(cipher, mode, secretKey, null); }