Wed Mar 19, 2008 12:55 am
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;
public class PolyTransMain{
/** Creates a new instance of DesMain */
public PolyTransMain() {
}
public static void main(String [] rr){
DesPanel mm = new DesPanel();
mm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mm.setVisible(true);
}
}
class DesPanel extends JFrame{
public DesPanel(){
this.setTitle("PolyTrans");
this.setSize(800,800);
this.setVisible(true);
GenerateGUI();
}
PolyTrans p;
public static JTextArea StepsText = new JTextArea(10,40);
private Container c=this.getContentPane();
private JButton btnCihper = new JButton("Cipher");
private JButton btnDeCihper = new JButton("Decipher");
private JTextArea CipherText = new JTextArea(4,40);
private JTextArea OriginalText = new JTextArea(4,40);
private JTextArea DeCipherText = new JTextArea(4,40);
private JScrollPane OriginalScorl=new JScrollPane(OriginalText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private JScrollPane CipherScorl=new JScrollPane(CipherText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private JScrollPane DecipherScorl=new JScrollPane(DeCipherText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private JScrollPane StepScorl=new JScrollPane(StepsText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private JTextField KeyWord = new JTextField(16);
private JTextField KeyWordTwo = new JTextField(16);
private JPanel PanelCipher = new JPanel();
private JPanel PanelDecipher = new JPanel();
private JPanel PanelKeyWord = new JPanel();
private JPanel PanelOriginaltxt = new JPanel();
private JPanel jpstep=new JPanel();
private JLabel lblKeyWord= new JLabel("KeyWord : ");
private void GenerateGUI(){
c.setLayout(new GridLayout(4,1));
c.setLayout(new FlowLayout());
PanelKeyWord.setLayout(new FlowLayout());
PanelKeyWord.add(lblKeyWord);
PanelKeyWord.add(KeyWord);
PanelOriginaltxt.setBorder(BorderFactory.createTitledBorder("Original Text"));
PanelOriginaltxt.setLayout(new FlowLayout());
PanelOriginaltxt.add(OriginalScorl);
PanelCipher.setBorder(BorderFactory.createTitledBorder("Cipher Text"));
PanelCipher.setLayout(new FlowLayout());
PanelCipher.add(CipherScorl);
PanelCipher.add(btnCihper);
PanelDecipher.setBorder(BorderFactory.createTitledBorder("Decipher Text"));
PanelDecipher.setLayout(new FlowLayout());
PanelDecipher.add(DecipherScorl);
PanelDecipher.add(btnDeCihper);
jpstep.setBorder(BorderFactory.createTitledBorder("Mointor"));
jpstep.setLayout(new FlowLayout());
jpstep.add(StepScorl);
c.add(PanelKeyWord);
c.add(PanelOriginaltxt);
c.add(PanelCipher);
c.add(PanelDecipher);
c.add(jpstep);
DoActions();
}
private void DoActions(){
ActionHandler action = new ActionHandler();
btnCihper.addActionListener(action);
btnDeCihper.addActionListener(action);
}
private class ActionHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnCihper){
String plaintext=OriginalText.getText();
String keyworD=KeyWord.getText();
StepsText.append("keyword : "+keyworD+'\n');
StepsText.append("PlainText : "+plaintext+'\n');
p= new PolyTrans(keyworD,plaintext);
p.Encrypt();
p.FillCharacMatrix();
CipherText.append(p.getCipher());
}
if(e.getSource()==btnDeCihper){
p.Decrypt();
DeCipherText.append("\n"+p.getDeCipher());
}
}
}
private String fina;
}
class DocumentSizeFilter extends DocumentFilter {
int maxCharacters;
public DocumentSizeFilter(int maxChars) {
maxCharacters = maxChars;
}
public void insertString(FilterBypass fb, int offs, String str, AttributeSet a)
throws BadLocationException {
if ((fb.getDocument().getLength() + str.length()) <= maxCharacters)
super.insertString(fb, offs, str, a);
else
Toolkit.getDefaultToolkit().beep();
}
public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)
throws BadLocationException {
if ((fb.getDocument().getLength() + str.length()- length) <= maxCharacters)
super.replace(fb, offs, length, str, a);
else
Toolkit.getDefaultToolkit().beep();
}
}
import java.util.Scanner;
public class PolyTrans {
/** Creates a new instance of PolyTrans */
public PolyTrans(String key, String plain) {
keyword=key;
PlainText=plain;
}
public void UserEntery() {
Encrypt();
FillCharacMatrix();
Decrypt();
}
public String getCipher(){
return cyphir;
}
public String getDeCipher(){
return dcyphir;
}
public void FillCharacMatrix(){
for(int i=0; i<=25; i++){
for(int j=0; j<=25; j++){
CharMatrix[i][j]=characters[(j+i)%26];
}
}
for(int i=0;i<=25;i++) {
System.out.print(" | ");
for(int j=0;j<=25;j++) {
System.out.print(" "+CharMatrix[i][j]+" ");
}
System.out.println(" | ");
}
}
public void Encrypt() {
int sub1=0,sub2=0,sub3=0;
int sub=0;
char char_num;
int start=0;
for(int i=0; i<PlainText.length(); i++) {
sub1=PlainText.substring(i,i+1).codePointAt(0);
sub2=keyword.substring(start,start+1).codePointAt(0);
start++;
sub3=sub1+sub2;
sub3=sub3%97;
sub=sub3%26;
if(cyphir==null)
cyphir=""+characters[sub];
else
cyphir+=characters[sub];
if(start==keyword.length()){
start=0;
}
}
System.out.println("The Cipher Text is: "+ cyphir);
}
public void Decrypt(){
int sub1=0,sub2=0,sub3=0;
int keyword_len;
int sub=0;
int cyphir_len;
char char_num;
int start=0;
for(int i=0; i<PlainText.length(); i++) {
sub1=cyphir.substring(i,i+1).codePointAt(0);
sub2=keyword.substring(start,start+1).codePointAt(0);
start++;
sub1=(sub1%97)%26;
sub2=(sub2%97)%26;
for(int k=0; k<26; k++) {
sub3=(Integer.parseInt(""+(short)CharMatrix[sub2][k])%97)%26;
if(sub1==sub3){
if(dcyphir==null)
dcyphir=Character.toString(CharMatrix[0][k]);
else
dcyphir+=Character.toString(CharMatrix[0][k]);
}
}
if(start==keyword.length()){
start=0;
}
}
System.out.println("The Dciphir Text is: "+ dcyphir);
}
private int Len=0;
private String keyword;
private String PlainText;
private String cyphir;
private String [][] charmatriex;
private String matrixv;
private char [][] CharMatrix=new char[26][26];
private char characters[]= {'a','b','c','d','e','f','g','h',
'i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
private String dcyphir;
}
Mon Jan 21, 2013 1:10 am
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.