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

Zi *_*ing 0 java arrays encryption file append

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

\n\n

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

\n\n

我的文件上的输出: \xcb\x9cV"\xc3\x83\xc2\xb7\xc3\x92\xc2\xb2\xc3\x964\xc3\xb4\xc2\xa6\xc5\x92T\xe2\x80\xb0m\xc3 \x8a\xc3\xae0\xe2\x80\x98Z^\'\xc3\xbb\xe2\x80\xa2\xc5\xa1\xc3\x99K\xc2\xb7 = 这是两个盐的组合。

\n\n

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

\n\n

我尝试 saltoutfile.write("/n") 但不起作用

\n\n
    public byte[] generateSalt() throws IOException{\n            //generate Salt value\n            // password, iv and salt should be transferred to the other end\n            // in a secure manner\n            // salt is used for encoding\n            // writing it to a file\n            // salt should be transferred to the recipient securely\n            // for decryption\n    byte[] salt = new byte[8];\n    SecureRandom secureRandom = new SecureRandom();\n    secureRandom.nextBytes(salt);\n            FileOutputStream saltOutFile = new FileOutputStream("C:\\\\KryptZIP\\\\salt.enc" , true);\n            saltOutFile.write(salt);\n        saltOutFile.close();\n            return salt;\n\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

正如评论中提到的:这是我读到的盐值

\n\n
    public static byte[] readSalt() throws IOException{\n    // reading the salt\n            // user should have secure mechanism to transfer the\n            // salt, iv and password to the recipient\n            FileInputStream saltFis = new FileInputStream("C:\\\\KryptZIP\\\\salt.enc");\n            byte[] salt = new byte[8];\n            saltFis.read(salt);\n            saltFis.close();\n            return salt;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Erw*_*idt 7

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

将您的代码更改为:

 FileOutputStream saltOutFile = new FileOutputStream("C:\\KryptZIP\\salt.enc", true);
Run Code Online (Sandbox Code Playgroud)