将密码输入流转换为字节数组?
Convert Cipher Input Stream to Byte array?
简单地说,我有一个 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
简单地说,我有一个 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