簡體   English   中英

如何使用 SecureRandom 而不是使用硬編碼字節數組進行 Java AES 加密和解密?

[英]How to use SecureRandom instead of using hardcoded bytes array for Java AES Encrytion and Decryption?

在我的代碼中,我對 IV 和密鑰使用硬編碼數組(如下所示)

**private static byte[] IVAes = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };

private static byte[] keyAes = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };

public static String encryptAes(String strPlain) {
        byte[] encrypted = null;
        if (StringUtils.isBlank(strPlain)) {
            return strPlain;
        }
        byte[] toEncrypt = strPlain.getBytes();
        try {
            AlgorithmParameterSpec paramSpec = new IvParameterSpec(IVAes);
            // Generate the key specs.
            SecretKeySpec skeySpec = new SecretKeySpec(keyAes, AES_ALGORITHM);
            // Instantiate the cipher
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, paramSpec);
            encrypted = cipher.doFinal(toEncrypt);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
                | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
            LOGGER.error(e.getMessage(), e);
        }
        return new String(Base64.encodeBase64(encrypted));
    }**

但出於安全考慮,不推薦使用硬編碼數組作為 IV 和 Key。 我可以使用 SecureRandom() 而不是這種類型的硬編碼數組,如下所示 -

**public static String encryptAes(String strPlain) {
        byte[] encrypted = null;
        if (StringUtils.isBlank(strPlain)) {
            return strPlain;
        }
        byte[] toEncrypt = strPlain.getBytes();
        try {
//---------calling generateIV method

            AlgorithmParameterSpec paramSpec = generateIv();
                // Instantiate the cipher
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, paramSpec);
            encrypted = cipher.doFinal(toEncrypt);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
                | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
            LOGGER.error(e.getMessage(), e);
        }
        return new String(Base64.encodeBase64(encrypted));
    }

public static IvParameterSpec generateIv() {
    byte[] IVAes = new byte[16];
    new SecureRandom().nextBytes(IVAes);
    return new IvParameterSpec(IVAes);
}

int n = 128;
public static SecretKey generateKey(int n) throws NoSuchAlgorithmException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(n);
    SecretKey key = keyGenerator.generateKey();
    return key;
}**

我只是想知道通過使用 SecureRandom 和密鑰生成器為 IV 和密鑰創建 16 個字節的數組將給出與我使用硬編碼數組時給出的結果相同的結果,如上所示?

密碼的整個想法是它與隨機無法區分(不考慮密文的長度,這顯然在某種程度上取決於消息的大小)。 如果您有唯一的密鑰和 IV,那么密文應該與隨機密文無法區分,盡管對於 CBC,IV 也應該是隨機的。 如果您想測試它是否“有效”,那么您必須解密密文。 否則只能檢查密文大小是否合適。

但是,如果您詢問代碼的安全性,那么可以肯定的是,使用SecureRandomKeyGenerator似乎沒問題。 您可能希望將 IV 作為密文的前綴,這是傳達隨機 IV 的常用方法。 如果您想知道如何分發密鑰,那么我建議您閱讀對稱密鑰管理; 這取決於用例。


對於我的加密迷們來說,是的,肯定相關的密鑰攻擊是一回事,所以擁有唯一的密鑰和 IV 並不完全是全部,但這些密鑰是隨機的,所以這很好。 答案可能還有其他一些通常可以忽略的理論/實踐問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM