首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

CipherOutputStream 转换有关问题

2013-04-02 
CipherOutputStream 转换问题public void decrypt(String file, String dest) throws Exception {Cipher c

CipherOutputStream 转换问题


public void decrypt(String file, String dest) throws Exception {
 
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, this.key, zeroIv);
        InputStream is = new FileInputStream(file);
        OutputStream out = new FileOutputStream(dest);
        CipherOutputStream cos = new CipherOutputStream(out, cipher);
        byte[] buffer = new byte[1024];
        int r;
        while ((r = is.read(buffer)) >= 0) {
            cos.write(buffer, 0, r);
        }
        cos.close();
        out.close();
        is.close();
    }



这样结果是得到解密后的文件,然后在读取文件,现在我不想去读解密后的文件,直接读流,该怎么做?或者说上面的方法我想要得到一个InputStream或String,然后供其他接口使用,该怎么做?
在论坛上看到

   while ((r = is.read(buffer)) >= 0) {
            cos.write(buffer, 0, r);
            System.out.println(new String(buffer, 0, r));
        }

但打印出来的是未解密的字符串,求高人指点。 解密 buffer string CipherOutputStream? Cipher?
[解决办法]
引用:

public void decrypt(String file, String dest) throws Exception {
  ...
}

这样结果是得到解密后的文件,然后在读取文件,现在我不想去读解密后的文件,直接读流,该怎么做?或者说上面的方法我想要得到一个InputStream或String,然后供其他接口使用,该怎么做?


CipherInputStream 不是正满足你的要求吗?

public void decrypt(String file, String dest) throws Exception {
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, this.key, zeroIv);
    InputStream is = new FileInputStream(file);
    CipherInputStream cis = new CipherInputStream(in, cipher);
    OutputStream os = new FileOutputStream(dest);
    byte[] buffer = new byte[1024];
    int r;
    while ((r = cis.read(buffer)) >= 0) {
        os.write(buffer, 0, r);
    }
    os.close();
    cis.close();
}

热点排行