private byte[] decode(CharSequence encodedPassword) { return Hex.decode(encodedPassword); }
private byte[] decode(String encodedBytes) { if (this.encodeHashAsBase64) { return Base64.getDecoder().decode(encodedBytes); } return Hex.decode(encodedBytes); }
private byte[] decode(String encodedBytes) { if (this.encodeHashAsBase64) { return Base64.getDecoder().decode(encodedBytes); } return Hex.decode(encodedBytes); }
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 String decrypt(String encryptedText) { return Utf8.decode(encryptor.decrypt(Hex.decode(encryptedText))); }
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(); }
@Test public void hexDecode() { byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66, (byte) 67, (byte) 0xC0, (byte) 0xC1, (byte) 0xC2 }; byte[] result = Hex.decode("01ff414243c0c1c2"); assertThat(Arrays.equals(bytes, result)).isTrue(); }
BouncyCastleAesBytesEncryptor(String password, CharSequence salt, BytesKeyGenerator ivGenerator) { if (ivGenerator.getKeyLength() != 16) { throw new IllegalArgumentException("ivGenerator key length != block size 16"); } this.ivGenerator = ivGenerator; PBEParametersGenerator keyGenerator = new PKCS5S2ParametersGenerator(); byte[] pkcs12PasswordBytes = PBEParametersGenerator .PKCS5PasswordToUTF8Bytes(password.toCharArray()); keyGenerator.init(pkcs12PasswordBytes, Hex.decode(salt), 1024); this.secretKey = (KeyParameter) keyGenerator.generateDerivedParameters(256); } }
BouncyCastleAesBytesEncryptor(String password, CharSequence salt, BytesKeyGenerator ivGenerator) { if (ivGenerator.getKeyLength() != 16) { throw new IllegalArgumentException("ivGenerator key length != block size 16"); } this.ivGenerator = ivGenerator; PBEParametersGenerator keyGenerator = new PKCS5S2ParametersGenerator(); byte[] pkcs12PasswordBytes = PBEParametersGenerator .PKCS5PasswordToUTF8Bytes(password.toCharArray()); keyGenerator.init(pkcs12PasswordBytes, Hex.decode(salt), 1024); this.secretKey = (KeyParameter) keyGenerator.generateDerivedParameters(256); } }
@Override public boolean matches(CharSequence rawPassword, String encodedPassword) { byte[] digested = Hex.decode(encodedPassword); byte[] salt = subArray(digested, 0, this.saltGenerator.getKeyLength()); return matches(digested, encodeAndConcatenate(rawPassword, salt)); }
@Before public void setUp() { this.generator = mock(BytesKeyGenerator.class); when(this.generator.generateKey()).thenReturn(Hex.decode("4b0febebd439db7ca77153cb254520c3")); when(this.generator.getKeyLength()).thenReturn(16); }
@Test public void decode() { assertThat(Hex.decode("41424344")).isEqualTo(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D' }); }
@Test public void decodeEmptyString() { assertThat(Hex.decode("")).isEmpty(); }
@Test public void string() { StringKeyGenerator keyGenerator = KeyGenerators.string(); String hexStringKey = keyGenerator.generateKey(); assertThat(hexStringKey.length()).isEqualTo(16); assertThat(Hex.decode(hexStringKey)).hasSize(8); String hexStringKey2 = keyGenerator.generateKey(); assertThat(hexStringKey.equals(hexStringKey2)).isFalse(); }
@Override public boolean matches(CharSequence rawPassword, String encodedPassword) { byte[] digested = Hex.decode(encodedPassword); byte[] salt = subArray(digested, 0, this.saltGenerator.getKeyLength()); return matches(digested, encodeAndConcatenate(rawPassword, salt)); }
@Test public void migrate() { final int saltLength = KeyGenerators.secureRandom().getKeyLength(); String encodedPassword = "ab1146a8458d4ce4e65789e5a3f60e423373cfa10b01abd23739e5ae2fdc37f8e9ede4ae6da65264"; String originalEncodedPassword = "ab1146a8458d4ce4ab1146a8458d4ce4e65789e5a3f60e423373cfa10b01abd23739e5ae2fdc37f8e9ede4ae6da65264"; byte[] originalBytes = Hex.decode(originalEncodedPassword); byte[] fixedBytes = Arrays.copyOfRange(originalBytes, saltLength, originalBytes.length); String fixedHex = String.valueOf(Hex.encode(fixedBytes)); assertThat(fixedHex).isEqualTo(encodedPassword); }
@Test public void decodeNotEven() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Hex-encoded string must have an even number of characters"); Hex.decode("414243444"); }
@Test public void decodeExistNonHexCharAtBoth() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Detected a Non-hex character at 5 or 6 position"); Hex.decode("4142GG"); }
@Test public void decodeExistNonHexCharAtSecond() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Detected a Non-hex character at 3 or 4 position"); Hex.decode("410G"); }
@Test public void decodeExistNonHexCharAtFirst() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Detected a Non-hex character at 1 or 2 position"); Hex.decode("G0"); }