gpt4 book ai didi

java - 在 Java 中使用 CipherOutputStream

转载 作者:行者123 更新时间:2023-11-30 04:58:44 30 4
gpt4 key购买 nike

我正在尝试使用 AES 密码来加密一些字节,但它返回一个无提示错误,这意味着我输入如下内容:

byte[] raw = new String("Test","UTF8").getBytes("UTF8");

并且它不会返回任何内容。我认为问题出在 ByteArrayInput/OutputStreams 但我不知道如何以其他方式做到这一点..

这是有问题的代码。

public byte[] encrypt(byte[] in) {
byte[] encrypted = null;
try {
aesCipher.getInstance("AES/CBC/PKCS5Padding");
aesCipher.init(Cipher.ENCRYPT_MODE, aeskeySpec);
ByteArrayInputStream bais = new ByteArrayInputStream(in);
ByteArrayOutputStream baos = new ByteArrayOutputStream(bais.available());
CipherOutputStream os = new CipherOutputStream(baos, aesCipher);
copy(bais, os);
os.flush();
byte[] raw = baos.toByteArray();
os.close();
encrypted = Base64.encodeBase64(raw);

} catch (FileNotFoundException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
}
return encrypted;
}

这是同一类中的另一个有效的函数:

public void encrypt(File in, File out) {


try {
aesCipher.getInstance("AES/CBC/PKCS5Padding");
aesCipher.init(Cipher.ENCRYPT_MODE, aeskeySpec);
FileInputStream is;
is = new FileInputStream(in);
CipherOutputStream os = new CipherOutputStream(new FileOutputStream(out), aesCipher);
copy(is, os);
os.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
}

}

private void copy(InputStream is, OutputStream os) throws IOException {
int i;
byte[] b = new byte[2048];
while ((i = is.read(b)) != -1) {
os.write(b, 0, i);
}
}

最佳答案

首先引起我注意的是这句话:

aesCipher.getInstance("AES/CBC/PKCS5Padding");

假设aesCipherCipher类型的变量,您在此处调用静态Cipher.getInstance并丢弃结果(而不是分配它到任何变量)。 IE。此行根本没有任何效果,aesCipher 与此行之前相同。

如果之前是null,那么它仍然是null,并且下一行(调用非静态方法)会给你一个NullPointerException。如果您的代码默默地吞噬未知异常(这可能超出了显示的代码),那么这是一个普遍问题。

除此之外,我认为 CipherOutputStream 上的 flush 并不会真正刷新整个缓冲区,而只是刷新可以写入的 block ,而无需添加任何填充。此处使用 close() 而不是 flush()(这似乎也适用于第二个示例)。

<小时/>

一般说明:A small self-contained complete compilable example将使我能够尝试并给你一个明确的答案,而不是仅仅猜测。例如,“不返回任何内容”并不是对方法行为的良好描述 - 该方法是否返回 null、空数组、抛出异常、永远阻塞?

关于java - 在 Java 中使用 CipherOutputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7656293/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com