【问题标题】:JavaFX encrypt and uncrypt a Zip with AES-256. Error with IVParameterSpecJavaFX 使用 AES-256 加密和解密 Zip。 IVParameterSpec 出错
【发布时间】:2015-04-11 23:08:38
【问题描述】:

我正在 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));”的 uncrypt 函数行中收到此错误:

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

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

谢谢!

【问题讨论】:

  • 我认为问题在于我没有保存 IV 和 Salt 字节。一个问题:将其保存在加密文件的第一个字节中是否安全?存储它的典型/安全形式是什么?
  • 您是正确的,您可以安全地将 IV 和盐添加到密文中。 IV 是为了使密文在语义上安全(与使用相同密钥时的类似明文不同),盐是为了防止彩虹表。
  • 所以如果我想在加密文件中添加 IV。我怎么知道尺寸?如果我做 ivBytes.length 结果是 16。这是我必须在加密文件开始时恢复的长度吗?
  • 是的,对于 AES,IV 总是 16 字节长。盐可能不同,因此您只需选择一种尺寸并保持一致。
  • 如果我使用密码生成 de salt 则不需要保存它,因为我总是生成相同的密码。是真的吗?

标签: java javafx cryptography


【解决方案1】:

基本上,问题在于我没有保存用于使加密更加随机的 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/

【讨论】:

  • 你的盐是可变长度的。 random.nextBytes(bytes); 用任何可能的字节值填充bytes,但new String(bytes); 仅将这些字节转换为可打印的字符,因此您可能会丢失一些字符。由于您将盐生成为字节,因此您应该在任何地方继续使用byte[]
猜你喜欢
  • 1970-01-01
  • 2022-12-12
  • 1970-01-01
  • 1970-01-01
  • 2022-10-16
  • 1970-01-01
  • 1970-01-01
  • 2014-08-05
  • 2021-09-02
相关资源
最近更新 更多