public void decrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, this.key, zeroIv);
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = is.read(buffer)) >= 0) {
cos.write(buffer, 0, r);
}
cos.close();
out.close();
is.close();
}
这样结果是得到解密后的文件,然后在读取文件,现在我不想去读解密后的文件,直接读流,该怎么做?或者说上面的方法我想要得到一个InputStream或String,然后供其他接口使用,该怎么做?
在论坛上看到
while ((r = is.read(buffer)) >= 0) {
cos.write(buffer, 0, r);
System.out.println(new String(buffer, 0, r));
}
但打印出来的是未解密的字符串,求高人指点。
解密 buffer string CipherOutputStream? Cipher?
------解决方案--------------------
CipherInputStream 不是正满足你的要求吗?
public void decrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, this.key, zeroIv);
InputStream is = new FileInputStream(file);
CipherInputStream cis = new CipherInputStream(in, cipher);
OutputStream os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) >= 0) {
os.write(buffer, 0, r);
}
os.close();
cis.close();
}