File encryption is the science of writing the contents of a text file in a secre
ID: 3539172 • Letter: F
Question
File encryption is the science of writing the contents of a text file in a secret code. Your encryption/decryption program should work like a filter, reading the contents of one file, modifying the data into a code, and then writing the coded contents out to a second file. The second file will be a version of the first file, but written in a secret code.
Although there are complex encryption techniques, you should come up with a simple one of your own. For example, you could read the first file one character at a time, and add 10 to the ASCII code of each character before it is written to the second file.
The program should have a menu that allows the user to perform the following tasks:
The source and destination file names should be displayed as part of the menu, if the user has specified.
how can i start fisrt...... plz help
Explanation / Answer
For encryption we need a key based on which the encryption will be done. Here we will use some classes Cipher,CipherInputStream,CipherOutputStream,SecretKeySpec. One thing you have to always remember is that the same key must be used both for encryption and decryption. We will use Cipher stream classes as only one function call is required for both either reading/writing and encryption/decryption. Otherwise we would have to call update() method for encryption/decryption and then write() for writing to file.
SecretKeySpec class : This class specifies a secret key in a provider-independent fashion and is only useful for raw secret keys that can be represented as a byte array and have no key parameters associated with them, e.g., DES or Triple DES keys.
Cipher class : This class provides the functionality of a cryptographic cipher for encryption and decryption. It forms the core of the Java Cryptographic Extension (JCE) framework. Its getInstance()method is called to get the object based on algorithm. Then the init() method is called for initializing the object with encryption mode and key.
CipherInputStream class : It is composed of an InputStream and a Cipher so that read() methods return data that are read in from the underlying InputStream but have been additionally processed by the Cipher. The Cipher must be fully initialized before being used by a CipherInputStream. It is used for decryption and does read and then update operation.
CipherOutputStream class : Just like above it is also composed of a stream and cipher and the cipher must be fully initialised before using this stream. It is used for encryption purpose.
--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.SecretKeySpec;
public class FileEncryptor{
private String algo;
private File file;
public FileEncryptor(String algo,String path) {
this.algo=algo; //setting algo
this.file=new File(path); //settong file
}
public void encrypt() throws Exception{
//opening streams
FileInputStream fis =new FileInputStream(file);
file=new File(file.getAbsolutePath()+".enc");
FileOutputStream fos =new FileOutputStream(file);
//generating key
byte k[] = "HignDlPs".getBytes();
SecretKeySpec key = new SecretKeySpec(k,algo.split("/")[0]);
//creating and initialising cipher and cipher streams
Cipher encrypt = Cipher.getInstance(algo);
encrypt.init(Cipher.ENCRYPT_MODE, key);
CipherOutputStream cout=new CipherOutputStream(fos, encrypt);
byte[] buf = new byte[1024];
int read;
while((read=fis.read(buf))!=-1) //reading data
cout.write(buf,0,read); //writing encrypted data
//closing streams
fis.close();
cout.flush();
cout.close();
}
public void decrypt() throws Exception{
//opening streams
FileInputStream fis =new FileInputStream(file);
file=new File(file.getAbsolutePath()+".dec");
FileOutputStream fos =new FileOutputStream(file);
//generating same key
byte k[] = "HignDlPs".getBytes();
SecretKeySpec key = new SecretKeySpec(k,algo.split("/")[0]);
//creating and initialising cipher and cipher streams
Cipher decrypt = Cipher.getInstance(algo);
decrypt.init(Cipher.DECRYPT_MODE, key);
CipherInputStream cin=new CipherInputStream(fis, decrypt);
byte[] buf = new byte[1024];
int read=0;
while((read=cin.read(buf))!=-1) //reading encrypted data
fos.write(buf,0,read); //writing decrypted data
//closing streams
cin.close();
fos.flush();
fos.close();
}
public static void main (String[] args)throws Exception {
new FileEncryptor("DES/ECB/PKCS5Padding","sample.txt").encrypt();
new FileEncryptor("DES/ECB/PKCS5Padding","sample.txt.enc").decrypt();
}
}
NOTE : The generated decrypted file name is not the same as that of original so taht you can check whether same contents have been generated or not. You can change this part
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.