File encryption and decryption
I am using the following code to encrypt video files on the sdcard. apparently when it is encrypted, the file is 0kb. Is this supposed to happen? Here is the code:
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) {
}
}
}
The other class:
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)
开发者_Go百科 {
e.printStackTrace();
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
}).start();
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
Nope, The file should obviously be greater than 0. I see that you are not closing the fos.
Try fos.flush() and fos.close(), and then the file would be written correctly.
精彩评论