我试图使AES加密,我正在生成盐。但是我遇到了一些问题。下面的代码工作正常,但每当我加密第二个等文件,文件中的盐被覆盖。有关如何将salt byte []添加到salt fil中而不覆盖它的任何建议?如何将字节数组附加到文件而不覆盖它使用java
伙计们..我更新了我的代码..感谢那部分,虽然它修复了覆盖问题,但它并没有使它到下一行。
输出我的文件:?〜V“A·Ò²Ö4ô|ŒT‰mÊî0'Z^'U•苏克·=这是二盐结合
任何想法如何使它在追加下一行
我想saltoutfile.write( “/ N”),但不工作
public byte[] generateSalt() throws IOException{
//generate Salt value
// password, iv and salt should be transferred to the other end
// in a secure manner
// salt is used for encoding
// writing it to a file
// salt should be transferred to the recipient securely
// for decryption
byte[] salt = new byte[8];
SecureRandom secureRandom = new SecureRandom();
secureRandom.nextBytes(salt);
FileOutputStream saltOutFile = new FileOutputStream("C:\\KryptZIP\\salt.enc" , true);
saltOutFile.write(salt);
saltOutFile.close();
return salt;
}
在评论中提及
:这是我读盐值
public static byte[] readSalt() throws IOException{
// reading the salt
// user should have secure mechanism to transfer the
// salt, iv and password to the recipient
FileInputStream saltFis = new FileInputStream("C:\\KryptZIP\\salt.enc");
byte[] salt = new byte[8];
saltFis.read(salt);
saltFis.close();
return salt;
}
出了什么问题['新的FileOutputStream中(文件名,真实)'](https://docs.oracle.com/javase/8/docs/api/java/ IO/FileOutputStream.html#FileOutputStream中,java.lang.String中-boolean-)? – Seelenvirtuose
我没有看到任何问题。每次运行代码时,它会将8个字节的数据附加到salt文件中。 – anacron
假设我使用这个: FileOutputStream saltOutFile = new FileOutputStream(“C:\\ KryptZIP \\ salt.enc”,true); 这会将下一个盐添加到盐文件?因为我不知道它是否会显示为下一行 –