How to Perform DES Encryption and Decryption of TXT file in JAVA with NetBeansIDE
1. Open NetBeansIDE.
2. Make a JFrame Form say EncryptTXT.java , I have already made it.
3. Make a class named DESEncrypt.java enter codes and Fix All Imports(It is necessary)
3.1 Write code in it
public static void encrypt(String key, InputStream is, OutputStream os) throws Exception {
encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os);
}
public static void decrypt(String key, InputStream is, OutputStream os) throws Exception {
encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os);
}
public static void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Exception {
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey desKey = skf.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
if (mode == Cipher.ENCRYPT_MODE) {
cipher.init(Cipher.ENCRYPT_MODE, desKey);
CipherInputStream cis = new CipherInputStream(is, cipher);
makeFile(cis, os);
} else if (mode == Cipher.DECRYPT_MODE) {
cipher.init(Cipher.DECRYPT_MODE, desKey);
CipherOutputStream cos = new CipherOutputStream(os, cipher);
makeFile(is, cos);
}
}
public static void makeFile(InputStream is, OutputStream os) throws IOException {
byte[] bytes = new byte[64];
int numBytes;
while ((numBytes = is.read(bytes)) != -1) {
os.write(bytes, 0, numBytes);
}
os.flush();
os.close();
is.close();
}
4. Right Click on ‘EncryptTXT.java’ form’s Encrypt button and Go to Events~action~actionPerformed
4.1 Paste the following code for Encrypt Button actionPerformed.
try{
JFileChooser fc=new JFileChooser();
fc.showOpenDialog(null);
String path=fc.getSelectedFile().getAbsolutePath();
jLabel2.setText(path);
File f=fc.getSelectedFile();
FileInputStream fis=new FileInputStream(f);
FileOutputStream fos=new FileOutputStream("encrypted.txt");
Thread.sleep(2000);
DESEncrypt.encrypt(key, fis, fos);
jLabel4.setText("C:\\Users\\raks\\Documents\\NetBeansProjects\\DESEncryption\\encrypted.txt");
}catch(Exception e)
{
System.out.print("Exception in EncryptTXT");
}
4.2 Paste the following code for Decrypt Button actionPerformed.
try{
JFileChooser fc=new JFileChooser();
fc.setCurrentDirectory(new File("C:\\Users\\raks\\Documents\\NetBeansProjects\\DESEncryption"));
fc.showOpenDialog(null);
String path=fc.getSelectedFile().getAbsolutePath();
jLabel2.setText(path);
File f=fc.getSelectedFile();
FileInputStream fis=new FileInputStream(f);
FileOutputStream fos=new FileOutputStream("decrypted.txt");
DESEncrypt.decrypt(key, fis, fos);
Thread.sleep(2000);
jLabel4.setText("C:\\Users\\raks\\Documents\\NetBeansProjects\\DESEncryption\\decrypted.txt");
}catch(Exception e)
{
System.out.print("Exception in EncryptTXT");
}
4.3 Paste the following code for Opening Encrypted File Folder button actionPerformed.
Runtime runtime=Runtime.getRuntime();
try {
runtime.exec("explorer.exe C:\\Users\\raks\\Documents\\NetBeansProjects\\DESEncryption");
} catch (IOException ex) {
Logger.getLogger(EncryptTXT.class.getName()).log(Level.SEVERE, null, ex);
}
5. Run the project with EncryptTXT.java file.
6. Finish
Hi,
ReplyDeleteRahul
Can you teach me java !!
M from Delhi Only
Thank you a lot.
ReplyDeletehey can you explain please how to create the menu in section 2 . i am using eclipse
ReplyDeletethanks
TINNING TINNING TINNING TINNING TINNING TINNING TINNING
ReplyDeleteTINNING TINNING TINNING titanium trim as seen on tv TINNING titanium teeth k9 TINNING TINNING TINNING TINNING is titanium expensive TINNING TINNING TINNING TINNING TINNING TINNING TINNING titanium nail TINNING TINNING TINNING micro touch titanium trimmer