0

(モデレーターへ)以前に関連する問題を投稿したことがありますが、これはより完全な投稿であるため、重複として閉じないでください。前の投稿を閉じることができます。

コンソール出力で-1を取得するたびに、出力ストリームにデータは書き込まれません。コンソール出力で3を取得するたびに、有効なデータが出力ストリームに書き込まれます。-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

これは復号化関数の呼び出しです。

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);

入力ストリームが作成されるExcelファイルには、コンソール出力のセルコンテンツに表示されるすべてのセルに関するデータがあります..有効な入力ストリームを使用しても(次のように)-1のコンソール出力が得られる理由を見つけるのを手伝ってください。

4

3 に答える 3

10

この無意味な行を削除します。

System.out.println(in.read(buf));

データをバッファに読み込みますが、それ以上無視しています! whileステートメント内で行う次の呼び出しでは、同じデータは読み戻されませが、バッファー長を超える次のバイトのみが読み取られます。本当に興味がある場合は、ループ内のsysout を実行することをお勧めします。numRead

inもう1つの問題は、引数を のものに置き換えていることですInputStream:

in = new CipherInputStream(in, dcipher);

それを独自のローカル変数に割り当て、メソッドの引数を変更しないでください。finalコンパイラがエラーを出すように、将来それらを宣言するのが最善です。例えば

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

または、メソッドの引数をオーバーライドするときに警告を表示するように IDE を構成することもできます (これは通常、悪い習慣です)。たとえば Eclipse では、Java > Compiler > Errors/Warnings > Name shadowing and conflicts > set all to Warningまたは多分Errorでこれを行うことができます。

于 2009-11-25T13:47:43.810 に答える
1

考えてみてください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);

そして何が起こるか見てください。

于 2009-11-25T14:10:47.040 に答える
1

ここと他の質問で検出された他の問題に加えて、あなたの本当の問題はencodingに関連している可能性があると思います。Excelファイルのエンコーディングは何ですか?

エンコーディングが何であるかがわかったら、次のことができます。

theCell_00.getBytes("MYENCODING");

また、暗号を作成する方法と、可能であれば鍵を教えていただけますか?

于 2009-11-25T14:29:52.137 に答える