2012-03-27 14 views
1

ファイルを部分的に暗号化することをenvolvesする何かを試してください。この小さな問題を除いてすべてがうまくいっています。何らかの理由で。 flushメソッドはn> 52まで動作します。ここで、nはループの数です。解読方法でそれを見ることができます。私がn fomrを< 10に変更した場合、<がフラッシュされます。私はファイルを見てそれをテストしました。 53まで新しい内容は追加されていません。OutputstreamのFlushメソッドは何もしません。

public class DesEncrypter { 
Cipher ecipher; 
Cipher dcipher; 

DesEncrypter(SecretKey key) { 
    // Create an 8-byte initialization vector 
    byte[] iv = new byte[]{ 
     (byte)0x8E, 0x12, 0x39, (byte)0x9C, 
     0x07, 0x72, 0x6F, 0x5A 
    }; 
    AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); 
    try { 
     ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); 
     dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); 

     // CBC requires an initialization vector 
     ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); 
     dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); 
    } catch (java.security.InvalidAlgorithmParameterException e) { 
    } catch (javax.crypto.NoSuchPaddingException e) { 
    } catch (java.security.NoSuchAlgorithmException e) { 
    } catch (java.security.InvalidKeyException e) { 
    } 
} 

// Buffer used to transport the bytes from one stream to another 
byte[] buf = new byte[1024]; 

public void encrypt(InputStream in, OutputStream out) { 
    try { 
     // Bytes written to out will be encrypted 
     AppendableOutputStream out_append = new AppendableOutputStream(out); 

     OutputStream out_c = new CipherOutputStream(out_append, ecipher); 

     // Read in the cleartext bytes and write to out to encrypt 
     int numRead = 0; 
     int count = 0; 
     int max = 1024; 
     boolean first = true; 

     while ((numRead = in.read(buf, 0, max)) > 0) {     
      System.out.println("running Total: " + count); 
      count += numRead; 
      // if this read puts as at less than a meg, encrypt 
      if(count <= 1024*1024){ 
       System.out.println("encrypted " + numRead + " of " + max +" bytes : total " + count); 
       out_c.write(buf, 0, numRead); 
       // last encryption pass, close buffer and fix max 
       if(count == 1024*1024){ 
        // fix reading 1k in case max was decreased 
        max = 1024; 
        out_c.close(); 
       } 
       // if next read will go over a meg, read less than 1k 
       else if(count + max > 1024*1024) 
        max = 1024*1024 - count; 
      } 
      // past the first meg, don't encrypt 
      else{ 
       System.out.println("processed " + numRead + " of " + max +" bytes : total " + count); 
       out.write(buf, 0, numRead); 
      } 

     } 
     out.close(); 

    } catch (java.io.IOException e) {} 

} 

// Movies encrypt only 1 MB 128 passes. 

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

     // Read in the decrypted bytes and write the cleartext to out 
     int numRead = 0; 
     int count = 0; 
     int max = 1024; 

     while ((numRead = in_c.read(buf, 0, max)) > 0) { 
      count += numRead; 
      System.out.println("decrypted " + numRead + " of " + max +" bytes : total " + count); 
      out.write(buf, 0, numRead); 
      if(count + max > 1024*1024){ 
       max = 1024*1024 - count; 
      } 
      if(count == 1024*1024) 
       max = 0; 
     } 

     //in.skip(count); 
     int n = 0; 
     while((numRead = in.read(buf)) > 0 && n < 10){ 
     count += numRead; 
     System.out.println("processed " + numRead + " of 1024 bytes : total " + count); 
      out.write(buf,0,numRead); 
      //System.out.println("buf"+buf.length +" numered" + numRead+ " n"+n); 
      // If i look at the file after anything under n < 51 the file doesn't change. 
      n++; 

     } 
     out.flush(); 
     out.close(); 
    } catch (java.io.IOException e) { 
     System.out.println("AHHHHHHHH!!!!!!"); 
    } 
} 
+4

これは決して良い考えではありません。catch(java.io.IOException e){}。空のキャッチブロックは、問題があるかどうかわからないことを意味します。スタックトレースを少なくとも印刷します。私はこれを見るのが好きではありません:System.out.println( "AHHHHHHH !!!!!!");少ない情報。スタックトレースを出力します。 – duffymo

+0

デバッガはあなたの友人です。あなたはそれを使って、その方法で起こっていることを見ているべきです。 –

+0

私は心から質問を理解していません。 flushメソッドはn> 52まで動作しますが、53まではフラッシュされず、フラッシュ呼び出しはループの外側にあり、ループが終了するまで実行されません。実際の問題は何かを明確にすることができますか? –

答えて

1

私は推測することができます。 decryptメソッドを呼び出すときに、OutputStreamoutのタイプが指定されている場合は、少なくとも非常に役立ちます。 flush()メソッドの動作は、具体的な実装によって異なります。例えば

あなたの出力ストリームがCipherOutputStream、またはストリームのそのタイプに接続されているいくつかの他のストリームであることを起こる場合は、そのflush()方法の説明は次の(私が強調重要な部分を)状態:

カプセル化された暗号オブジェクトによって既に処理されたバッファリングされた出力バイトを強制的に書き出し、この出力ストリームをフラッシュします。 カプセル化された暗号によってバッファされ、それによって処理されるのを待っているバイトは書き出されません。たとえば、カプセル化された暗号がブロック暗号であり、書き込みメソッドの1つを使用して書き込まれた合計バイト数が暗号のブロックサイズより小さい場合、バイトは書き込まれません。

それはあなたの問題の原因である可能性があります。しかし、私が述べたように、特定のストリームの種類に関する詳細を知ることは非常に役に立ちます。

0

私がやっていたことは、復号化メソッドの入力ストリームがデータを乱していたようです。それを読み返していたときに復号化するのではなく、処理していたときに復号化しました。パッディングもちょっと混乱しました。解読方法で1024 * 1024のコードに余分な8バイトを加えなければならなかった。 誰もが気にしている場合は、改訂された方法があります 皆さん、ありがとうございました。

public void encrypt(InputStream in, OutputStream out) { 
    try { 
     // Bytes written to out will be encrypted 
     AppendableOutputStream out_append = new AppendableOutputStream(out); 

     OutputStream out_c = new CipherOutputStream(out_append, ecipher); 

     // Read in the cleartext bytes and write to out to encrypt 
     int numRead = 0; 
     int count = 0; 
     int max = 1024; 
     boolean first = true; 

     while ((numRead = in.read(buf, 0, max)) > 0) {     
      //System.out.println("running Total: " + count); 
      count += numRead; 
      // if this read puts as at less than a meg, encrypt 
      if(count <= 1024*1024){ 
       //System.out.println("encrypted " + numRead + " of " + max +" bytes : total " + count); 
       out_c.write(buf, 0, numRead); 
       // last encryption pass, close buffer and fix max 
       if(count == 1024*1024){ 
        // fix reading 1k in case max was decreased 
        max = 1024; 
        out_c.close(); 
       } 
       // if next read will go over a meg, read less than 1k 
       else if(count + max > 1024*1024) 
        max = 1024*1024 - count; 
      } 
      // past the first meg, don't encrypt 
      else{ 
       //System.out.println("processed " + numRead + " of " + max +" bytes : total " + count); 
       out.write(buf, 0, numRead); 
      } 

     } 
     out.flush(); 

     out.close(); 

    } catch (java.io.IOException e) { 

     System.out.println("AHHHHHHHH!!!!!!111"); 

    } 

} 

// Movies encrypt only 1 MB 128 passes. 

public void decrypt(InputStream in, OutputStream out) { 
    try { 
     // Bytes written to out will be decrypted 
     AppendableOutputStream out_append = new AppendableOutputStream(out); 
     System.out.println(ecipher.getOutputSize(1024*1024)); 
     OutputStream out_d = new CipherOutputStream(out_append, dcipher); 

     // Read in the decrypted bytes and write the cleartext to out 
     int numRead = 0; 
     int count = 0; 
     int max = 1024; 

     while ((numRead = in.read(buf, 0, max)) > 0) { 
      count += numRead; 
      if(count <= ecipher.getOutputSize(1024*1024)){ 
       out_d.write(buf, 0, numRead); 
       // last encryption pass, close buffer and fix max 
       if(count == ecipher.getOutputSize(1024*1024)){ 
        // fix reading 1k in case max was decreased 
        max = 1024; 
        out_d.close(); 
       } 
       // if next read will go over a meg, read less than 1k 
       else if(count + max > ecipher.getOutputSize(1024*1024)) 
        max = ecipher.getOutputSize(1024*1024) - count; 
      } 
      // past the first meg, don't decrypt 
      else{ 
       out.write(buf, 0, numRead); 
      } 

     } 
     out.close(); 
    } catch (java.io.IOException e) { 
    } 
} 
関連する問題