@Test public void cipher() throws Exception { byte[] salt = new byte[16]; byte[] aesNonce = new byte[12]; Random r = new SecureRandom(); r.nextBytes(salt); r.nextBytes(aesNonce); Cipher cipher = HttpAuthUtil.cipher("pwd".toCharArray(), salt, aesNonce, Cipher.ENCRYPT_MODE); assertThat(cipher, notNullValue()); }
static Cipher cipher(char[] masterPassword, byte[] salt, byte[] nonce, int cipherMode) throws HttpAuthException { try { SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); KeySpec keySpec = new PBEKeySpec(masterPassword, salt, 10000, 256); SecretKeySpec spec = new SecretKeySpec(secretKeyFactory.generateSecret(keySpec).getEncoded(), "AES"); Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); cipher.init(cipherMode, spec, new GCMParameterSpec(128, nonce)); return cipher; } catch (Exception e) { throw new HttpAuthException("Failed to prepare a cipher instance", e); } }
HttpAuthUtil { static Cipher cipher(char[] masterPassword, byte[] salt, byte[] nonce, int cipherMode) throws HttpAuthException { try { SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); KeySpec keySpec = new PBEKeySpec(masterPassword, salt, 10000, 256); SecretKeySpec spec = new SecretKeySpec(secretKeyFactory.generateSecret(keySpec).getEncoded(), "AES"); Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); cipher.init(cipherMode, spec, new GCMParameterSpec(128, nonce)); return cipher; } catch (Exception e) { throw new HttpAuthException("Failed to prepare a cipher instance", e); } } }
HttpAuthUtil { static Cipher cipher(char[] masterPassword, byte[] salt, byte[] nonce, int cipherMode) throws HttpAuthException { try { SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); KeySpec keySpec = new PBEKeySpec(masterPassword, salt, 10000, 256); SecretKeySpec spec = new SecretKeySpec(secretKeyFactory.generateSecret(keySpec).getEncoded(), "AES"); Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); cipher.init(cipherMode, spec, new GCMParameterSpec(128, nonce)); return cipher; } catch (Exception e) { throw new HttpAuthException("Failed to prepare a cipher instance", e); } } private  HttpAuthUtil(); }
HttpAuthUtil { static Cipher cipher(char[] masterPassword, byte[] salt, byte[] nonce, int cipherMode) throws HttpAuthException { try { SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); KeySpec keySpec = new PBEKeySpec(masterPassword, salt, 10000, 256); SecretKeySpec spec = new SecretKeySpec(secretKeyFactory.generateSecret(keySpec).getEncoded(), "AES"); Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); cipher.init(cipherMode, spec, new GCMParameterSpec(128, nonce)); return cipher; } catch (Exception e) { throw new HttpAuthException("Failed to prepare a cipher instance", e); } } private  HttpAuthUtil();  }
HttpAuthUtil { static Cipher cipher(char[] masterPassword, byte[] salt, byte[] nonce, int cipherMode) throws HttpAuthException { try { SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); KeySpec keySpec = new PBEKeySpec(masterPassword, salt, 10000, 256); SecretKeySpec spec = new SecretKeySpec(secretKeyFactory.generateSecret(keySpec).getEncoded(), "AES"); Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); cipher.init(cipherMode, spec, new GCMParameterSpec(128, nonce)); return cipher; } catch (Exception e) { throw new HttpAuthException("Failed to prepare a cipher instance", e); } } private  HttpAuthUtil();   }