【问题标题】:Review of this AES-128 CBC example with password-based encryption使用基于密码的加密查看此 AES-128 CBC 示例
【发布时间】:2017-10-08 21:28:44
【问题描述】:

我需要一些帮助来验证以下代码 sn-p 以使用 CBC、PKCS5Padding 和 IV 进行 Java AES 加密。

我测试了代码并且能够加密和解密。我有几个问题,如下所述。

  1. 作为一个好的约定,密码应该存储在哪里?
  2. 向密文附加/检索盐和 IV 字节的方式好吗?
  3. 非常感谢任何其他 cmets,谢谢!
public class Encryption {

    private static int iterations = 65536;
    private static int keySize = 128;
    private static char[] password = "password".toCharArray();
    private static String algorithm= "PBKDF2WithHmacSHA1";


    private static final String SEPARATOR = "~";


     public static void main(String []args) throws Exception {

         String filePath = "test.xml";

         String fileContent = new String(Files.readAllBytes(Paths.get(filePath)));

         String encrMesg = encrypt(fileContent);

         System.out.println("Encrypted: " + encrypt(encrMesg)); 

         System.out.println("Decrypted: " + decrypt(encrMesg)); 
     }


    public static String encrypt(String plaintext) throws Exception {


        byte[] saltBytes = getSalt().getBytes();

        SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
        PBEKeySpec spec = new PBEKeySpec(password, saltBytes, iterations, keySize);
        SecretKey secretKey = skf.generateSecret(spec);
        SecretKeySpec secretSpec = new SecretKeySpec(secretKey.getEncoded(), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretSpec);
        AlgorithmParameters params = cipher.getParameters();

        byte[] ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();
        byte[] cipherText = cipher.doFinal(String.valueOf(plaintext).getBytes("UTF-8"));

        return DatatypeConverter.printBase64Binary(ivBytes)+SEPARATOR+DatatypeConverter.printBase64Binary(saltBytes)
        +SEPARATOR+DatatypeConverter.printBase64Binary(cipherText);
    }

    public static String decrypt(String encryptedText) throws Exception {

        System.out.println(encryptedText);

        String[] encryptedArr = encryptedText.split(SEPARATOR);

        byte[] ivBytes = DatatypeConverter.parseBase64Binary(new String(encryptedArr[0]));

        byte[] salt = DatatypeConverter.parseBase64Binary(new String(encryptedArr[1]));

        byte[] encryptedTextBytes = DatatypeConverter.parseBase64Binary(new String(encryptedArr[2]));

        SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
        PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, keySize);
        SecretKey secretKey = skf.generateSecret(spec);
        SecretKeySpec secretSpec = new SecretKeySpec(secretKey.getEncoded(), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretSpec, new IvParameterSpec(ivBytes));

        byte[] decryptedTextBytes = null;

        try {
            decryptedTextBytes = cipher.doFinal(encryptedTextBytes);
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }

        return new String(decryptedTextBytes);

    }

    public static String getSalt() throws Exception {

        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        byte[] salt = new byte[20];
        sr.nextBytes(salt);
        return new String(salt);
    }

}

【问题讨论】:

  • 存储密码的最佳位置是人脑。这就是首先使用密码的全部意义所在。如果将其存储在机器上,最好以安全的方式存储二进制密钥。
  • 你为什么不使用做得很好的库之一呢? RNCryptor 和 this one 浮现在脑海中。

标签: java cryptography aes salt initialization-vector


【解决方案1】:

查询

作为一个好的约定,密码应该存储在哪里?

对称密钥最好存放在保险库中。否则,他们应该继续使用密钥库,但是您会遇到保护密钥库密码的问题。

是将 Salt 和 IV 字节附加/检索到 Cipher 的方式 文字好吗?

盐应该通过以下方式生成:

SecureRandom random = SecureRandom.getInstanceStrong();

否则,您将使用较弱的熵池(即 linux 中的 /dev/urandom)来生成安全数字,这会导致更容易被破解的弱密钥。

非常感谢任何其他 cmets,谢谢!

在处理字符串转换时,您应该始终使用相同的编码,即.getBytes("UTF-8") 以避免出现问题。例如,在转换盐时不要使用它。

【讨论】:

  • 私钥同样难以保护。与对称加密相比,非对称加密非常慢,并且数据长度被限制为小于密钥大小。通常数据使用AES等对称方法加密,密钥使用非对称加密进行加密。请注意,TLS 使用对称加密对数据进行加密。
  • 几乎所有数据都使用对称加密进行加密。给定 RSA 2048 位密钥,数据限制小于 256 字节,对于 256 位 EC 密钥,数据限制小于 32 字节。使用 AES,基本上没有数据长度限制。对称加密是加密的工作马。在 iOS 的情况下,所有文件系统数据都使用对称加密进行加密:AES。
  • 注意:最后一段 WRT 对称和非对称密钥已从答案中删除,相关评论也是如此。
  • 是的,我正在努力使这与 OP 尽可能相关,并尝试在此过程中学习。感谢您的评论,使答案变得更好、更相关。
猜你喜欢
  • 2013-08-11
  • 1970-01-01
  • 2019-08-17
  • 2020-04-15
  • 1970-01-01
  • 2021-06-25
  • 1970-01-01
  • 2020-11-29
  • 2017-03-27
相关资源
最近更新 更多