5 Replies - 16022 Views - Last Post: 24 October 2010 - 11:12 PM Rate Topic: -----

#1 small baby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-October 10

Playfair Cipher Code

Posted 24 October 2010 - 01:01 PM

Sorry for that .. i have a code but i think that u can make it
really sorry

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class assig extends JFrame

{



private JTextField txtKey,txtInp,txtOut;
private JLabel lab=new JLabel("I = j AND i");
cahr toBeFound1, toBeFound2;






private JButton comClear,comDec,comEnc,ext;
Container container = getContentPane();
////////////////method
public void division()
{

char m[];


String mou=txtKey.getText().toUpperCase();
m=new char[mou.length()];

int zx=0;
char zz[];



String mou1="ABCDEFGHIKLMNOPQRSTUVWXYZ";
int i,j;
String out=" ";
String key = "";
zz=new char[mou1.length()];
//input data to key
for( i=0; i<mou1.length();i++)
{for ( j=0;j<key.length();j++)
{
if((mou1.charAt(i)==key.charAt(j))||(mou1.charAt(i)=='i'&& key.charAt(j)=='j')||(mou1.charAt(i)=='j'&& key.charAt(j)=='i'))
break;
}
if (j==key.length()){
out+=mou1.charAt(i);
}
out=key+out;




for(i=0;i<mou.length();i++)
{
m[i]=mou.charAt(i);
}//end for

}

}//end Division




public assig()
{
super("assignment#1");
createFrame();

comClear.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{
for(int i=0;i<5;i++)
txtOut.setText("");
txtInp.setText("");
txtKey.setText("");
}});

//String c=txtOut.getText();

comDec.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{
division();



txtInp.setText("");
txtOut.setText("djsklgjsdgj");



}});

comEnc.addActionListener(new ActionListener()
{
String mz=txtInp.getText();
public void actionPerformed(ActionEvent e)
{

txtInp.setText(mz);
txtOut.setText("");


}});


ext.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{
System.exit(0);

}});

}//end constracter



public void createFrame()
{
container.setLayout(null);




//textfield
txtKey = new JTextField("key");
txtInp = new JTextField("Input");
txtOut = new JTextField("out");
//array=new JTextField[5][5];
comClear=new JButton("Clear");
comEnc=new JButton("DECRYPT");
comDec=new JButton("ENCRYPT");
ext=new JButton("Exit");
lab.setBounds(200,150,200, 200);






txtKey .setBounds(500,10,100,30);
txtInp.setBounds(10,50,150,100);
txtOut.setBounds(10,160,150,100);
comClear.setBounds(10,10,100,30);
comDec.setBounds(120,10,100,30);
comEnc.setBounds(230,10,100,30);
ext.setBounds(340,10,100,30);

// array1[0].setBounds(230,10,100,30);


container.add(lab);
container.add(txtKey);
container.add(txtInp);
container.add(txtOut);
container.add(comClear);
container.add(comDec);
container.add(comEnc);
container.add(ext);




}//end method createframe

public static void main(String asd[])
{
assig assig = new assig();
assig.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
assig.setSize(700,450);
assig.setLocation(180,100);
assig.setVisible(true);
}//end main


}//end class 



this is my code
and i have another code for encryption
but i didn't know how to make the 2 dimensional array in the first class
because i make the encryption code in 2 dimensional array

this is the code
char toFind1,new1;
char toFind2,new2;
int row1,row2;
int col1,col2;

for(int i=0; i<5; i++)
for(int j=0; j<5; i++)
if (toFind==char[i][j])
{
row1=i;
col1=j;
}

for(int i=0; i<5; i++)
for(int j=0; j<5; j++)

if (toFind2== char[i][j])
{
row2=i;
col2=j;
}

if(row1==row2)
{
new1=table1[row][(col1+1)%5];
new2=table1[row1][(col2+1)%5];
}
else if (col1==col2)
{
new1=table1[(row1+1)%5][col1];
new2=table2[(row2+1)%5][col1];
}
else
{
new1=table1[(row2][col2];
new2=table1[(row2][col1];
}



and i want to fill the char table by this code
int count=0;
char[][] table1;
for(int i=0; i<5; i++)
for(int j=0; j<5; j++)
{
char [i][j]=newtxt.charAt(count);
count++;
}



at the end .. i want the matrix which is 5X5
if u need any information i wrote it in the notepad that attached

---------------

sorry again for bothering u
i hope that this question is not abuse :)

thank for replying
best regards ..

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Playfair Cipher Code

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Playfair Cipher Code

Posted 24 October 2010 - 04:05 PM

I wrote a tutorial about Playfair cypher code
Go see it

And please indent your code correctly, nobody will try to decipher it the way it is posted

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class assig extends JFrame

{



	private JTextField txtKey,txtInp,txtOut;
	private JLabel lab=new JLabel("I = j AND i");
	cahr toBeFound1, toBeFound2;






	private JButton comClear,comDec,comEnc,ext;
	Container container = getContentPane();
	////////////////method
	public void division()
	{

		char m[];


		String mou=txtKey.getText().toUpperCase();
		m=new char[mou.length()];

		int zx=0;
		char zz[];



		String mou1="ABCDEFGHIKLMNOPQRSTUVWXYZ";
		int i,j;
		String out=" ";
		String key = "";
		zz=new char[mou1.length()];
		//input data to key
		for( i=0; i<mou1.length();i++)
		{for ( j=0;j<key.length();j++)
		{
			if((mou1.charAt(i)==key.charAt(j))||(mou1.charAt(i)=='i'&& key.charAt(j)=='j')||(mou1.charAt(i)=='j'&& key.charAt(j)=='i'))
				break;
		}
		if (j==key.length()){
			out+=mou1.charAt(i);
		}
		out=key+out;




		for(i=0;i<mou.length();i++)
		{
			m[i]=mou.charAt(i);
		}//end for

		}

	}//end Division




	public assig()
	{
		super("assignment#1");
		createFrame();

		comClear.addActionListener(new ActionListener()
		{public void actionPerformed(ActionEvent e)
		{
			for(int i=0;i<5;i++)
				txtOut.setText("");
			txtInp.setText("");
			txtKey.setText("");
		}});

		//String c=txtOut.getText();

		comDec.addActionListener(new ActionListener()
		{public void actionPerformed(ActionEvent e)
		{
			division();



			txtInp.setText("");
			txtOut.setText("djsklgjsdgj");



		}});

		comEnc.addActionListener(new ActionListener()
		{
			String mz=txtInp.getText();
			public void actionPerformed(ActionEvent e)
			{

				txtInp.setText(mz);
				txtOut.setText("");


			}});


		ext.addActionListener(new ActionListener()
		{public void actionPerformed(ActionEvent e)
		{
			System.exit(0);

		}});

	}//end constracter



	public void createFrame()
	{
		container.setLayout(null);




		//textfield
		txtKey = new JTextField("key");
		txtInp = new JTextField("Input");
		txtOut = new JTextField("out");
		//array=new JTextField[5][5];
		comClear=new JButton("Clear");
		comEnc=new JButton("DECRYPT");
		comDec=new JButton("ENCRYPT");
		ext=new JButton("Exit");
		lab.setBounds(200,150,200, 200);






		txtKey .setBounds(500,10,100,30);
		txtInp.setBounds(10,50,150,100);
		txtOut.setBounds(10,160,150,100);
		comClear.setBounds(10,10,100,30);
		comDec.setBounds(120,10,100,30);
		comEnc.setBounds(230,10,100,30);
		ext.setBounds(340,10,100,30);

		// array1[0].setBounds(230,10,100,30);


		container.add(lab);
		container.add(txtKey);
		container.add(txtInp);
		container.add(txtOut);
		container.add(comClear);
		container.add(comDec);
		container.add(comEnc);
		container.add(ext);




	}//end method createframe

	public static void main(String asd[])
	{
		assig assig = new assig();
		assig.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		assig.setSize(700,450);
		assig.setLocation(180,100);
		assig.setVisible(true);
	}//end main


}//end class 


Was This Post Helpful? 0
  • +
  • -

#3 small baby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-October 10

Re: Playfair Cipher Code

Posted 24 October 2010 - 08:51 PM

ok .. sorry for that

and i'll see ur tutorial ..

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Playfair Cipher Code

Posted 24 October 2010 - 09:00 PM

http://www.dreaminco...-playfair-code/

It is part of a serie I made
Was This Post Helpful? 0
  • +
  • -

#5 small baby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-October 10

Re: Playfair Cipher Code

Posted 24 October 2010 - 09:26 PM

thank u very much :)
Was This Post Helpful? 0
  • +
  • -

#6 small baby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-October 10

Re: Playfair Cipher Code

Posted 24 October 2010 - 11:12 PM

ok i understood ur method
byt really i can't apply it now because i have a code
this is another code i wrote before
just i want the matrix because i didn't know how to do it

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RectangleProgram extends JFrame
{
	private JLabel inputBoxL, outputBoxL, keyBoxL, cipherBoxL;
	private JTextField inputBoxTF, outputBoxTF, keyBoxTF, cipherBoxTF;
	private JButton clearB, exitB, encryptB, decryptB;
	
	private ClearButtonHandler cbHandler;
	private ExitButtonHandler ebHandler;
	private EncryptButtonHandler nbHandler;
	private DecryptButtonHandler dbHandler;
		
	private static final int WIDTH = 400;
	private static final int HEIGHT = 300;
	
	public RectangleProgram()
	{
		inputBoxL = new JLabel("Enter the plain/cipher text:", SwingConstants.LEFT);
		keyBoxL = new JLabel("Enter the keyword:", SwingConstants.LEFT);
		cipherBoxL = new JLabel("CIPHERBOX", SwingConstants.LEFT);
		outputBoxL = new JLabel("The result of Encr/Decr:", SwingConstants.LEFT);
		
		inputBoxTF = new JTextField(10);
		keyBoxTF = new JTextField(10);
		cipherBoxTF = new JTextField(10);
		outputBoxTF = new JTextField(10);
		
		clearB = new JButton("Clear");
		cbHandler  = new ClearButtonHandler();
		clearB.addActionListener(cbHandler);
		
		exitB = new JButton("Exit");
		ebHandler = new ExitButtonHandler();
		exitB.addActionListener(ebHandler);
		
		encryptB = new JButton("Encrypt");
		nbHandler = new EncryptButtonHandler();
		encryptB.addActionListener(nbHandler);
		
		decryptB = new JButton("Decrypt");
		dbHandler = new DecryptButtonHandler();
		decryptB.addActionListener(dbHandler);
		
		setTitle("Playfair Cipher");
		
		Container pane = getContentPane();
		
		pane.setLayout(new GridLayout(6, 2));
		
		pane.add(encryptB);
		pane.add(decryptB);
		pane.add(inputBoxL);
		pane.add(inputBoxTF);
		pane.add(keyBoxL);
		pane.add(keyBoxTF);
		pane.add(cipherBoxL);
		pane.add(cipherBoxTF);
		pane.add(outputBoxL);
		pane.add(outputBoxTF);
		pane.add(clearB);
		pane.add(exitB);
		
		setSize(WIDTH, HEIGHT);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	private class ClearButtonHandler implements ActionListener
	{ 
		public void actionPerformed(ActionEvent e)
		{
			inputBoxTF.setText("");
			outputBoxTF.setText("");
			keyBoxTF.setText("");
			cipherBoxTF.setText("");
		}
	}
	
	private class ExitButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			System.exit(0);
		}
	}
	
	private class EncryptButtonHandler implements ActionListener
	{ 
		public void actionPerformed(ActionEvent e)
		{
			double width,length,area,perimeter;
			
			length = Double.parseDouble(inputBoxTF.getText());
			width = Double.parseDouble(outputBoxTF.getText());
			area = length * width;
			perimeter = 2 * (length + width);
			keyBoxTF.setText(""+area);
			cipherBoxTF.setText("" +perimeter);
		}
	}
	
	private class DecryptButtonHandler implements ActionListener
	{ 
		public void actionPerformed(ActionEvent e)
		{
			double width,length,area,perimeter;
			
			length = Double.parseDouble(inputBoxTF.getText());
			width = Double.parseDouble(outputBoxTF.getText());
			area = length * width;
			perimeter = 2 * (length + width);
			keyBoxTF.setText(""+area);
			cipherBoxTF.setText("" +perimeter);
		}
	}
	
	public static void main(String[] args)
	{
		RectangleProgram rectObject = new RectangleProgram ();
	}
}



soo please help me ..
because i don't have time to submit the assignment
i'll be very thankful :)
and thanks for that code .. but its little bit hard to me

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1