2011-07-23 26 views
0

次のコードを使用して、sdcardのビデオファイルを暗号化しています。明らかに暗号化されている場合、ファイルは0kbです。これは起こるはずですか?ファイルの暗号化と復号化

package com.messageHider; 

import java.io.InputStream; 
import java.io.OutputStream; 
import java.security.spec.AlgorithmParameterSpec; 
import javax.crypto.Cipher; 
import javax.crypto.CipherInputStream; 
import javax.crypto.CipherOutputStream; 
import javax.crypto.SecretKey; 
import javax.crypto.spec.IvParameterSpec; 

public class DesEncrypter { 
    Cipher ecipher; 
    Cipher dcipher; 
    byte[] buf=null; 
    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) { 
     } 
    } 


    public void encrypt(InputStream in, OutputStream out,int fileSize) { 
     buf= new byte[fileSize]; 
     try { 
      // Bytes written to out will be encrypted 
      out = new CipherOutputStream(out, ecipher); 

      // Read in the cleartext bytes and write to out to encrypt 
      int numRead = 0; 
      while ((numRead = in.read(buf)) >= 0) { 
       out.write(buf, 0, numRead); 
      } 
      out.close(); 
     } catch (java.io.IOException e) { 
     } 
    } 

    public void decrypt(InputStream in, OutputStream out,int fileSize) { 
     buf= new byte[fileSize]; 
     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; 
      while ((numRead = in.read(buf)) >= 0) { 
       out.write(buf, 0, numRead); 
      } 
      out.close(); 
     } catch (java.io.IOException e) { 
     } 
    } 
} 

他のクラス:ここ はコードである

DialogInterface.OnClickListener dialoglistener=new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      switch(which) 
      { 
      case DialogInterface.BUTTON_POSITIVE: 
         new Thread(new Runnable() { 
          @Override 
          public void run() { 
           videoPathCursor=getContentResolver().query(videoUri, new String[]{VideoColumns.DATA,VideoColumns.SIZE}, VideoColumns.DISPLAY_NAME+"=?", new String[]{videoTitle}, null); 
           videoPathCursor.moveToFirst(); 
           videoPath=videoPathCursor.getString(videoPathCursor.getColumnIndex(VideoColumns.DATA)); 
           videoSize=videoPathCursor.getString(videoPathCursor.getColumnIndex(VideoColumns.SIZE)); 
           ContentValues values=new ContentValues(); 
           values.put(dbConnection.VIDEO_TITLE, videoTitle); 
           values.put(dbConnection.VIDEO_SIZE,videoSize); 
           dbConnection conn=new dbConnection(getApplicationContext()); 
           SQLiteDatabase db=conn.getWritableDatabase(); 
           db.insert(dbConnection.TABLE_VIDEOS, null, values); 
          } 
         }).start(); 
         new Thread(new Runnable() 
         { 
          @Override 
          public void run() { 
           try { 
            File sd=Environment.getExternalStorageDirectory(); 
            File outFile=new File(sd, "Encrypt/"+videoTitle); 
            FileInputStream fis=new FileInputStream(videoPath); 
            FileOutputStream fos=new FileOutputStream(outFile); 
            SecretKey key=KeyGenerator.getInstance("DES").generateKey(); 
            DesEncrypter encrypter=new DesEncrypter(key); 
            encrypter.encrypt(fis, fos, Integer.parseInt(videoSize)); 
            getContentResolver().delete(videoUri,VideoColumns.DISPLAY_NAME+"=?", new String[]{videoTitle}); 
            File vidFile=new File(videoPath); 
            vidFile.delete(); 
           } 
           catch (FileNotFoundException e) 
           { 
            e.printStackTrace(); 
           } 
           catch (NoSuchAlgorithmException e) { 

            e.printStackTrace(); 
           } 
          } 
         }).start(); 
       break; 
      case DialogInterface.BUTTON_NEGATIVE: 
       break; 
      } 
     } 
    }; 

答えて

0

いや、ファイルは明らかに私はあなたがFOSを閉じていないことがわかり0より大きくなければなりません。

fos.flush()とfos.close()を試してみると、ファイルが正しく書き込まれます。

+0

ちょうどそれが動作していないと試しました – John

+0

'vidFile.delete();'は何か関係がありますか? – rossum

+0

例外はありますか? –