Sei sulla pagina 1di 6

import java.io.

File;
import java.io.*;
//import java.awt.Container;
import java.awt.event.ActionEvent;
//import java.awt.image.BufferedImage;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;

public class Steganography_Controller


{
private static int mult(int x, int y, int n)
{
int k = 1;
int j;
for (j = 1; j<=y; j++)
k = (k * x) % n;
return (int) k;
}
private Steganography_View view;
private Steganography model;

private JPanel decode_panel;


private JPanel encode_panel;
private JTextArea input;
private JButton encodeButton,decodeButton;
private JLabel image_input;
private JMenuItem encode;
private JMenuItem decode;
private JMenuItem exit;

private Encode enc;


private Decode dec;
private EncodeButton encButton;
private DecodeButton decButton;

private String stat_path = "";


private String stat_name = "";

public Steganography_Controller(Steganography_View aView, Steganography


aModel)
{
view = aView;
model = aModel;

encode_panel = view.getTextPanel();
decode_panel = view.getImagePanel();
input = view.getText();
image_input = view.getImageInput();
encodeButton = view.getEButton();
decodeButton = view.getDButton();
encode = view.getEncode();
decode = view.getDecode();
exit = view.getExit();

enc = new Encode();


encode.addActionListener(enc);
dec = new Decode();
decode.addActionListener(dec);
exit.addActionListener(new Exit());
encButton = new EncodeButton();
encodeButton.addActionListener(encButton);
decButton = new DecodeButton();
decodeButton.addActionListener(decButton);

encode_view();
}

private void encode_view()


{
update();
view.setContentPane(encode_panel);
view.setVisible(true);
}

private void decode_view()


{
update();
view.setContentPane(decode_panel);
view.setVisible(true);
}

private class Encode implements ActionListener


{
public void actionPerformed(ActionEvent e)
{
encode_view();
}
}
private class Decode implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
decode_view();
JFileChooser chooser = new JFileChooser("./");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRE
CTORIES);
chooser.setFileFilter(new Image_Filter());
int returnVal = chooser.showOpenDialog(view);
if (returnVal == JFileChooser.APPROVE_OPTION){
File directory = chooser.getSelectedFile();
try{
String image = directory.getPath();
stat_name = directory.getName();
stat_path = directory.getPath();
stat_path = stat_path.substring(0,stat_p
ath.length()-stat_name.length()-1);
stat_name = stat_name.substring(0, stat_
name.length()-4);
image_input.setIcon(new ImageIcon(ImageI
O.read(new File(image))));
}
catch(Exception except) {
JOptionPane.showMessageDialog(view, "The File ca
nnot be opened!",
"Error!", JOptionPane.INFORMATION_MESSAG
E);
}
}
}
}

private class Exit implements ActionListener


{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}

private class EncodeButton implements ActionListener


{
public void actionPerformed(ActionEvent e)
{
JFileChooser chooser = new JFileChooser("./");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRE
CTORIES);
chooser.setFileFilter(new Image_Filter());
int returnVal = chooser.showOpenDialog(view);
if (returnVal == JFileChooser.APPROVE_OPTION){
File directory = chooser.getSelectedFile();
try{
String text = input.getText();
String ext = Image_Filter.getExtension(
directory);
String name = directory.getName();
String path = directory.getPath();
path = path.substring(0,path.length()-na
me.length()-1);
name = name.substring(0, name.length()-4
);
String stegan = JOptionPane.showInputDia
log(view,
"Enter o
utput file name:", "File name",
JOptionP
ane.PLAIN_MESSAGE);
if(model.encode(path,name,ext,stegan,tex
t))
{
JOptionPane.showMessageDialog(vi
ew, "The Image was encoded Successfully!",
"Success!", JOptionPane.
INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(vi
ew, "The Image could not be encoded!",
"Error!", JOptionPane.IN
FORMATION_MESSAGE);
}
decode_view();
image_input.setIcon(new ImageIcon(ImageI
O.read(new File(path + "/" + stegan + ".png"))));
}
catch(Exception except) {
JOptionPane.showMessageDialog(view, "The File ca
nnot be opened!",
"Error!", JOptionPane.INFORMATION_MESSAG
E);
}
}
}
}

private class DecodeButton implements ActionListener


{
public void actionPerformed(ActionEvent e)
{
String message = model.decode(stat_path, stat_name);
System.out.println(stat_path + ", " + stat_name);
if(message != "")
{
encode_view();
JOptionPane.showMessageDialog(view, "The Image w
as decoded Successfully!",
"Success!", JOptionPane.
INFORMATION_MESSAGE);
input.setText(message);
}
else
{
JOptionPane.showMessageDialog(view, "The Image c
ould not be decoded!",
"Error!", JOptionPane.IN
FORMATION_MESSAGE);
}
}
}

public void update()


{
//input.setText(""); image_in
put.setIcon(null); //clear image
stat_path = "";
stat_name = "";
}

public static void main(String args[])


{
// String msg = new String(new char[100]);
int[] pt = new int[100];
int[] ct = new int[100];
int n;
int d;
int e;
int p;
int q;
int i;
System.out.print("Enter message : ");
String string = "";
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader msg = new BufferedReader(input);
// read in user input
try
{
string = msg.readLine();
}
catch(Exception g){}
System.out.println("You typed: " + string);
// strcpy(pt, msg);
for(i = 0;i<string.length();i++)
pt[i] = string.charAt(i);
n = 253;
d = 17;
e = 13;
System.out.print("\nCT = ");
for(i = 0; i<string.length(); i++)
ct[i] = mult(pt[i], e,n);
for(i = 0; i<string.length(); i++)
System.out.printf("%d ", ct[i]);
System.out.print("\nPT = ");
for(i = 0; i<string.length(); i++)
System.out.printf("%c", pt[i]);
for(i = 0; i<string.length(); i++)
pt[i] = mult(ct[i], d,n);
new Steganography_Controller(new Steganography_View("Steganography",ct,string.le
ngth()),
new Steganography());
}
}

Potrebbero piacerti anche