Java AES加密和解密与静态秘密

Aar*_*ron 6 java encryption aes password-encryption

我有一个应用程序需要在配置文件中存储一些秘密密码,如数据库和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");
    }
    return ivBytes;
}

public static String encrpytString (String input) throws Exception {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(getMasterPassword(), getSaltBytes(), 65536,256);
    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    ivBytes = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
    byte[] encryptedTextBytes = cipher.doFinal(input.getBytes("UTF-8"));
    return DatatypeConverter.printBase64Binary(encryptedTextBytes);        
}

public static String decrpytString (String input) throws Exception {
    byte[] encryptedTextBytes = DatatypeConverter.parseBase64Binary(input);
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(getMasterPassword(), getSaltBytes(), 65536, 256);
    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(getIvBytes()));
    byte[] decryptedTextBytes = cipher.doFinal(encryptedTextBytes);
    return new String(decryptedTextBytes);
}
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助!

Aar*_*ron 6

好的,看起来我已经找到了我的问题的答案。我从这篇 Stackoverflow 帖子中获取了我的信息。据我了解,IV(初始化向量)用于将熵添加到加密过程中。每次创建新密码时,Java 都会创建一个略有不同的 IV。因此有两种解决方案:

  1. 使用固定的 IV,或
  2. 将 IV 与加密数据一起存储。

根据我的阅读,选项 1 不是很好的做法;所以选项2是。我知道应该可以简单地将 IV 附加到加密字符串(因为仍然需要秘密),因此可以在需要解密时重建 IV。

这是几乎完整的解决方案。我仍然在解密时遇到一些填充错误(请参阅我的评论)。我现在没有时间花在它上面,所以作为临时措施,我立即尝试解密一个加密的字符串并继续尝试(迭代)直到它起作用。它似乎有大约 50% 的命中率 + 我加密的频率不够高,以至于它成为一个性能问题。如果有人可以提出修复建议(只是为了完整起见),那就太好了。

private static final String salt = "SaltySalt";

private static final int IV_LENGTH = 16;

private static byte[] getSaltBytes() throws Exception {
    return salt.getBytes("UTF-8");
}

private static char[] getMasterPassword() {
    return "SuperSecretPassword".toCharArray();
}

public static String encrpytString (String input) throws Exception {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(getMasterPassword(), getSaltBytes(), 65536,256);
    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    byte[] ivBytes = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
    byte[] encryptedTextBytes = cipher.doFinal(input.getBytes("UTF-8"));
    byte[] finalByteArray = new byte[ivBytes.length + encryptedTextBytes.length]; 
    System.arraycopy(ivBytes, 0, finalByteArray, 0, ivBytes.length);
    System.arraycopy(encryptedTextBytes, 0, finalByteArray, ivBytes.length, encryptedTextBytes.length);
    return DatatypeConverter.printBase64Binary(finalByteArray);        
}

public static String decrpytString (String input) throws Exception {
    if (input.length() <= IV_LENGTH) {
        throw new Exception("The input string is not long enough to contain the initialisation bytes and data.");
    }
    byte[] byteArray = DatatypeConverter.parseBase64Binary(input);
    byte[] ivBytes = new byte[IV_LENGTH];
    System.arraycopy(byteArray, 0, ivBytes, 0, 16);
    byte[] encryptedTextBytes = new byte[byteArray.length - ivBytes.length];
    System.arraycopy(byteArray, IV_LENGTH, encryptedTextBytes, 0, encryptedTextBytes.length);
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(getMasterPassword(), getSaltBytes(), 65536, 256);
    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes));
    byte[] decryptedTextBytes = cipher.doFinal(encryptedTextBytes);
    return new String(decryptedTextBytes);
}
Run Code Online (Sandbox Code Playgroud)

  • 对于 AES/CBC/PKCS7Padding,大多数人选择在密文前添加 IV。因此,在解密时,从密文中切出前 16 个字节,它们是 IV,初始化密码,然后使用其余的 doFinal。这有一些不错的属性,最值得注意的是不遵循此约定的实现,但只需从 0 IV 开始并丢弃第一个块仍然可以解密文本。 (2认同)