Saturday, January 29, 2011

Image Encryption & Decryption in JAVA

// Client.java

import java.io.*;
import javax.crypto.*;
import java.security.*;
import java.util.*;
import javax.crypto.spec.*;
import java.net.*;

class client
{
    public static void main(String[] arr) throws Exception
    {

        int i;
        Key k;
        Cipher c;
        KeyGenerator kg;
        FileOutputStream fos;
        CipherInputStream cis;

        Scanner input = new Scanner(System.in);

        Socket cs = new Socket("localhost",1987);
   
        ObjectOutputStream oos = new ObjectOutputStream(cs.getOutputStream());

        c = Cipher.getInstance("DES");
        kg = KeyGenerator.getInstance("DES");
        k = kg.generateKey();

        c.init(Cipher.ENCRYPT_MODE,k);
   
        cis = new CipherInputStream(new FileInputStream(new File("my.jpg")),c);

        int j=0;

        oos.writeObject(k);       

        while((i=cis.read())!=-1)
        {
            oos.writeInt(i);
            oos.flush();
        }
        cs.close();
    }
}


// server.java

import java.io.*;
import javax.crypto.*;
import java.security.*;
import java.util.*;
import javax.crypto.spec.*;
import java.net.*;


class myserver implements Runnable
{
    Socket cs;
    Thread t;
    Cipher c;
    KeyGenerator kg;

    myserver(Socket cs)
    {
        this.cs=cs;

        try
        {

        }catch(Exception e)
        {
   
        }

        t = new Thread(this);
        t.start();
    }

    public void run()
    {

        try
        {
            c = Cipher.getInstance("DES");
            ObjectInputStream ois = new ObjectInputStream(cs.getInputStream());
            FileOutputStream fos = new FileOutputStream("myenc.jpg");

            Key k = (Key) ois.readObject();
            c.init(Cipher.DECRYPT_MODE,k);

            int i;
            while((i=ois.readInt())!=-1)
            {
                fos.write(i);
            }
        }
        catch(Exception e)
        {

        }
       
        try
        {
            CipherInputStream cis = new CipherInputStream(new FileInputStream(new File("myenc.jpg")),c);

            FileOutputStream fos1 = new FileOutputStream("again.jpg");

            int j;
            while((j=cis.read())!=-1)
                fos1.write(j);

            fos1.close();
        }
        catch(Exception e)
        {

        }   
    }
}

class server
{
    public static void main(String[] arr) throws Exception
    {
        Socket cs;
        ServerSocket ss = new ServerSocket(1987);

        while(true)
        {
            cs = ss.accept();
            new myserver(cs);
        }
    }
}

4 comments:

  1. how to execute tis program???

    ReplyDelete
  2. using command Prompt
    --------------------
    start java server
    start java client

    you can find one encrypted file at sibling location of original image file.

    ReplyDelete
  3. give me the java source code for hiding message using hiding key in an image , pls reply soon i need it immediately

    ReplyDelete
  4. Hi can you just describe the code. What code does in each special line? Because I want convert this Decryption part to android.
    Thank you

    ReplyDelete

Disqus for yogi's talk

comments powered by Disqus