English 中文(简体)
在 Java使用锡西特OutputStream
原标题:Using CipherOutputStream in Java

I m trying to use an AES cipher to encrypt some bytes but it s returning a silent error, meaning I enter something like:

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/PKCS5Pachlor”);

假设<代码>aesCipher是一种类型的变式:Cipher/code>,你在此指静态的<代码>Cipher.getInstance,并放弃结果(不将其分配给任何变量)。 页: 1

如果是<代码>null,则该代码仍是<>null,下行(称为非静态方法)将给你一个Null PointerException。 如果你的法典无声地夸大了不为人知的例外情况(这可能会超出所显示的法典),这是一个普遍的问题。

除此以外,我假定,西半球OutputStream的fl没有真正冲动整个缓冲,但只有能够写成的许多区块,没有添加任何dding子。 此处使用<代码>close(,而不是flush()(另见第二例)。


一般性意见:A 小型自成一体的汇编实例将使我能够尝试,给你一个明确的答复,而不仅仅是猜测。 例如,“不归还任何东西”并不是对贵方方法行为的良好描述——方法返回<代码>null、空阵、 throw成例外、相互封锁吗?





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签