文件加密和解密

时间:2011-07-23 18:20:10

标签: android encryption java-io

我使用以下代码加密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;
            }   
        }
    };

1 个答案:

答案 0 :(得分:0)

不,文件显然应该大于0.我看到你没有关闭fos。

尝试使用fos.flush()和fos.close(),然后正确编写文件。