import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public
class
PasswordEncryption {
public
static
void main(String[] args) {
String password =
"mySecretPassword"
;
String salt =
"someUniqueSaltValue"
;
int iterationCount = 40000;
int keyLength = 128;
SecretKeySpec secretKey = createSecretKey(password.toCharArray(), salt, iterationCount, keyLength);
String encryptedPassword = encrypt(password, secretKey);
String decryptedPassword = decrypt(encryptedPassword, secretKey);
System.out.println(
"Original password: "
+ password);
System.out.println(
"Encrypted password: "
+ encryptedPassword);
System.out.println(
"Decrypted password: "
+ decryptedPassword);
}
private
static
SecretKeySpec createSecretKey(char[] password, String salt, int iterationCount, int keyLength) {
try
{
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(
"PBKDF2WithHmacSHA512"
);
PBEKeySpec keySpec =
new
PBEKeySpec(password, salt.getBytes(
"UTF-8"
), iterationCount, keyLength);
return
new
SecretKeySpec(keyFactory.generateSecret(keySpec).getEncoded(),
"AES"
);
}
catch
(NoSuchAlgorithmException | InvalidKeySpecException | UnsupportedEncodingException e) {
throw
new
RuntimeException(e);
}
}
private
static
String encrypt(String password, SecretKeySpec secretKey) {
try
{
Cipher cipher = Cipher.getInstance(
"AES/CBC/PKCS5Padding"
);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return
Base64.getEncoder().encodeToString(cipher.doFinal(password.getBytes(
"UTF-8"
)));
}
catch
(NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException | UnsupportedEncodingException e) {
throw
new
RuntimeException(e);
}
}
private
static
String decrypt(String encryptedPassword, SecretKeySpec secretKey) {
try
{
Cipher cipher = Cipher.getInstance(
"AES/CBC/PKCS5Padding"
);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return
new
String(cipher.doFinal(Base64.getDecoder().decode(encryptedPassword)));
}
catch
(NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | BadPaddingException | IllegalBlockSizeException e) {
throw
new
RuntimeException(e);
}
}
}