没有密钥生成器的Blowfish文件加密

时间:2017-06-08 08:50:04

标签: java encryption blowfish secret-key

如何在不使用Blowfish算法的KeyGenerator的情况下加密我的文件?

这是我的代码的一部分

Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, key);
FileInputStream fis = new FileInputStream(plain);

FileOutputStream fos = new FileOutputStream(copy);
CipherOutputStream out2 = new CipherOutputStream(fos, cipher);
byte[] buffer = new byte[1024];
while (fis.read(buffer)>=0) {
    out2.write(buffer);
}

1 个答案:

答案 0 :(得分:0)

如果您有钥匙,可以使用SecretKeySpec提供,如下所示:

byte[] key = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
SecretKey keySpec = new SecretKeySpec(key, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);