Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void encrypt(String password, String secret) throws NoSuchAlgorithmException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchPaddingException, IOException {
- byte[] salt = getSalt();
- byte[] iv = getIV();
- PBEKeySpec spec = new PBEKeySpec(secret.toCharArray(), salt, 100000, 256);
- SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
- byte[] keyBytes = skf.generateSecret(spec).getEncoded();
- SecretKey key = new SecretKeySpec(keyBytes, "AES");
- IvParameterSpec ivspec = new IvParameterSpec(iv);
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
- cipher.init(Cipher.ENCRYPT_MODE, key, ivspec);
- byte[] encrypted = cipher.doFinal(password.getBytes());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement