I'm writing a small app to help me learn more about cryptography. All
it does is encrypt all of the files in directory A, and put the
encrypted versions of the files in directory B. It then decrypts the
files in directory B, and puts them in directory C. I'm getting some
exceptions when I try to decrypt a binary file though. Here's the
code:
//////////////////////////////////////////////////////////////////////
public class FileEncrypter
{
String PATH = "";
String JAR_FILE_NAME = "img.jar";
final String ENCRYPTED_FILES_DIRECTORY_NAME = "encrypted_files";
final String DECRYPTED_FILES_DIRECTORY_NAME = "decrypted_files";
SecretKeySpec specKey = null;
Cipher cipher = null;
// byte[] encryptedBytes;
public FileEncrypter( String path )
{
this.PATH = path;
try
{
init();
encryptFiles();
}
catch ( Exception ex )
{
ex.printStackTrace();
}
System.out.println( "done" );
}
private void init() throws NoSuchPaddingException,
NoSuchAlgorithmException, FileNotFoundException
{
//make directory for the encrypted files
File encryptedDirectory = new File( PATH + File.separator +
ENCRYPTED_FILES_DIRECTORY_NAME );
encryptedDirectory.mkdir();
//make directory for the decrypted files
File decryptedDirectory = new File( PATH + File.separator +
DECRYPTED_FILES_DIRECTORY_NAME );
decryptedDirectory.mkdir();
//initialize encryption objects
cipher = Cipher.getInstance( "Blowfish" );
KeyGenerator kgen = KeyGenerator.getInstance( "Blowfish" );
SecretKey secretKey = kgen.generateKey();
byte[] bytes = secretKey.getEncoded();
System.out.println("Key is: [" + new
sun.misc.BASE64Encoder().encode(bytes) + "]");
specKey = new SecretKeySpec( bytes, "Blowfish" );
}
private void encryptFiles()
{
//create an array of all the files in the file directory
File imgDir = new File( PATH );
String[] files = imgDir.list();
File originalFile = null;
File encryptedFile = null;
File decryptedFile = null;
//loop thru the array of file names
for ( int i = 0; i < files.length; i++ )
{
//create a file object from the current file name in the
array
originalFile = new File( PATH + File.separator + files[ i ]
);
//ignore directories
if ( !originalFile.isDirectory() )
{
//create an empty copy of the file and put it in the
encrypted files directory
encryptedFile = new File( originalFile.getParent() +
File.separator + ENCRYPTED_FILES_DIRECTORY_NAME, "encrypted_" + files[
i ] );
//create an empty copy of the file and put it in the
decrypted files directory
decryptedFile = new File( originalFile.getParent() +
File.separator + DECRYPTED_FILES_DIRECTORY_NAME, files[ i ] );
//encrypt the file
encryptFile( originalFile, encryptedFile );
//decrypt the file
decryptFile( encryptedFile, decryptedFile );
}
}
}
private void decryptFile(File encryptedFile, File fileToDecrypt )
{
FileOutputStream decryptedFileOutputStream = null;
FileInputStream fileInputStream = null;
try
{
fileInputStream = new FileInputStream( encryptedFile );
decryptedFileOutputStream = new FileOutputStream(
fileToDecrypt );
//set cipher to decrypt
cipher.init( Cipher.DECRYPT_MODE, specKey );
int fileByteSize = fileInputStream.available();//research
FileInputStream.available
//read in the encrypted bytes into a byte array
byte[] encryptedBytes = new byte[fileByteSize];
fileInputStream.read( encryptedBytes );
//decrypt the encrypted file bytes
//this line throws these
exceptions:javax.crypto.BadPaddingException: Given final block not
properly padded;
//javax.crypto.IllegalBlockSizeException: Input length (with
padding) not multiple of 8 bytes
byte[] decryptedBytes = cipher.doFinal( encryptedBytes );
//write the encrypted bytes into the file
decryptedFileOutputStream.write( decryptedBytes, 0,
fileByteSize );
}
catch ( Exception ex )
{
ex.printStackTrace();
}
finally
{
if( fileInputStream != null ) try{ fileInputStream.close(); }
catch( IOException ignore ){}
if( decryptedFileOutputStream != null ) try{
decryptedFileOutputStream.close(); } catch( IOException ignore ){}
}
}
private void encryptFile(File originalFile, File fileToEncrypt )
{
FileOutputStream encryptedFileOutputStream = null;
FileInputStream fileInputStream = null;
try
{
fileInputStream = new FileInputStream( originalFile );
encryptedFileOutputStream = new FileOutputStream(
fileToEncrypt );
//set cipher to encrypt
cipher.init( Cipher.ENCRYPT_MODE, specKey );
int fileByteSize = fileInputStream.available();//research
FileInputStream.available
//read in the original bytes into a byte array
byte[] originalBytes = new byte[fileByteSize];
fileInputStream.read( originalBytes );
//encrypt the original file bytes
byte[] encryptedBytes = cipher.doFinal( originalBytes );
//write the encrypted bytes into the file
encryptedFileOutputStream.write( encryptedBytes, 0,
fileByteSize );
}
catch ( Exception e )
{
e.printStackTrace();
}
finally
{
if ( fileInputStream != null ) try{ fileInputStream.close();
}catch( IOException ioe ){}
if ( encryptedFileOutputStream != null ) try{
encryptedFileOutputStream.close(); }catch( IOException ioe ){}
}
}
public static void main( String args[] )
{
String PATH = "C:" + File.separator + "java" + File.separator +
"projects" + File.separator + "Encryption" + File.separator + "img";
new FileEncrypter( PATH );
}
}
////////////////////////////////////////////////////////////////////////////
I think this has something to do with ASCII characters that are
created when the original binary is encrypted maybe? Thanks in advance
for any help. Chris