java—为什么我总是将-1作为in.read(buf)的值?

java—为什么我总是将-1作为in.read(buf)的值?,java,encryption,aes,Java,Encryption,Aes,对于版主,请注意,我之前发布过一个相关问题,但这是一个更完整的帖子,所以请不要重复关闭它。您可以关闭上一个帖子 每当我在控制台输出中得到-1时,outputstream中不会写入任何数据,每当我在控制台输出中得到3时,outputstream中会写入有效数据。在不同的场合,-1和-3的出现是随机的 这是密码 public void decrypt(InputStream in, OutputStream out) { try { // Bytes read from in will

对于版主,请注意,我之前发布过一个相关问题,但这是一个更完整的帖子,所以请不要重复关闭它。您可以关闭上一个帖子

每当我在控制台输出中得到-1时,outputstream中不会写入任何数据,每当我在控制台输出中得到3时,outputstream中会写入有效数据。在不同的场合,-1和-3的出现是随机的

这是密码

public void decrypt(InputStream in, OutputStream out) {
  try {
    // Bytes read from in will be decrypted
    in = new CipherInputStream(in, dcipher);

    // Read in the decrypted bytes and write the cleartext to out
    int numRead = 0;
    System.out.println(in.read(buf));
    while ((numRead = in.read(buf)) >= 0) {
      out.write(buf, 0, numRead);
    }
    //out.close();
  }
  catch (java.io.IOException e) {
  }
}
这里是控制台输出

the Cell Content : ¼OKs>N?h¸GX&ŸH
-1
the Cell Content : 3Ëù%¥þ-]'±ŠM݆
3
the Cell Content : ´`?û…óï>»†µýÆ$
-1
the Cell Content : 9ûÊ‘øxIÐ8`ýÊeú
3
the Cell Content : •x€ÌWã’ç4æ~?Gû{
-1
the Cell Content : ÉJ‹SD
-1
the Cell Content : ¯'´öƒ²wCÐ)/
3
the Cell Content : ¼?\š
-1
the Cell Content : 4¤¢ÃUÚړ‹ïEk?É•
-1
the Cell Content : vì=¨;°e¼~{GÀ“È"?
3
the Cell Content : 0ò*"MoañôefU?ô?
-1
the Cell Content : –çä,M9"kIF FJÅ
3
the Cell Content : ¬¼aâÅ•b鉭-SoP~Æ
3
the Cell Content : œ¦LKØ•0I¾>n=á
3
the Cell Content : Å'?X °¡¯“nJ/0è˜
3
the Cell Content : 3™æ&‡õvâr`õ_4¾õ
3
the Cell Content : l羚jT/`‚«h™&
3
the Cell Content : ~à‘_X;eÜ$8º!šŒì
3
the Cell Content : ùݳ9ˆ>‰Liœr‡
3
the Cell Content : UzÛ,­»è–­Üí‡AB®µ
3
the Cell Content : ’ùZnë¥æFó¦–ñ?~
-1
the Cell Content : 4ê¶È˜¬ ”Ôÿ4vä
3
这是对decrypt函数的调用

InputStream excelResource=new FileInputStream(path);
Workbook rwb=Workbook.getWorkbook(excelResource);
int sheetCount=rwb.getNumberOfSheets();
Sheet rs = rwb.getSheet(0);
int rows = rs.getRows();
int cols = rs.getColumns();
for(int i=0; i<rows; i++) {
  for(int j=0; j<Col.length; j++) {
    String theCell_00 = rs.getCell(j,i).getContents();
    System.out.println("the Cell Content : " + theCell_00);

    in = new ByteArrayInputStream(theCell_00.getBytes());
    out = new FileOutputStream("c:\\Decrypted.txt");
    encrypter.decrypt(in, out);

从中创建inputstream的excel文件在控制台输出的单元格内容中显示的每个单元格上都有数据。。请帮助我找出为什么即使使用有效的inputstream,我也会得到-1控制台输出。

删除这一无意义的行:

System.out.println(in.read(buf));
它确实会将数据读入缓冲区,但您会进一步忽略它!在while语句中执行的下一个调用不会读回相同的数据,而只读回超出缓冲区长度的下一个字节。如果你真的感兴趣的话,最好在循环中使用sysout-of-numRead

另一个问题是您正在用另一个InputStream替换in参数:

将其分配给自己的局部变量,并保持方法参数不变。最好是在将来将它们声明为final,这样编译器就会给出错误。例如

public void decrypt(final InputStream in, final OutputStream out)

或者,您也可以将IDE配置为在重写方法参数时显示警告,这通常是一种不好的做法。例如,在Eclipse中,您可以通过Java>编译器>错误/警告>名称阴影和冲突>将所有设置为警告或可能是错误来实现这一点。

只是一个想法,尝试不要在加密/清除输入流中使用In。如:

  public void decrypt(InputStream ciph_in, OutputStream out) {
  InputStream in;
  try {
    // Bytes read from in will be decrypted
    in = new CipherInputStream(ciph_in, dcipher);

看看会发生什么。

除了在这里和中检测到的其他问题之外,我认为您真正的问题可能与编码有关。Excel文件的编码是什么

一旦知道编码是什么,您可以执行以下操作:

theCell_00.getBytes("MYENCODING");

另外,您能告诉我们您是如何创建密码的吗?如果可能的话,还可以创建密钥吗?

好的,您是对的,但这并不能解决问题。。oustream在循环中丢失了很多次,我只是用它来演示它丢失的时间。。我想你应该找到真正的problem@rover12:对不起?我想你应该提供一个更好的SSCCE。请改变你的态度,现在看来你是在把自己的过错推到我这边。这并不鼓励我进一步帮助你。@BaulsC对不起,亲爱的,我不是故意粗鲁的。。我太累了,想在这里找到虫子。。。你的评论没有解决问题,所以我得到了。。忘乎所以。抱歉,真是太好了!rover12始终覆盖in字段,因此稍后将始终关闭CipherInputStream,但从不关闭原始BufferedInputStream。流不应该被声明为类成员..考虑一下,但是对于17个测试中的10个,他可以完美地解密,我想如果编码是痛苦的原因,那么测试应该总是解码,但永远不会产生有效的输出,现在他在某些情况下收到零输出
theCell_00.getBytes("MYENCODING");