1

我正在尝试使用 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);
    }
}
4

1 回答 1

2

首先引起我注意的是这一行:

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

假设aesCipher是一个类型的变量Cipher,你在这里调用 staticCipher.getInstance并丢弃结果(不将其分配给任何变量)。即这条线完全没有效果,aesCipher和之前这条线一样。

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

除此之外,我认为flushCipherOutputStream 上的 并没有真正刷新整个缓冲区,而只是在不添加任何填充的情况下可以写入尽可能多的块。在此处使用close()而不是flush()(这在第二个示例中似乎也有效)。


一般性评论:一个小的自包含完整可编译示例将使我能够尝试它并给你一个明确的答案,而不仅仅是猜测。例如,“不返回任何东西”不能很好地描述您的方法的行为 - 方法是否返回null、空数组、抛出异常、永远阻塞?

于 2011-10-06T10:17:32.473 回答