Java AES加解密过程及Initialization Vector的用法

Lee*_*nen 6 java encryption cryptography aes

我想学习 AES 加密的基础知识,所以我开始制作一个非常简单的 Java 程序。该程序将一个文本文件加载到 a 中String并要求用户提供密钥。然后程序使用 AES 加密文本,用加密的文本创建一个新的文本文件。该程序将初始化向量 (IV) 打印给用户。

该程序还具有解密功能。用户指定加密文本文件以及初始化向量和密钥,以将其解密回新文本文件中的原始文本。

但是,我认为我做错了什么。用户需要同时拥有密钥和 IV 来解密文件是 AES 加密中的正常程序吗?我浏览过互联网,几乎在每个示例中,加密数据都可以由用户仅指定密钥来解密,但在我的情况下,用户需要同时拥有密钥和 IV。该程序运行良好,但我认为它效率不高。

那么我应该使用一个常数,已知的 IV 用于所有加密和解密还是什么?还有一些教程使用“盐”,它是什么,我应该使用它吗?

这是我的加密和解密方法:

public String encrypt(String stringToEncrypt, String userKey)
        throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

    // User gives string key which is formatted to 16 byte and to a secret
    // key
    byte[] key = userKey.getBytes();
    MessageDigest sha = MessageDigest.getInstance("SHA-1");
    key = sha.digest(key);
    key = Arrays.copyOf(key, 16);
    SecretKeySpec secretKey = new SecretKeySpec(key, "AES");

    // Cipher initialization
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);

    // Encryption and encoding
    String encryptedData = new BASE64Encoder().encode(cipher
            .doFinal(stringToEncrypt.getBytes()));

    // IV is printed to user
    System.out.println("\nENCRYPTION IV: \n"
            + new BASE64Encoder().encode(cipher.getIV()) + "\n");

    // Function returns encrypted string which can be writed to text file
    return encryptedData;

}

public String decrypt(String stringToDecrypt, String userKey, String userIv)
        throws NoSuchAlgorithmException, IOException,
        NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException,
        BadPaddingException {

    // User gives the same string key which was used for encryption
    byte[] key = userKey.getBytes();
    MessageDigest sha = MessageDigest.getInstance("SHA-1");
    key = sha.digest(key);
    key = Arrays.copyOf(key, 16);
    SecretKeySpec secretKey = new SecretKeySpec(key, "AES");

    // Decode string iv to byte
    byte[] iv = new BASE64Decoder().decodeBuffer(userIv);

    // Cipher initialization
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
    cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));

    // Decryption and decoding
    String decryptedData = new String(cipher.doFinal(new BASE64Decoder()
            .decodeBuffer(stringToDecrypt)));

    // Function returns decrypted string which can be writed to text file
    return decryptedData;

}
Run Code Online (Sandbox Code Playgroud)

更新

我现在更新了我的代码以使用带有盐等的“PBKDF2WithHmacSHA256”算法。我还将初始化向量 (IV) 字节数组与作为前缀的密文字节数组组合在一起,以便我可以将它们拆分为解密方法并在那里获取 IV(那就是工作正常)。

但是现在密钥存在问题,因为我也在解密方法中生成新的加密密钥,这当然是加密数据的错误密钥。我希望能够关闭程序,因此我无法将密钥存储为类变量。很难解释这个问题,但我希望你理解这个问题......

public static byte[] getEncryptedPassword(String password, byte[] salt,
        int iterations, int derivedKeyLength)
        throws NoSuchAlgorithmException, InvalidKeySpecException {

    KeySpec mKeySpec = new PBEKeySpec(password.toCharArray(), salt,
            iterations, derivedKeyLength);

    SecretKeyFactory mSecretKeyFactory = SecretKeyFactory
            .getInstance("PBKDF2WithHmacSHA256");

    return mSecretKeyFactory.generateSecret(mKeySpec).getEncoded();

}

public String encrypt(String dataToEncrypt, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {

    byte[] mEncryptedPassword = getEncryptedPassword(key, generateSalt(),
            16384, 128);

    SecretKeySpec mSecretKeySpec = new SecretKeySpec(mEncryptedPassword, "AES");


    Cipher mCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

    mCipher.init(Cipher.ENCRYPT_MODE, mSecretKeySpec);

    byte[] ivBytes = mCipher.getIV();
    byte[] encryptedTextBytes = mCipher.doFinal(dataToEncrypt.getBytes());

    byte[] combined = new byte[ivBytes.length+encryptedTextBytes.length];       
    System.arraycopy(ivBytes, 0, combined, 0, ivBytes.length);
    System.arraycopy(encryptedTextBytes, 0, combined, ivBytes.length, encryptedTextBytes.length);

    return Base64.getEncoder().encodeToString(combined);

}

public String decrypt(String dataToDecrypt, String key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {


    byte[] encryptedCombinedBytes = Base64.getDecoder().decode(dataToDecrypt);
    byte[] mEncryptedPassword = getEncryptedPassword(key, generateSalt(),
            16384, 128);

    byte[] ivbytes = Arrays.copyOfRange(encryptedCombinedBytes,0,16);

    SecretKeySpec mSecretKeySpec = new SecretKeySpec(mEncryptedPassword, "AES");

    Cipher mCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

    mCipher.init(Cipher.DECRYPT_MODE, mSecretKeySpec, new IvParameterSpec(ivbytes));    

    byte[] encryptedTextBytes = Arrays.copyOfRange(encryptedCombinedBytes, 16, encryptedCombinedBytes.length);

    System.out.println(encryptedTextBytes.length);
    byte[] decryptedTextBytes = mCipher.doFinal(encryptedTextBytes);



    return Base64.getEncoder().encodeToString(decryptedTextBytes);

}

public byte[] generateSalt() {
    SecureRandom random = new SecureRandom();
    byte saltBytes[] = new byte[16];
    random.nextBytes(saltBytes);
    return saltBytes;
}}
Run Code Online (Sandbox Code Playgroud)

我希望有人知道如何使这更好。谢谢!

Mar*_*dil 6

只需将 IV 保存在文件中加密数据之前即可。

你不应该多次使用同一个 IV(这是可以的,如果你每次都滚动一个新的 IV,并且碰巧你滚动了两次相同的 IV,所以你不必存储和检查)。多次使用相同的 IV 会带来巨大的安全风险,因为对相同的内容加密两次会表明它实际上是相同的内容。

将 IV 与加密数据一起存储是一种常见且安全的过程,因为它的作用是将“随机性”引入加密方案,并且它不应该是秘密的,而只是安全地(在某些方案中是随机的)生成。

  • @LeeviLehtonen IV 始终大小相同([块大小](http://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html#getBlockSize()))。在以 Base 64 进行编码之前*将 IV 添加到密文前面会更好、更高效... (4认同)