简单地说,我有一个 CipherInputStream,我想将其转换为字节数组。其他帖子没有帮助。如何实现这一目标?
FileInputStream fis = new FileInputStream("dataPath/data");
SecretKeySpec sks = new SecretKeySpec("password".getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cis = new CipherInputStream(fis, cipher);
那么如何从 cis
检索字节数组?
最佳答案
CipherInputStream
是标准 InputStream
的实现,因此您可以使用 ByteArrayOutputStream
将其读入字节数组,例如:
CipherInputStream cis = ...
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[4096];
while ((len = cis.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, len);
}
baos.flush();
byte[] cipherByteArray = baos.toByteArray(); // get the byte array
关于android - 将密码输入流转换为字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36679390/