对于我的 java 类,我的项目的一部分涉及从用户获取加密密钥长度并将其四舍五入到最接近的 1024 倍数。长度作为 long 传递。在我的方法中,我得到了 long 并且得到了要写入的文件路径。在示例中,我看到了这种实现:
try (FileOutputStream out = new FileOutputStream(file)) {
byte[] bytes = new byte[1024];
new SecureRandom().nextBytes(bytes);
out.write(bytes);
}
但是我在哪里以及如何实现我的长变量?我不能在 byte[long] 中输入 long。我知道我需要使用 SecureRandom().nextBytes() 根据我的教授。任何帮助将不胜感激,因为这部分一直让我发疯。
这是我到目前为止所拥有的,但我不禁认为这不是我的教授想要的方式......
public void oneKeyGenerator(String keyPath, long keyLength) {
final long CONST_MULTIPLE = 1024;
try {
FileOutputStream out = new FileOutputStream(keyPath);
byte[] bytes = new byte[1024];
for(long x = 0; x < keyLength/CONST_MULTIPLE; x++) {
new SecureRandom().nextBytes(bytes);
out.write(bytes);
}
} catch(IOException e){
gui.fileException(e.getMessage());
}
}