Friday, June 17, 2011

How to setup a Android development environment with eclipse

Follow the instructions given below.

1.Download Eclipse IDE

First step is to download Eclipse IDE from http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/ganymedesr2. We need this software to use as a development environment for Android.


2.Download Google Android SDK
We need to download Google Android Software Development Kit for development. Go to http://code.google.com/android/download.html, read and agree to the license and click the "continue" button. Choose the platform that you are currently running on. Once downloaded, locate the archive file and extract or expand it to a location that you will remember. Note the location, as you will need to enter this directory into Eclipse in a moment.

2.Configuring add-on in Eclipse
Start the Eclipse from installation directory.



Wednesday, May 11, 2011

Using AES ( CBC ) Encryption in java with Client Server

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

public class DesServer
{

 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,new IvParameterSpec(f.iv)));    
           
    break;
    }
    
    ois.close();
    is.close();
    s.close();
    ss.close();  
  
 }
 
  
 
 static String decryptString(String data,Key key,IvParameterSpec ivspec)throws Exception
 {
     KeyGenerator kg = KeyGenerator.getInstance("DES");
  kg.init(56);
  Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
  Key k = key;
  
  c.init(Cipher.DECRYPT_MODE,new SecretKeySpec("12345678".getBytes(),"DES"),ivspec);
  
  
  
  byte[] plaintext = c.doFinal(data.getBytes());
  
  return new String(plaintext);
 }
 
  
 
}

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

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




public class DesClient
{
    static KeyGenerator kg = null;
 static Cipher c = null;
 static Key k = null;
 static byte[] ivector=null;
 public static void main(String []str)
 {
  
  String datatosend=null;
     try
  {
      datatosend = encryptString(getDataFromFile("Data.txt"));
   Frame f = new Frame(datatosend,k,ivector);
   
   
   
   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("DES");
  kg.init(56);
  c = Cipher.getInstance("DES/CBC/PKCS5Padding");
  k = kg.generateKey();
  
    
  c.init(Cipher.ENCRYPT_MODE,new SecretKeySpec("12345678".getBytes(),"DES"));
  byte[] plaintext = data.getBytes();
  byte[] ciphertext = c.doFinal(plaintext);
  
  ivector = c.getIV();
          
  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 : 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;
 byte[] iv;

    Frame(String mydata,Key key,byte[] ivector)
    { 
  data=mydata;
  k = key;
  if(ivector==null)
  {
     iv = null;
  }
  else
  {
   iv = new byte[ivector.length];
   for(int i=0;i<ivector.length;i++)
   {
    iv[i]=ivector[i];
   }
  }
 }  

}