2009-07-27 7 views
4

私はBouncy Castleを使用してファイルシステムからXMLファイルを解読しています。私は復号化されたテキストを出力し、データの最後のバイトで致命的なエラーSAXParseExceptionを取得します。以下は私の解読方法と暗号オブジェクトの設定です。BouncyCastle最終バイト解読の問題

私は当初暗号ストリームを使用していましたが、すべてが完璧に機能しました(コメントアウトされたコードはストリームでした)。ポリシーファイルとエンドユーザーが256ビット無制限のバージョンを持っていないため、私はバウンシーキャッスルを使用する必要があります。

最後のバイトが通っていない理由は何ですか?データの最後のチャンクと

public void decrypt(InputStream in, OutputStream out) { 
    try 
    { 
     paddedBufferedBlockCipher.init(false, 
      new ParametersWithIV(keyParam, _defaultIv)); 
//   cipher.init(Cipher.DECRYPT_MODE, secretKey, ivs); 
//   CipherInputStream cipherInputStream 
//      = new CipherInputStream(in, cipher); 

     byte[] buffer = new byte[4096]; 
     byte[] outBuffer = new byte[4096]; 

     for (int count = 0; (count = in.read(buffer)) != -1;) { 
      paddedBufferedBlockCipher.processBytes(buffer, 0, 
       count, outBuffer, 0); 
      out.write(outBuffer, 0, count);   
     } 
    } 
    catch(Exception e) { 
     e.printStackTrace(); 
    } 
} 

[Fatal Error] :40:23: Element type "Publi" must be followed by either attribute specifications, ">" or "/>". 
org.xml.sax.SAXParseException: Element type "Publi" must be followed by either attribute specifications, ">" or "/>". 
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:264) 
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:292) 
+0

ストリームのオプションを確認する必要はありませんでした。私はループでこれを行うので、doFinal()は必要なく、うまくいくと仮定しました。私は "カウント"変数のシステムを実行し、最後の数字が何であるかを確認します –

+0

間違っています: "バッファの最後のブロックを処理します。バッファが現在いっぱいで、**パディングを追加する必要があります**コールtoFinalは2 * getBlockSize()バイトを生成します。 " – akarnokd

答えて

2

あなたは(doFinalを呼んでください):

keyParam = new KeyParameter(key); 
engine = new AESEngine(); 
paddedBufferedBlockCipher = 
    new PaddedBufferedBlockCipher(new CBCBlockCipher(engine)); 

復号化方法:コンストラクタから

public void decrypt(InputStream in, OutputStream out) { 
    try 
    { 
     paddedBufferedBlockCipher.init(false, 
      new ParametersWithIV(keyParam, _defaultIv)); 
     byte[] buffer = new byte[4096]; 
     byte[] outBuffer = new byte[4096]; 

     for (int count = 0; (count = in.read(buffer)) != -1;) { 
      int c2 = paddedBufferedBlockCipher.processBytes(buffer, 0, 
       count, outBuffer, 0); 
      out.write(outBuffer, 0, c2);      
     } 
     count = paddedBufferedBlockCipher.doFinal(outBuffer, 0); 
     out.write(outBuffer, 0, count);      
    } 
    catch(Exception e) { 
     e.printStackTrace(); 
    } 
} 
+0

最後に2行を追加すると、出力が正しくなります。私はXMLファイル全体を取得します。しかし、ファイルがうまく見えても、上記と同じSAXParseExceptionを取得します。 –

+0

解読が完了した直後にオブジェクトにXMLを逆シリアル化しようとしています。 –

+0

実際に試した2つのXMLファイルがあります。上記の例外があります。もう1つのファイルは、 "無効なXML文字(Unicode:0x0)が文書の要素内容に見つかりました。" –

関連する問題