我有一个应用程序需要在配置文件中存储一些秘密密码,如数据库和ftp密码/详细信息.我环顾四周,发现了许多使用AES的加密/解密解决方案,但我似乎无法弄清楚如何在不更改密钥的情况下使其工作.这意味着我可以加密和解密(使用相同的SecretKey),但是在重启等时保持持久性.我似乎无法使SecretKey保持不变.以下示例显示了我的方法:
String secret = Encryptor.encrpytString("This is secret");
String test = Encryptor.decrpytString(secret);
System.out.println(test); //This is secret is printed
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.但是,如果我运行它,我可能会得到'2Vhht/L80UlQ184S3rlAWw =='的值作为我的秘密,下次它是'MeC4zCf9S5wUUKAu8rvpCQ ==',所以可能关键是正在改变.我假设我正在对这个问题运用一些反直觉的逻辑,如果有人能够解释a)我做错了什么,或者b)允许我存储加密的密码信息的解决方案,我会很感激并可通过提供的信息检索.
我的方法如下:
private static final String salt = "SaltySalt";
private static byte [] ivBytes = null;
private static byte[] getSaltBytes() throws Exception {
return salt.getBytes("UTF-8");
}
private static char[] getMasterPassword() {
return "SuperSecretPassword".toCharArray();
}
private static byte[] getIvBytes() throws Exception {
if (ivBytes == null) {
//I don't have the parameters, so I'll generate a dummy encryption to create them
encrpytString("test"); …Run Code Online (Sandbox Code Playgroud)