Circuit Negma

C++, C, VB.NET, PCB, Electronics, Circuit Design

Encrypt and Decrypt a File

Posted by Circuit Negma on February 6, 2006


import jencrypt.*; 
import java.io.*; 
 
// The example encrypt and decrypt file. 
public class EncryptFile { 
 
  public static void main(String[] args) { 
    byte[] password = "1234".getBytes(); // password 
    byte[] buffer = new byte[8192]; // temporary buffer 
    int length; 
 
    //Encrypt file 
    System.out.println("Start encrypt and decrypt file example ..."); 
    try { 
      System.out.println( 
          " 1. Encrypt 'original.txt' file into 'cipher.bin' file."); 
 
      // create input stream for read file 
      FileInputStream inFile = new FileInputStream("original.txt"); 
      BufferedInputStream bufInFile = new BufferedInputStream(inFile); 
 
      // create output stream for encrypt file 
      FileOutputStream outFile = new FileOutputStream("cipher.bin"); 
      BufferedOutputStream bufOutFile = new BufferedOutputStream(outFile); 
      EncryptOutputStream enOut = new EncryptOutputStream(password, bufOutFile); 
 
      // write encrypt file 
      while ( (length = bufInFile.read(buffer)) != -1) { 
        enOut.write(buffer, 0, length); 
      } 
 
      // close input and output stream 
      enOut.close(); 
      bufInFile.close(); 
    } 
    catch (Exception es) { 
      System.out.println(es); 
    } 
 
    //Decrypt file 
    try { 
      System.out.println(" 2. Decrypt 'cipher.bin' file into 'clear.txt'"); 
 
      // create input stream for read encrypted file 
      FileInputStream inFile = new FileInputStream("cipher.bin"); 
      BufferedInputStream bufInFile = new BufferedInputStream(inFile); 
      DecryptInputStream deIn = new DecryptInputStream(password, bufInFile); 
 
      // create output stream for write clear-text file 
      FileOutputStream outFile = new FileOutputStream("clear.txt"); 
      BufferedOutputStream bufOutFile = new BufferedOutputStream(outFile); 
 
      // write clear-text file 
      while ( (length = deIn.read(buffer)) != -1) { 
        bufOutFile.write(buffer, 0, length); 
      } 
 
      // close input and output stream 
      bufOutFile.close(); 
      deIn.close(); 
    } 
    catch (Exception es) { 
      System.out.println(es); 
    } 
    System.out.println(" 3. Please see 'cipher.bin' and 'clear.txt' file."); 
  } 
} 

3 Responses to “Encrypt and Decrypt a File”

  1. Peter said

    as-94783-sa

    hello

  2. Wilco said

    Hey there

    Thanks for this great post it was just the missing info that i needed for my study

    I have bookmarked your site in the hope that you will bring more of this great stuff to the table

    Greets

    Wilco Breens

  3. Hi,

    By the way, you can use the above code to encrypt and decrypt files.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: