private void initializeCipher(String kdfName, byte[] kdfOptions, Cipher cipher) throws Buffer.BufferException { if (kdfName.equals(BCRYPT)) { PlainBuffer opts = new PlainBuffer(kdfOptions); byte[] passphrase = new byte[0]; if (pwdf != null) { CharBuffer charBuffer = CharBuffer.wrap(pwdf.reqPassword(null)); ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer); passphrase = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit()); } byte[] keyiv = new byte[48]; new BCrypt().pbkdf(passphrase, opts.readBytes(), opts.readUInt32AsInt(), keyiv); byte[] key = Arrays.copyOfRange(keyiv, 0, 32); byte[] iv = Arrays.copyOfRange(keyiv, 32, 48); cipher.init(Cipher.Mode.Decrypt, key, iv); } else { throw new IllegalStateException("No support for KDF '" + kdfName + "'."); } }
public void testBCryptPbkdfTestVectors() throws Exception { System.out.print("BCrypt.pbkdf w/ known vectors: "); for (BCryptPbkdfTV tv : bcrypt_pbkdf_test_vectors) { byte[] output = new byte[tv.out.length]; new BCrypt().pbkdf(tv.pass, tv.salt, tv.rounds, output); assertEquals(Arrays.toString(tv.out), Arrays.toString(output)); System.out.print("."); } System.out.println(""); } }
private static byte[] generateKayAndIvPbkdf2(byte[] password, byte[] salt, int rounds, int keyLength, int ivLength) { byte[] keyAndIV = new byte[keyLength + ivLength]; new BCrypt().pbkdf(password, salt, rounds, keyAndIV); return keyAndIV; }
private void initializeCipher(String kdfName, byte[] kdfOptions, Cipher cipher) throws Buffer.BufferException { if (kdfName.equals(BCRYPT)) { PlainBuffer opts = new PlainBuffer(kdfOptions); byte[] passphrase = new byte[0]; if (pwdf != null) { CharBuffer charBuffer = CharBuffer.wrap(pwdf.reqPassword(null)); ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer); passphrase = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit()); } byte[] keyiv = new byte[48]; new BCrypt().pbkdf(passphrase, opts.readBytes(), opts.readUInt32AsInt(), keyiv); byte[] key = Arrays.copyOfRange(keyiv, 0, 32); byte[] iv = Arrays.copyOfRange(keyiv, 32, 48); cipher.init(Cipher.Mode.Decrypt, key, iv); } else { throw new IllegalStateException("No support for KDF '" + kdfName + "'."); } }