【发布时间】:2017-07-06 14:03:49
【问题描述】:
我尝试进行 AES 加密,并且正在生成 salt 。但是我遇到了一些问题。下面的代码工作正常,但每当我加密第二个等文件时,文件中的盐就会被覆盖。关于如何将盐字节 [] 附加到盐文件而不覆盖它的任何建议?
伙计们..我更新了我的代码..感谢那部分虽然它修复了覆盖问题,但它没有进入下一行。
我的文件中的输出:∼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;
}
【问题讨论】:
-
我没有发现任何问题。每次我运行您的代码时,它都会向盐文件附加 8 个字节的数据。
-
假设我使用这个: FileOutputStream saltOutFile = new FileOutputStream("C:\\KryptZIP\\salt.enc",true);这会将下一个盐添加到盐文件中吗?因为我想知道它会出现在下一行
-
仅在基于字符的场景中谈论“线”才有意义(例如在使用
Reader时)。您正在使用一个完全基于字节的OutputStream。在这样的流中没有像“线”这样的概念。此外,您为什么不直接尝试一下? -
您正在将 bytes 写入文件。你不能指望把它读成 text.
标签: java arrays file encryption append