import javax.swing.*;Execute the above program.
import java.io.*;
public class encrypt
{
public static void main(String arg[]) throws FileNotFoundException, IOException
{
String choice = JOptionPane.showInputDialog("Encrypt(E)/Decrypt(D)");
String file= JOptionPane.showInputDialog("File Name:");
File usrfile = new File(file);
File encfile = new File("enc.txt");
encfile.createNewFile();
BufferedWriter userStream = new BufferedWriter( new FileWriter(encfile));
try
{
PushbackReader in = new PushbackReader( new BufferedReader ( new FileReader(usrfile)));
int c=-4,i=0,temp=0;
while(c!=-1)
{
c=in.read();
temp=c;
if(c==-1)break;
if(choice.equals("e")||choice.equals("E"))
{
if(temp%2==0)temp+=9;
else temp-=7;
}
else if(choice.equals("d")||choice.equals("D"))
{
if(temp%2!=0)temp-=9;
else temp+=7;
}
userStream.write(String.valueOf((char)temp));
}
in.close();
userStream.close();
usrfile.delete();
File usrfile2 = new File(file);
encfile.renameTo(usrfile2);
}
catch(IOException e)
{
JOptionPane.showMessageDialog(null,e);
}
System.exit(0);
}
}
Encrypt(E)/Decrypt(D)
File Name:
Sample File
Encrypted File
To get back the non-gibberish form or decrypted text, execute the code once again and enter d in the first window and file name in the following window.
Note:
- This code can be used to encrypt/decrypt only files encoded in Text Format
- The file to be encrypted has to be in the same folder from which the code is executed.
- Sound knowledge of Java is required to understand and successfully execute the above code.