需要确认其256位文件加密/解密吗?

问题描述 投票:0回答:1

我们用Java编写了256位文件加密/解密代码。它工作正常,并且使用相同的密钥+盐组合正确加密和解密文件。

import java.io.IOException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.File;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.spec.KeySpec;
import java.util.Base64;
import javax.crypto.KeyGenerator;


public class CryptoUtils
{
    private static final String ALGORITHM = "AES";
    private static final String TRANSFORMATION = "AES";
    private static final String SECRET_KEY = "ncrm7tDy0Y137YNT4+6/0szt4weszTlqj/iPLySCTKY=";
    private static final String SALT = "ssshhhhhhhhhhh!!!!";
    
    private static void doCrypto256(final int opmode, final File file, final File file2) {       
        try {
            byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            IvParameterSpec ivspec = new IvParameterSpec(iv);
            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");            
            KeySpec spec = new PBEKeySpec (SECRET_KEY.toCharArray(), SALT.getBytes(), 100, 256);
            SecretKey tmp = factory.generateSecret(spec);            
            SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");           
            Cipher instance = Cipher.getInstance("AES/CBC/PKCS5Padding");
            instance.init(opmode, secret,ivspec);             
            final FileInputStream fileInputStream = new FileInputStream(file);
            final byte[] array = new byte[(int)file.length()];
            fileInputStream.read(array);
            final byte[] doFinal = instance.doFinal(array);
            final FileOutputStream fileOutputStream = new FileOutputStream(file2);
            fileOutputStream.write(doFinal);
            fileInputStream.close();
            fileOutputStream.close();
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }  
    
    public static void main(String[] args) {     
       File inputFile = new File("D:/JaveEncryptDecrypt/sample.pdf");            
        try {         
          doCrypto256(1, inputFile, inputFile);  //encryption on 256 
         doCrypto256(2, inputFile, inputFile);  //Decryption on 256 
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
    }
}

我已经生成了一个 AES-256 密钥,我已在类中定义了该密钥。我想知道是否是256位加密/解密?如果我需要更改任何内容,请告诉我。

谢谢

java encryption encryption-symmetric encryption-asymmetric
1个回答
-1
投票

我想知道它是否是256位加密/解密?

是的,您提供的代码是使用256位AES加密和解密的。

如果我需要更改任何内容,请告诉我。我正在等待您的重播。

  1. 我可以看到

    IV (Initialization Vector)
    被设置为全零的固定值。这不被认为是安全的做法。我建议您为每个加密操作生成一个随机 IV,以确保更好的安全性。

  2. 我还可以看到密钥和盐是硬编码的。我建议安全地存储并在需要时检索它们,而不是直接将它们嵌入到代码中。

  3. 我认为您的代码会用加密或解密的数据覆盖输入文件。我认为最好为加密和解密版本创建单独的输出文件,以避免数据丢失。

© www.soinside.com 2019 - 2024. All rights reserved.