[英]how to append a byte array to file without overwriting it using java
我試圖進行AES加密,並且正在生成鹽。 但是我遇到了一些問題。 下面的代碼可以正常工作,但是每當我加密第二個文件等等時,文件中的鹽就會被覆蓋。 關於如何將salt字節[]附加到salt fil而不覆蓋它的任何建議?
Guys..i更新了我的代碼。.感謝這一部分,盡管它解決了覆蓋問題,但並沒有進入下一行。
在我的文件上輸出:〜V“÷ҲÖ4ô¦T‰mÊî0'Z^'û•šÙK·=這是兩個鹽的組合。
任何想法如何使其附加在下一行?
我嘗試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
的構造函數。 第二個參數是一個布爾標志,指示是要添加 ( true
)到文件還是要從頭開始寫入( false
)。
將您的代碼更改為:
FileOutputStream saltOutFile = new FileOutputStream("C:\\KryptZIP\\salt.enc", true);
使用Apache FileUtils 。 使用方法
FileUtils.writeByteArrayToFile(java.io.File file,bytes[] content,boolean append)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.