Problem Decrypting Binary File

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chris

    Problem Decrypting Binary File

    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_file s";
    final String DECRYPTED_FILES _DIRECTORY_NAME = "decrypted_file s";

    SecretKeySpec specKey = null;
    Cipher cipher = null;

    // byte[] encryptedBytes;

    public FileEncrypter( String path )
    {
    this.PATH = path;
    try
    {
    init();
    encryptFiles();
    }
    catch ( Exception ex )
    {
    ex.printStackTr ace();
    }
    System.out.prin tln( "done" );
    }

    private void init() throws NoSuchPaddingEx ception,
    NoSuchAlgorithm Exception, FileNotFoundExc eption
    {
    //make directory for the encrypted files
    File encryptedDirect ory = new File( PATH + File.separator +
    ENCRYPTED_FILES _DIRECTORY_NAME );
    encryptedDirect ory.mkdir();

    //make directory for the decrypted files
    File decryptedDirect ory = new File( PATH + File.separator +
    DECRYPTED_FILES _DIRECTORY_NAME );
    decryptedDirect ory.mkdir();

    //initialize encryption objects
    cipher = Cipher.getInsta nce( "Blowfish" );
    KeyGenerator kgen = KeyGenerator.ge tInstance( "Blowfish" );
    SecretKey secretKey = kgen.generateKe y();
    byte[] bytes = secretKey.getEn coded();

    System.out.prin tln("Key is: [" + new
    sun.misc.BASE64 Encoder().encod e(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.i sDirectory() )
    {
    //create an empty copy of the file and put it in the
    encrypted files directory
    encryptedFile = new File( originalFile.ge tParent() +
    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.ge tParent() +
    File.separator + DECRYPTED_FILES _DIRECTORY_NAME , files[ i ] );

    //encrypt the file
    encryptFile( originalFile, encryptedFile );
    //decrypt the file
    decryptFile( encryptedFile, decryptedFile );
    }
    }
    }

    private void decryptFile(Fil e encryptedFile, File fileToDecrypt )
    {
    FileOutputStrea m decryptedFileOu tputStream = null;
    FileInputStream fileInputStream = null;
    try
    {
    fileInputStream = new FileInputStream ( encryptedFile );
    decryptedFileOu tputStream = new FileOutputStrea m(
    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:java x.crypto.BadPad dingException: Given final block not
    properly padded;
    //javax.crypto.Il legalBlockSizeE xception: Input length (with
    padding) not multiple of 8 bytes
    byte[] decryptedBytes = cipher.doFinal( encryptedBytes );

    //write the encrypted bytes into the file
    decryptedFileOu tputStream.writ e( decryptedBytes, 0,
    fileByteSize );
    }
    catch ( Exception ex )
    {
    ex.printStackTr ace();
    }
    finally
    {
    if( fileInputStream != null ) try{ fileInputStream .close(); }
    catch( IOException ignore ){}
    if( decryptedFileOu tputStream != null ) try{
    decryptedFileOu tputStream.clos e(); } catch( IOException ignore ){}
    }
    }

    private void encryptFile(Fil e originalFile, File fileToEncrypt )
    {
    FileOutputStrea m encryptedFileOu tputStream = null;
    FileInputStream fileInputStream = null;
    try
    {
    fileInputStream = new FileInputStream ( originalFile );
    encryptedFileOu tputStream = new FileOutputStrea m(
    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
    encryptedFileOu tputStream.writ e( encryptedBytes, 0,
    fileByteSize );
    }
    catch ( Exception e )
    {
    e.printStackTra ce();
    }
    finally
    {
    if ( fileInputStream != null ) try{ fileInputStream .close();
    }catch( IOException ioe ){}
    if ( encryptedFileOu tputStream != null ) try{
    encryptedFileOu tputStream.clos e(); }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
Working...