Pet*_*y B 125 java security encryption configuration cryptography
我有一个程序从配置文件中读取服务器信息,并希望加密该配置中的密码,该密码可由我的程序读取并解密.
质量要求:
关于我将如何做到这一点的任何建议?我正在考虑编写自己的算法,但我觉得它会非常不安全.
Joh*_*all 164
一种简单的方法是在Java中使用基于密码的加密.这允许您使用密码加密和解密文本.
这基本上意味着初始化javax.crypto.Cipher与算法"AES/CBC/PKCS5Padding",并从获得的关键javax.crypto.SecretKeyFactory与"PBKDF2WithHmacSHA512"算法.
这是一个代码示例(更新以替换不太安全的基于MD5的变体):
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.AlgorithmParameters;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
public class ProtectedConfigFile {
public static void main(String[] args) throws Exception {
String password = System.getProperty("password");
if (password == null) {
throw new IllegalArgumentException("Run with -Dpassword=<password>");
}
// The salt (probably) can be stored along with the encrypted data
byte[] salt = new String("12345678").getBytes();
// Decreasing this speeds down startup time and can be useful during testing, but it also makes it easier for brute force attackers
int iterationCount = 40000;
// Other values give me java.security.InvalidKeyException: Illegal key size or default parameters
int keyLength = 128;
SecretKeySpec key = createSecretKey(password.toCharArray(),
salt, iterationCount, keyLength);
String originalPassword = "secret";
System.out.println("Original password: " + originalPassword);
String encryptedPassword = encrypt(originalPassword, key);
System.out.println("Encrypted password: " + encryptedPassword);
String decryptedPassword = decrypt(encryptedPassword, key);
System.out.println("Decrypted password: " + decryptedPassword);
}
private static SecretKeySpec createSecretKey(char[] password, byte[] salt, int iterationCount, int keyLength) throws NoSuchAlgorithmException, InvalidKeySpecException {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
PBEKeySpec keySpec = new PBEKeySpec(password, salt, iterationCount, keyLength);
SecretKey keyTmp = keyFactory.generateSecret(keySpec);
return new SecretKeySpec(keyTmp.getEncoded(), "AES");
}
private static String encrypt(String property, SecretKeySpec key) throws GeneralSecurityException, UnsupportedEncodingException {
Cipher pbeCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
pbeCipher.init(Cipher.ENCRYPT_MODE, key);
AlgorithmParameters parameters = pbeCipher.getParameters();
IvParameterSpec ivParameterSpec = parameters.getParameterSpec(IvParameterSpec.class);
byte[] cryptoText = pbeCipher.doFinal(property.getBytes("UTF-8"));
byte[] iv = ivParameterSpec.getIV();
return base64Encode(iv) + ":" + base64Encode(cryptoText);
}
private static String base64Encode(byte[] bytes) {
return Base64.getEncoder().encodeToString(bytes);
}
private static String decrypt(String string, SecretKeySpec key) throws GeneralSecurityException, IOException {
String iv = string.split(":")[0];
String property = string.split(":")[1];
Cipher pbeCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
pbeCipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(base64Decode(iv)));
return new String(pbeCipher.doFinal(base64Decode(property)), "UTF-8");
}
private static byte[] base64Decode(String property) throws IOException {
return Base64.getDecoder().decode(property);
}
}
Run Code Online (Sandbox Code Playgroud)
仍然存在一个问题:您应该在哪里存储用于加密密码的密码?您可以将其存储在源文件中并对其进行模糊处理,但再次找到它并不难.或者,您可以在启动Java进程(-DpropertyProtectionPassword=...)时将其作为系统属性提供.
如果您使用KeyStore,同样的问题仍然存在,KeyStore也受密码保护.基本上,您需要在某处拥有一个主密码,而且很难保护.
Jee*_*Bee 20
是的,绝对不要编写自己的算法.Java有很多加密API.
如果您安装的操作系统有密钥库,那么您可以使用它来存储加密密钥,您需要加密和解密配置或其他文件中的敏感数据.
oxb*_*kes 16
我认为最好的方法是确保您的配置文件(包含您的密码)只能由特定用户帐户访问.例如,您可能拥有一个特定appuser于应用程序的用户,只有受信任的人才能拥有密码(以及他们的密码su).
这样,没有烦人的加密开销,你仍然有一个安全的密码.
编辑: 我假设你没有在可信环境之外导出你的应用程序配置(鉴于这个问题,我不确定是否有任何意义)
重要的一点,以及房间里的大象等等,是如果您的应用程序可以获取密码,那么可以访问该框的黑客也可以获取它!
解决此问题的唯一方法是应用程序使用标准输入在控制台上要求“主密码”,然后使用它来解密存储在文件中的密码。当然,这完全使得应用程序在启动时与操作系统一起在无人看管的情况下启动是不可能的。
然而,即使有这种烦恼,如果黑客设法获得 root 访问权限(或者甚至只是作为运行应用程序的用户访问),他可以转储内存并在那里找到密码。
需要确保的是,不要让整个公司都可以访问生产服务器(从而访问密码),并确保不可能破解这个盒子!
| 归档时间: |
|
| 查看次数: |
182325 次 |
| 最近记录: |