JavaFX 使用 AES-256 加密和解密 Zip。 IVParameterSpec 错误

JavaFX encrypt and uncrypt a Zip with AES-256. Error with IVParameterSpec

我正在用 JavaFX 创建一个应用程序,它首先压缩选定的文件夹,然后对其进行加密。 压缩文件夹并将其加密的过程 好的,问题是当我尝试解密它时:出现带有 IVParameterSpec 的警告。

我在想问题是我需要保存 IVS 的某个地方。也许在加密文件的开头? 我该怎么做?

我正在按照本教程使用 SHA-256 加密的 AES 进行以下修改:http://karanbalkar.com/2014/02/tutorial-76-implement-aes-256-encryptiondecryption-using-java/

加密与解密:

private static String password;
private static String salt;
private static int pswdIterations = 65536  ;
private static int keySize = 256;
private byte[] ivBytes;
public void encryptToFile(byte[] bytes, File out) throws Exception {  
    byte[] saltBytes = salt.getBytes("UTF-8");

    System.out.println("Salt bfre:" +salt);

    // Derive the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(
            password.toCharArray(),
            saltBytes,
            pswdIterations,
            keySize
            );

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

    //encrypt the message
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    AlgorithmParameters params = cipher.getParameters();
    ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();

    CipherOutputStream os = new CipherOutputStream(new FileOutputStream(out, true), cipher);
    os.write(bytes);
    os.close();

}

public byte[] decryptToFile(File in) throws Exception {  
    byte[] saltBytes = salt.getBytes("UTF-8");

    // Derive the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(
            password.toCharArray(),
            saltBytes,
            pswdIterations,
            keySize
            );

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

    // Decrypt the message
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes));

    FileInputStream is = new FileInputStream(in);
    byte[] encBytes = new byte[(int) in.length()];
    is.read(encBytes);
is.close();    

    byte[] decryptedBytes = null;
    try {
        decryptedBytes = cipher.doFinal(encBytes);
    } catch (IllegalBlockSizeException | BadPaddingException e) {
        e.printStackTrace();
    }

    return decryptedBytes;

}

现在主要是:

@FXML private void btDoIt (ActionEvent event){
    if (tests()){
        HashClass hash = new HashClass(tfPassword.getText());

        SimetricWithSha256 aes256 = new SimetricWithSha256();
        aes256.setPassword(tfPassword.getText());
        aes256.setSalt(hash.sumCalcToString("SHA-256"));

        if (rbCrypt.isSelected()){

            AppZip appZip = new AppZip(tfPath.getText(),destCrypt);
            ByteArrayOutputStream baos = appZip.zip();
            try {
                aes256.encryptToFile(baos.toByteArray(), new File (tfPath.getText()+destCrypt) );
                baos.close();

            } catch (Exception ex) {
                Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
            }

        }else if (rbUncrypt.isSelected() ){
            try {                    
                byte[] decrypted = aes256.decryptToFile(new File (tfPath.getText() ));
                FileOutputStream fos = new FileOutputStream(tfPath.getText()+destDeCrypt);
                fos.write(decrypted);
                fos.close();


            } catch (Exception ex) {
                Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
            } 

        }
    }
}

如果我在解密后立即执行解密过程,它就可以正常工作。问题是当我在第二个选项中这样做时: else if (rbUncrypt.isSelected() )

我在解密函数行中收到此错误,其中“cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes));” :

java.lang.NullPointerException at javax.crypto.spec.IvParameterSpec.(IvParameterSpec.java:53) at encriptarusb.SimetricWithSha256.decryptToFile(SimetricWithSha256.java:137)

也许是因为我必须保存 ivBytes?

谢谢!

基本上问题是我没有保存用于使加密更随机的 IVBytes。

因此,如果我想解密文件,我需要相同的 IV 和相同的盐。 在此示例中,我使用了如何对密码的哈希 (SHA-256) 进行加盐(但最好使用随机生成的加盐)。

对于 IVBytes,我将其放入我加密的同一个文件中,因为它们不一定是秘密的。在解密过程中,我将使用文件的第一个字节(IV 字节)进行解密:

public void encryptOfFile(byte[] bytes, File out) throws Exception {  

    byte[] saltBytes = salt.getBytes("UTF-8");

    System.out.println("Salt bfre:" +salt);

    // Derive the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(
            password.toCharArray(),
            saltBytes,
            pswdIterations,
            keySize
            );

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

    //encrypt the message
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    AlgorithmParameters params = cipher.getParameters();
    ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();

    //First copy the IVBytes at the beginning  of the file
    FileOutputStream os = new FileOutputStream(out, true);
    os.write(ivBytes);

    CipherOutputStream cos = new CipherOutputStream(os, cipher);
    cos.write(bytes);

    cos.close();
    os.close();

}

public byte[] decryptToFile(File in) throws Exception {  
    byte[] saltBytes = salt.getBytes("UTF-8");

    // Derive the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(
            password.toCharArray(),
            saltBytes,
            pswdIterations,
            keySize
            );

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");


    //Get IVBytes of the first 16 bytes of the file
    FileInputStream is = new FileInputStream(in);
    byte [] ivBytesRecovered = new byte [16];
    if (is.read(ivBytesRecovered) != ivBytesRecovered.length) {
        //is.close();
        throw new IllegalStateException("Too short file");
    }

    // Decrypt the message
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytesRecovered));        

    byte[] encBytes = new byte[(int) in.length()-16];
    is.read(encBytes);

    byte[] decryptedBytes = null;
    try {
        decryptedBytes = cipher.doFinal(encBytes);
    } catch (IllegalBlockSizeException | BadPaddingException e) {
        e.printStackTrace();
    }

    is.close();

    return decryptedBytes;

}

此外,如果要生成随机盐:

public String generateSalt() {
    SecureRandom random = new SecureRandom();
    byte bytes[] = new byte[20];
    random.nextBytes(bytes);
    String s = new String(bytes);
    return s;
}

这里是我的参考资料(我用过的三个例子):

http://javapapers.com/java/java-file-encryption-decryption-using-aes-password-based-encryption-pbe/
http://karanbalkar.com/2014/02/tutorial-76-implement-aes-256-encryptiondecryption-using-java/
http://cryptofreek.org/2012/12/10/encrypting-and-decrypting-java-part-2/