由于安全原因,我无法使用jasypt工具。
因此,我创建了一个库,它将从用户那里获取db密码并对其进行加密。
现在,如果我想在我的application.properties文件中使用此加密密码,那么我该如何解密该密码。
加密实现
private static final Charset UTF_8 = StandardCharsets.UTF_8;
/** The Constant KEY. */
private static final String KEY = "SomePassworddKey";
/** The Constant INIT_VECTOR. */
private static final String INIT_VECTOR = "encryptionIntVec";
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the Database Password :");
String dbPassword = scanner.next();
String encryptedString = null;
try {
encryptedString = encrypt(dbPassword);
} catch (Exception e) {
System.out.println(e.getMessage());
scanner.close();
}
System.out.println(encryptedString);
}
private static String encrypt(String value) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
IvParameterSpec iv = new IvParameterSpec(INIT_VECTOR.getBytes(UTF_8));
SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes(UTF_8), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(value.getBytes());
return Base64.encodeBase64String(encrypted);
}