herInputStream和CipherOutputStrea

herInputStream和CipherOutputStrea

本文介绍了CipherInputStream和CipherOutputStream不生成文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码但是文件 b.xlsx c.xlsx 为0字节。为什么 CipherOuputSteam 不起作用?

  public static void main(String [ ] args)throws Exception {

KeyPair keys = KeyPairGenerator.getInstance(RSA)。generateKeyPair();
密码密码= Cipher.getInstance(RSA);

cipher.init(Cipher.ENCRYPT_MODE,keys.getPublic());

FileInputStream fis;
FileOutputStream fos;
CipherOutputStream cos;

fis = new FileInputStream(C:/temp/a.xlsx);
fos = new FileOutputStream(C:/temp/b.xlsx);

cos = new CipherOutputStream(fos,cipher);

byte [] block = new byte [8];
int i;
while((i = fis.read(block))!= -1){
cos.write(block,0,i);
}
cos.close();
fos.close();



cipher.init(Cipher.DECRYPT_MODE,keys.getPrivate());
CipherInputStream cis1,cis2;
fis = new FileInputStream(c:/temp/b.xlsx);
CipherInputStream cis = new CipherInputStream(fis,cipher);
fos = new FileOutputStream(c:/temp/c.xlsx);

while((i = cis.read(block))!= -1){
fos.write(block,0,i);
}
fos.close();
fis.close();
cis.close();
}


解决方案

问题在于您的使用 - 这是不正确的,并且执行 CipherOutputStream ,它掩盖了一个非常重要的异常 - IllegalBlockSizeException



问题是您不能使用RSA密钥来加密长度大于密钥大小的数据(在您的示例中为128个字节)。
您应该使用大型数据块的对称加密算法 - 例如 AES



如果要使用非对称密钥(例如安全传输数据),您可以找到一个很好的例子这个。


I have the following code. However the files b.xlsx and c.xlsx are of 0 bytes. Why is CipherOuputSteam not working?

public static void main(String[] args) throws Exception {

    KeyPair keys = KeyPairGenerator.getInstance("RSA").generateKeyPair();
    Cipher cipher = Cipher.getInstance("RSA");

    cipher.init(Cipher.ENCRYPT_MODE, keys.getPublic());

    FileInputStream fis;
    FileOutputStream fos;
    CipherOutputStream  cos;

    fis = new FileInputStream("C:/temp/a.xlsx");
    fos = new FileOutputStream("C:/temp/b.xlsx");

    cos = new CipherOutputStream (fos, cipher);

    byte[] block = new byte[8];
    int i;
    while ((i = fis.read(block)) != -1) {
        cos.write(block, 0, i);
    }
    cos.close();
    fos.close();



    cipher.init(Cipher.DECRYPT_MODE, keys.getPrivate());
    CipherInputStream cis1, cis2;
    fis = new FileInputStream("c:/temp/b.xlsx");
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    fos = new FileOutputStream("c:/temp/c.xlsx");

    while ((i = cis.read(block)) != -1) {
        fos.write(block, 0, i);
    }
    fos.close();
    fis.close();
    cis.close();
}
解决方案

The problem lies in your usage - which is incorrect and in the implementation of CipherOutputStream which masks a very important exception - IllegalBlockSizeException.

The problem is that you cannot use an RSA key to encrypt data which is longer than the size of the key (which is 128 bytes in your example).you should use a symmetric encryption algorithm for large blocks of data - e.g. AES.

If you want to use asymmetric keys for a reason (safe transmition of data for example) - you can find a good example on this SO answer.

这篇关于CipherInputStream和CipherOutputStream不生成文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 19:15