Tuesday, May 10, 2011

Using AES ( ECB ) Encryption in java with Client Server

//File : AESClient.java
//AESClient
//Developed by Malhar Vora
//Developed on : 9-5-2011
import java.io.*;
import java.net.*;
import java.util.*;
import javax.crypto.*;
import java.security.*;
import javax.crypto.spec.*;




public class AESClient
{
    static KeyGenerator kg = null;
 static Cipher c = null;
 static Key k = null;
 public static void main(String []str)
 {
  
  String datatosend=null;
     try
  {
      datatosend = encryptString(getDataFromFile("Data.txt"));
   Frame f = new Frame(datatosend,k);
   
   
   
   sendFrame("localhost",5656,f);
    
  }
  catch(Exception e)
  {
   System.out.println(e);
  }
  
 }
 
 static String getDataFromFile(String filename) throws Exception 
 {
     String str="";
  Scanner sc  = new Scanner(new File(filename));
  while(sc.hasNextLine())
  {
   str=str + sc.nextLine();
  }
  sc.close();
  return str;
  
 }
 
 static String encryptString(String data) throws Exception
 {
     kg = KeyGenerator.getInstance("AES");
  c = Cipher.getInstance("AES");
  k = kg.generateKey();
  
  c.init(Cipher.ENCRYPT_MODE,k);
  byte[] plaintext = data.getBytes();
  byte[] ciphertext = c.doFinal(plaintext);
  
  return new String(ciphertext);
  
 }
 
 static void sendFrame(String remoteaddress,int port,Frame f) throws Exception
 {
     Socket s = new Socket(remoteaddress,port);
  OutputStream os = s.getOutputStream();
  ObjectOutputStream ous = new ObjectOutputStream(os);
  
  ous.writeObject(f);
  
  ous.close();
  os.close();
  s.close();       
 
 }
}

/********************************************************************/

//File : AESServer.java
//AESServer
//Developed by Malhar Vora
//Developed on : 9-5-2011
import java.io.*;
import java.net.*;
import java.util.*;
import javax.crypto.*;
import java.security.*;
import javax.crypto.spec.*;

public class AESServer
{

 public static void main(String []str)throws Exception
 {
    ServerSocket ss = new ServerSocket(5656);
    Socket s = null;
    InputStream is = null;
    ObjectInputStream ois = null;
    Frame f =null;
    
    while(true)
    {
    s=ss.accept();
    is = s.getInputStream();
    ois = new ObjectInputStream(is);
    f = (Frame)ois.readObject(); 
          System.out.println(decryptString(f.data,f.k));    
    
    break;
    }
    
    ois.close();
    is.close();
    s.close();
    ss.close();  
  
 }
 
  
 
 static String decryptString(String data,Key key)throws Exception
 {
     KeyGenerator kg = KeyGenerator.getInstance("AES");
  Cipher c = Cipher.getInstance("AES");
  Key k = key;
  
  c.init(Cipher.DECRYPT_MODE,k);
  byte[] plaintext = c.doFinal(data.getBytes());
  
  return new String(plaintext);
 }
 
}

/*****************************************************/

//File : Frame.java
//Frame Class
//Developed by Malhar Vora
//Developed on : 9-5-2011
import java.io.*;
import java.net.*;
import java.util.*;
import javax.crypto.*;
import java.security.*;
import javax.crypto.spec.*;

public class Frame implements Serializable
{
    
    String data;
    Key k;

    Frame(String mydata,Key key)
    { 
  data=mydata;
  k = key;
 }  

}


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.