【发布时间】: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