gpt4 book ai didi

java - 如何使用java将字节数组 append 到文件而不覆盖它

转载 作者:行者123 更新时间:2023-12-01 17:59:12 27 4
gpt4 key购买 nike

我尝试进行 AES 加密,并且正在生成盐。但是我遇到了一些问题。下面的代码工作正常,但每当我加密第二个等文件时,文件中的盐就会被覆盖。关于如何将 salt 字节 [] append 到 salt fil 中而不覆盖它有什么建议吗?

伙计们..我更新了我的代码..感谢那部分,虽然它解决了覆盖问题,但它没有进入下一行。

我的文件上的输出:“V”à·Ò²Ö4ôϒT‰mÊî0‘Z^'û•šÙK· = 这是两种盐的组合。

知道如何将其 append 到下一行吗?

我尝试 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;
}

最佳答案

您需要使用带有 2 个参数的 FileOutputStream 的构造函数。第二个参数是一个 boolean 标志,指示是否要追加 (true) 到文件或从头开始 (false )。

将代码更改为:

 FileOutputStream saltOutFile = new FileOutputStream("C:\\KryptZIP\\salt.enc", true);

关于java - 如何使用java将字节数组 append 到文件而不覆盖它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42291259/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com