I import javautil import javaio import javaximageioImageIO i

I

import java.util.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class hidingtext {

public static void main(String[] args) {
  
Scanner input = new Scanner(System.in);
System.out.print(\"Enter the input image file name (with .jpg extension): \");
String file = input.next();

BufferedImage img;

try {
img = ImageIO.read(new File(file));
// There is a method ImageIO.write() to create a
// picture file from a BufferedImage object
  
int h = img.getHeight();
int w = img.getWidth();
System.out.println(\"Image height = \" + h + \" and Image width = \" + w);
System.out.println(\"Total number of pixels = \" + (h*w));
  
// store each pixel\'s RGB values
int[][] pixelData = new int[h*w][3];
int[] rgb;

int numberOfPixels = 0;
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
rgb = PixelData(img, j, i);

for(int k = 0; k < rgb.length; k++){
pixelData[numberOfPixels][k] = rgb[k];
}

numberOfPixels++;
}
}

} catch (IOException e) {
e.printStackTrace();
}

}

private static int[] PixelData(BufferedImage img, int x, int y) {
int argb = img.getRGB(x, y);
  
int[] rgb = new int[3];
rgb[0] = (argb >> 16) & 0xff; //Red
rgb[1] = (argb >> 8) & 0xff; // Green
rgb[2] = (argb) & 0xff; // Blue

// 0xff is hex for 255 and used as a mask to get last 8 bits of a bit string

System.out.println(\"Red: \" + rgb[0] + \", Green: \" + rgb[1] + \", Blue: \" + rgb[2]);
return rgb;
}

}

Steganography is the technique of hiding secret messages/information within other nonsecret data/information. One popular way of steganography is hiding text within images. Each image is a collection of pixels, where each pixel is typically represented by 3 values (RGB). Each of R, G, and B is a value between 0 and 255 and thus can be represented by 8 bits. The color of the pixel depends on these values. However, if the least significant bit (last bit from the right) is changed for each of R, G, B then the resulting change in pixel color may not be noticeable. Using this fact, we can convert a text message to bits and embed them one by one in R, G, B values of the pixel. That is, each R, G, B value\'s last bit actually carries one bit of the secret message. This is one way to create a covert (secret) channel to pass information. Upon receipt of the image, the receiver can extract the last bit from each R, G, B and combine them to reveal the secret message. In this group assignment you are required to write a Java program that can embed and extract simple text message into a chosen JPEG image. The program should also allow to extract the message from the image. In particular, the program should do the following: Prompts user to enter a choice of JPEG image to hide a text message. Prompts user to enter the text message that is to be hidden. Create a modified image after hiding the text message (as explained above) and save the image as a new JPEG picture. Display the hidden message when a JPEG image is supplied. [For testing purpose, the user may choose to use a different image file that already has a message hidden into it.] [This program is simple in terms of coding complexity (use of simple loops and arrays), but the logic (design of the steps) should he carefully thought of Also, it is important to use the Java API methods appropriately.]

Solution

import java.awt.*;

import java.io.*;

import javax.swing.*;

public class Sender extends JFrame implements Runnable, ActionListener {
JLabel l1,l4;
JComboBox c1;
JButton b1,b2,b3,b4,b5;
Socket s=null;
JTextArea t1;
Container c;
JScrollPane scr;
Connection con=null;
Statement st=null;
BufferedImage bg;
long length;
String ll;
FileInputStream fis;
Vector v1,v2,v3;
String filename=\"\";
  

Sender()
{
v1=new Vector();
v2=new Vector();
v3=new Vector();
c1=new JComboBox();
  
c1.addItem(\"1234\");

l1=new JLabel(\"Select Image\");
l2=new JLabel(\"Your Selected Image is Here!!!\");
b1=new JButton(\"Cancel\");
b2=new JButton(\"Browse\");
b3=new JButton(\"Encrypt\");
b4=new JButton(\"Send\");
t1=new JTextArea();
t1.setLineWrap(true);
scr=new JScrollPane(t1);
c=this.getContentPane();
c.setLayout(null);
l1.setBounds(10, 10, 100, 40);
c1.setBounds(100, 10, 100, 40);
b1.setBounds(10, 60, 100, 40);
b2.setBounds(120,60,100,40);
l2.setBounds(10,120,100,40);
b3.setBounds(120,120,100,40);
l3.setBounds(250,50,400,400);
l4.setBounds(270,10,200,30);
scr.setBounds(10,220,200,200);
b4.setBounds(10,440,100,40);
b5.setBounds(120,440,100,40);
c.add(l1);
c.add(c1);
c.add(b1);
c.add(b2);
c.add(l2);
c.add(b3);
c.add(l3);
c.add(scr);
c.add(b4);
b2.setEnabled(false);
b3.setEnabled(false);
b4.setEnabled(false);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);


  
  
}
public void actionPerformed(ActionEvent e)

{ if(o==b2)
{
System.exit(0);
}
if(o==b3)
{

try
{
FileDialog fd=new FileDialog(this,\"openfile\",FileDialog.LOAD);
fd.setVisible(true);
String path=fd.getDirectory()+fd.getFile();
System.out.println(path);
File f1=new File(path);
System.out.println(f1.length());
length=f1.length();
ll=fd.getFile()+\"@\"+length;
fis=new FileInputStream(f1);
filename=fd.getFile();
BufferedImage br=ImageIO.read(f1);
bg=new BufferedImage(400,400,BufferedImage.TYPE_INT_RGB);
Graphics2D g=bg.createGraphics();
g.drawImage(br,0,0,400,400,null);
g.dispose();
Icon i=new ImageIcon(bg);
l3.setIcon(i);
c.add(l4);
b4.setEnabled(true);
}
catch(javax.imageio.IIOException e2)
{
  
}
catch(java.net.ConnectException e3)
{
JOptionPane.showMessageDialog(\" \");
}
catch(Exception e1)
{
  
e1.printStackTrace();
  
}
  
}
  
if(o==b3)
{
b5.setEnabled(true);
System.out.println(\"Encryption\");
b4.setEnabled(false);
  
  
//Encryption
String s2=t1.getText();
String s3=enc.enc(s2);
String s1=s2+\"%\"+s3;
System.out.println(\"text is\"+s1);
String data=\"\";
for(int i=0;i<s1.length();i++)
{
char cha=s1.charAt(i);
System.out.println(\"charecter is\"+cha);
int dat=(int)cha;
System.out.println(\"Ascii data is\"+dat);
  
v1.add(dat);//v1=new Vector();
data+=dat+\"@\";
System.out.println(data);
  
  
}
  
System.out.println(\"Final data is\"+data);
String data1[]=data.split(\"@\");

try
{
String encdata=\"\";
int i1=0;
  

}
catch(Exception e111)
{
e111.printStackTrace();
  
  
}


  
//Image retreival
int i11;
try {
System.out.println(\"Size of image was \"+fis.available());
while((i11=fis.read())!=-1)
{
  
  
v2.add(i11);
  
}
System.out.println(\"vector two\"+v2.size());
System.out.println(\"Vector one\"+v1.size());
} catch (IOException ex) {
ex.printStackTrace();
}
  
  
  
for(int x=0;x<v2.size();x++)
{
if(x==12)
{
for(int y=0;y<v1.size();y++)
{
v3.add(v1.get(y));
}
}
else
{
v3.add(v2.get(x));
}

  
  
}
try {
File gh=new File(\"client\\\\\"+filename);
FileOutputStream fis1=new FileOutputStream(gh);
  
for(int vv=0;vv<v3.size();vv++)
{
fis1.write(Integer.parseInt(v3.get(vv).toString()));
  
}
System.out.println(v3.size());
fis1.close();
fis.close();
JOptionPane.showMessageDialog(this,\"Encrypted Succesfully\");
  }

catch (Exception ex)

{
ex.printStackTrace();
}
  
  
}
if(o==b5)
{
try
{
  
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
   dos.writeUTF(ll);
FileInputStream fii=new FileInputStream(\"client\\\\\"+filename);
byte b[]=new byte[fii.available()];
DataInputStream dis=new DataInputStream(fii);
dis.read(b,0,b.length);
ObjectOutputStream os1=new ObjectOutputStream(os);
os1.writeObject(b);
   int siz=v1.size();
   DataOutputStream dd=new DataOutputStream(os);
dd.write(siz);
JOptionPane.showMessageDialog(this, \"Sent Successfully\");
dis.close();
os1.close();
b5.setEnabled(false);
}
catch(Exception e1)
{
  
e1.printStackTrace();
  
}
}
}
public static void main(String [] args)
{
Sender s=new Sender();
s.setTitle(\"Sender\");
s.setSize(800,800);
s.setVisible(true);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
}
  
}

I import java.util.*; import java.io.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; public class hidingtext { public static void main(Str
I import java.util.*; import java.io.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; public class hidingtext { public static void main(Str
I import java.util.*; import java.io.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; public class hidingtext { public static void main(Str
I import java.util.*; import java.io.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; public class hidingtext { public static void main(Str
I import java.util.*; import java.io.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; public class hidingtext { public static void main(Str
I import java.util.*; import java.io.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; public class hidingtext { public static void main(Str

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site