Java Program to Send a File over a Network after Encryption

Here is a simple java program which can be used to send and receive encrypted files over network. The sender encrypts the file before sending and the receiver decrypts the file and displays it. The program is based on a networking lab experiment at my college. So, although we say 'encryption', characters are simply incremented here. The lab question was as follows:

A client wants a file that is with a server in a secure system. The server encrypts the file in such a way that 'A' will be replaced with 'B', 'B' with 'C'... and 'Z' with 'A' and sends to the client. The client should get file and decrypt it to the original one and display the content. Implement it using TCP/IP.


So, the program is a sample program. The 'encryption' here is just as in the question although it is not worthy to be called 'encryption'. The program uses ServerSocket and Socket classes for TCP/IP socket connections. I have not written separate codes for server and client. Rather, i wrote a single simple java program which can act as either the server (sender) or a client (receiver). In the program, the user will be prompted to choose whether to be server or a client. The sample java program for encrypted secure file transfer over a network is as follows:


import java.awt.Color;
import java.io.*;
import java.net.*;
import javax.swing.*;


public class EncryptedTransfer extends JFrame{
File myfile;
boolean sender;
Socket s;
ServerSocket ss;
JTextArea ta=new JTextArea();
JProgressBar jpb=new JProgressBar();
public EncryptedTransfer(boolean send)
{
try{
sender=send;
setLayout(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(325,75);
getContentPane().setBackground(Color.BLACK);
jpb.setBackground(Color.BLACK);
jpb.setForeground(Color.BLUE);
add(jpb);
jpb.setBounds(5, 5, 300, 25);
if(sender)
    {
    JFileChooser jfc=new JFileChooser();
    jfc.setDialogTitle("Select the file to send");
    int t=jfc.showOpenDialog(null);
    if(t==JFileChooser.APPROVE_OPTION)
        {
        myfile=jfc.getSelectedFile();
        ss=new ServerSocket(19999);
        ss.setSoTimeout(0);
        System.out.println("ip:"+InetAddress.getLocalHost().getHostAddress());
        s=ss.accept();
        }
    else System.exit(0);
    }
else
    {
    setSize(325,335);
    String ip=JOptionPane.showInputDialog("Enter sender ip:");
    s=new Socket(InetAddress.getByName(ip),19999);
    JScrollPane jsp=new JScrollPane(ta);
    add(jsp);
    jsp.setBounds(5,35,300,260);
    }
thread.start();
}catch(Exception ex){ex.printStackTrace();}
}

public String encrypt(String s)
{
String tt="";
char c;
for(int i=0;i<s.length();i++)
{
c=s.charAt(i);
if((c>='A'&&c<'Z')||(c>='a'&&c<'z'))
    c++;
else if(c=='Z'||c=='z')
    c-=25;
tt=tt+c;
}
return tt;
}

public String decrypt(String s)
{
String tt="";
char c;
for(int i=0;i<s.length();i++)
{
c=s.charAt(i);
if((c>'A'&&c<='Z')||(c>'a'&&c<='z'))
    c--;
else if(c=='A'||c=='a')
    c+=25;
tt=tt+c;
}
return tt;
}

public static void main(String args[])    
{
Object[] opt={"Send a file","Receive a file"};
int i=JOptionPane.showOptionDialog(null,"Select an option","What to do?",JOptionPane.DEFAULT_OPTION,JOptionPane.QUESTION_MESSAGE, null,opt,opt[0]);
if(i==0)
    new EncryptedTransfer(true).setVisible(true);
else
    new EncryptedTransfer(false).setVisible(true);
}

Thread thread=new Thread()
{
public void run()
{
try{
s.setSoTimeout(0);
if(sender)
    {
    PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
    BufferedReader br=new BufferedReader(new FileReader(myfile));
    pw.println("Size="+myfile.length());
    String s="";
    int l=0;
    for(;(s=br.readLine())!=null;)
        {
        s=encrypt(s);
        l+=s.length()+1;
        jpb.setValue(l*100/(int)myfile.length());
        pw.println(s);
        }
    ss.close();
    jpb.setValue(100);
    }
else
    {
    s.setSoTimeout(1000);
    BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
    long size=Long.parseLong(br.readLine().substring(5));
    String s,encrypted="";
    try{
        while((s=br.readLine())!=null)
        {
        encrypted+=s+"\n";
        jpb.setValue(encrypted.length()*100/(int)size);
        }
    }catch(Exception ex){}
    ta.setText("Encrypted:\n"+encrypted+"\nDecrypted:\n"+decrypt(encrypted));
    jpb.setValue(100);
    }
}catch(Exception ex){ex.printStackTrace();}
}
};
}

No comments:

Post a Comment