Saturday, 24 December 2011

Java program to encrypt or decrypt a file in DES mode

Java can be very efficiently in encrypting files in DES mode. In order to encrypt any text or file , first it is necessary to generate a particular key that will be used for encryption. Next we have to create a Cipher object with the particular mode of encryption as its parameter. Then the Cipher object is initialized with the key and either ENCRYPT_MODE or DECRYPT_MODE . After all this is done, we just need to read a stream of bytes from a particular file using FileInputStream and encrypt it and then write to another file using FileOutputStream.
NOTE : The same key must be used for encrypting and decrypting the same file.
Code Extracts :
byte key[] = "klHGtyCD".getBytes(); //setting my own key
SecretKeySpec secretKey = new SecretKeySpec(key,"DES"); //key generated
Cipher encrypt=Cipher.getInstance("DES/ECB/PKCS5Padding"); //cipher object
encrypt.init(Cipher.ENCRYPT_MODE, secretKey); //initializing object


Click to DOWNLOAD the JAVA file of Encrypter from 4shared.com
Click to DOWNLOAD the JAVA file of Decrypter from 4shared.com

No comments:

Post a Comment