How to Encrypt a File in Java


Ever since version 1.4, the Java SDK Standard Edition has included the Java Cryptography Extension by default. This provides a unified framework for Java developers to use a variety of standard encryption algorithms in their applications. Using one of the standard algorithms that come with the Java Cryptography Extension is fairly easy, and a very simple encryption application can be created in only a few dozen lines to secure your own data files.

Things You'll Need

  • Computer<br />Java<br />Text editor or Java IDE
  • Create the FileEncrypter class. <br /><br />public class FileEncrypter {<br /> public static void main(String[] args) throws Exception {<br /> << insert code here>><br /> }<br />}<br /><br />The class will require the following imports:<br /><br />import<br />GO<br />import<br />GO<br />import<br />GO<br />import javax.crypto.Cipher<br />GO<br />import javax.crypto.KeyGenerator<br />GO<br />import javax.crypto.SecretKey<br />GO<br /><br />There are a number of extensions and enhancements you should be able to add to this class on your own, but it will provide a basic encryption functionality for you to build features such as a user interface on top of.

  • Declare and initialize the variables you’ll need. At the minimum, you’ll need a Cipher, a KeyGenerator and a SecretKey. You’ll also need a byte array each to hold the encrypted and unencrypted data. <br /><br /> byte[] plainData<br />GO<br /> byte[] encryptedData<br />GO<br /> KeyGenerator keygen = KeyGenerator.getInstance(\"DES\")<br />GO<br /> SecretKey key = keygen.generateKey()<br />GO<br /> Cipher cipher = Cipher.getInstance(\"DES/ECB/PKCS5Padding\")<br />GO<br /> cipher.init(Cipher.ENCRYPT_MODE, key)<br />GO<br /><br />This function will use DES encryption, but the Java Cryptography Extension also includes a variety of other schemes. You’ll also need to initialize an input and output file and populate the plainData array from the data in the input file. <br /><br /> File f = new File(\"input.txt\")<br />GO<br /> FileInputStream in = new FileInputStream(f)<br />GO<br /> plainData = new byte[(int)f.length()]<br />GO<br />;

  • Perform the encryption. This is actually the easiest task for you to perform and can be accomplished in a single line. <br /><br /> encryptedData = cipher.doFinal(plainData);

  • Write the encrypted data to a new file. <br /><br /> FileOutputStream target = new FileOutputStream(new File(\"encrypted.txt\"))<br />GO<br /> target.write(encryptedData)<br />GO<br /> target.close();

Tips & Warnings

  • There are a number of enhancements that can be made to this program. As written, for example, it does not store the randomly generated SecretKey as the individual programmer may be the best judge of how to deal with this sensitive piece of information. In addition, programmers may want to add command line arguments such as “input” and “output.” Finally, the program could be slightly restructured to implement better Object Oriented Design techniques.

Related Searches


  • Photo Credit Jupiterimages/Pixland/Getty Images
Promoted By Zergnet


You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!