2011-07-23 310 views
0

我正在使用以下代碼來加密SD卡上的視頻文件。顯然,當它被加密時,該文件是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

不,該文件應明顯大於0我看,你是不是關閉FOS更大。

嘗試fos.flush()和fos.close(),然後該文件將被正確寫入。

+0

剛剛嘗試過bt,它不起作用 – John

+0

可以'vidFile.delete();'有什麼關係嗎? – rossum

+0

你有沒有例外? –