置换加密算法

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class MainFrame extends JFrame{//置换加密算法 private panel1 = null ;

private JPanel panel2 = null ;

private JPanel panel11 = null ;

private JPanel panel12 = null ;

private JPanel panel13 = null ;

private JPanel panel14 = null ;

private JPanel panel15 = null ;

private JPanel panel21 = null ;

private JPanel panel22 = null ;

private JPanel panel23 = null ;

private JPanel panel24 = null ;

private JPanel panel25 = null ;

private JLabel label1 = null ;

private JLabel label2 = null ;

private JLabel label3 = null ;

private JLabel label4 = null ;

private JLabel label5 = null ;

private JLabel label6 = null ;

private JLabel label7 = null ;

private JLabel label8 = null ;

private JButton button1=null ;

private JButton button2=null ;

private JButton button3=null ;

private JButton button4=null ;

private JTextArea ta1 = null ;

private JTextArea ta2 = null ;

private JTextArea ta3 = null ;

private JTextArea ta4 = null ;

private JTextField tf1 = null ;

private JTextField tf2 = null ;

private Container container ;

private String text =null ; //用于明文

private String key1=null ; //用于密钥1

private String cipher =null ; //用于密文 private String key2=null ; //用于密钥2

public MainFrame () {

container = this .getContentPane();

label1=new JLabel(" 置换加密" );

label2=new JLabel(" 置换解密" );

label3=new JLabel(" 明文:" );

label4=new JLabel(" 密钥:" );

label5=new JLabel(" 密文:" );

label6=new JLabel(" 密文:" );

label7=new JLabel(" 密钥:" );

label8=new JLabel(" 明文:" );

button1 = new JButton(" 加密" );

button2 = new JButton(" 清空" );

button3 = new JButton(" 解密" );

button4 = new JButton(" 清空" );

button1.addActionListener(new ActionLis());

button2.addActionListener(new ActionLis()); button3.addActionListener(new ActionLis()); button4.addActionListener(new ActionLis()); ta1= new JTextArea(3, 15); ta2= new JTextArea(3, 15); ta3= new JTextArea("",3, 15); ta4= new JTextArea("",3, 15); ta3.setLineWrap(true ); ta3.setLineWrap(true ); tf1 = new JTextField(10); tf2 = new JTextField(10); panel1 = new JPanel(); panel2 = new JPanel(); panel11 = new JPanel(); panel12 = new JPanel(); panel13 = new JPanel(); panel14 = new JPanel(); panel15 = new JPanel(); panel21 = new JPanel(); panel22 = new JPanel(); panel23 = new JPanel(); panel24 = new JPanel(); panel25 = new JPanel(); panel11.add(label1); panel12.add(label3); panel12.add(ta1); panel13.add(label4); panel13.add(tf1); panel14.add(button1); panel14.add(button2); panel15.add(label5); panel15.add(ta3); panel1.add(panel11); panel1.add(panel12); panel1.add(panel13); panel1.add(panel14); panel1.add(panel15); panel21.add(label2); panel22.add(label6); panel22.add(ta2); panel23.add(label7); panel23.add(tf2); panel24.add(button3); panel24.add(button4); panel25.add(label8); panel25.add(ta4); panel2.add(panel21); panel2.add(panel22); panel2.add(panel23); panel2.add(panel24); panel2.add(panel25); panel1.setLayout(new GridLayout(5, 1)); panel2.setLayout(new GridLayout(5, 1)); container .add(panel1); container .add(panel2); container .setLayout(new GridLayout(1, 2));

} this .setTitle(" 置换加密解密" ); this .setSize(600, 500); this .setVisible(true ); } class ActionLis implements ActionListener{ public void actionPerformed(ActionEvent e) { if (e.getSource() == button1) { text =ta1.getText(); key1=tf1.getText(); Crypt c =new Crypt(text , key1); ta3.setText(c.getcrypt()); } else if (e.getSource() == button2) { ta1.setText("" ); tf1.setText("" ); ta3.setText("" ); } else if (e.getSource() == button3){ cipher =ta2.getText(); key2=tf2.getText(); Decrypt d =new Decrypt(cipher , key2); ta4.setText(d.gettext()); } else { ta2.setText("" ); tf2.setText("" ); ta4.setText("" ); } } } public static void main(String[] args) { new MainFrame(); }

public class Crypt {

private String text , key , ctext , otext ;

private int ctextl , keyl ;

private char [] ch1, ch3, ch4;

private int [] in1;

private char [][] ch2;

private int ch2r ;

public Crypt(String text,String key) {

this . text =text;

this . key =key;

String ctext=text.replaceAll(" ", "" ); //消去明文中空格 ch1=ctext.toCharArray();

ctextl =ctext.length();

keyl =key.length();

if (ctextl %keyl ==0)//设置二维数组行大小

{ch2r =ctextl /keyl ;}

else

{ch2r =ctextl /keyl +1;}

ch2=new char [ch2r ][keyl ];

for (int i=0,j=0;i

{ch2[i][k]=ch1[j];

j++;}}

in1=new int [keyl ];

Keys x=new Keys(key);

in1=x.getkey();

ch4=new char [ch2r *keyl ];//存储输出的字符数组

for (int i=0,k=0;i

{

for (int j=0;j

ch4[k]=ch2[j][in1[i]];

k++;

}

}

otext =String.valueOf (ch4);

}

String getcrypt()

{

return otext ;

}

}

public class Decrypt {

private String cipher , key , otext ;

private int cipherl , keyl ;

private char [] ch1, ch3, ch4;

private char [][] ch2;

private int ch2r ;

private int [] in ;

public Decrypt(String cipher,String key) {

this . cipher =cipher;

this . key =key;

cipherl =cipher.length();

keyl =key.length();

ch1=cipher.toCharArray();

if (cipherl %keyl ==0)//设置二维数组行大小

{ch2r =cipherl /keyl ;}

else

{ch2r =cipherl /keyl +1;}

in =new int [keyl ];

Keys x=new Keys(key);

in =x.getkey();

/*ch3=key.toCharArray();

for(int i=0;i

{

in[i]=Integer.parseInt(String.valueOf(ch3[i])); }*/

ch2=new char [ch2r ][keyl ];

for (int i=0,k=0;i

{

for (int j=0;j

ch2[j][in [i]]=ch1[k];

k++;

}

}

ch4=new char [ch2r *keyl ];

for (int i=0,k=0;i

{

for (int j=0;j

ch4[k]=ch2[i][j];

k++;

}

}

otext =String.valueOf (ch4);

}

String gettext()

{

return otext ;

}

}

public class Keys {

private String key ;

private char [] ch1, ch2;

private int [] in ;

private int keyl ;

public Keys(String key) {

ch1=key.toCharArray();

ch2=key.toCharArray();

keyl =key.length();

in =new int [keyl ];

char temp ;

for (int i=0;i

int k=i;

for (int j=i+1;ji)

{ temp=ch1[i];

ch1[i]=ch1[k];

ch1[k]=temp;}

}

for (int i=0;i

for (int j=0;j

in [i]=j;

else {};}

}

}

int [] getkey()

{

return in ;

}

}

加密

输入明文:we set the time at six aclock 密钥:date

预计结果:etitacwttaxoeeeil

解密

输入密文:etitacwttaxoeeeil

密钥:date

解密预计结果:

wesetthetimeatsixaclock


相关文章

  • 信息安全(1)
  • 1.截获:截获是指一个非授权方介入系统,使得信息在传输过程中泄露或被窃听,它破坏了 信息的保密性.非授权方可以是一个人,也可以是一个程序.截获攻击主要包括:① 利用 电磁泄露或搭线窃听等方式可截获机密信息,通过对信息流向.流量.通信频度和长度等参 数的分析,推测出有用信息,如用户口令.账号等.② 文 ...

  • 西安理工大学实验报告
  • 西安理工大学实验报告用纸 第 页(共 页) 西安理工大学实验报告 课 程: 信息安全 班 级: 计升本101 学 号: 2100912011 指导老师: 吕林涛.王勇超 姓 名: 张亚妮 报告退发: 日 期: 2011-12-09 至 2011-12-16 教师审批签字: 一.实验题目:置换密码技术 ...

  • 密码学概论
  • 密码学基本思想:.............................................................................................................................2 数学基础:.......... ...

  • 网络与信息安全技术考试试题及答案
  • 网络与信息安全技术A卷 一. 单项选择题(每小题2分,共20分) 1.信息安全的基本属性是___. A. 保密性 B.完整性 C. 可用性.可控性.可靠性 D. A,B,C都是 2.假设使用一种加密算法,它的加密方法很简单:将每一个字母加5,即a加密成f.这种算法的密钥就是5,那么它属于___. A ...

  • 计算机密码学习题
  • 一.已知密文ILPQPUN 使用的是移位密码,试解密(提示:明文为有意义的英文). 答:原文: ILPQPUN 移动1位:HKOPOTM 移动2位:GJNONSL 移动3位:FIMNMRK 移动4位:EHLMLQJ 移动5位:DGKLKPI 移动6位:CFJKJOH 移动7位:BEIJING 明文为 ...

  • 安全数据交换技术在HIS中的应用
  • 计 算 机 工 程 第 34 卷 第22期 Vol .34 No.22 Computer Engineering · 安全技术 · 文章编号:1000-3428(2008)22-0195-03 文献标识码:A 2008年11月 November 2008 中图分类号:TP393.08 安全数据交换技 ...

  • 希赛教育软考网络工程师:网络安全技术与信息加密
  • 希赛教育软考网络工程师:网络安全技术与信息加密 随着计算机网络技术的飞速发展,大大改变了人们的生活面貌,促进了社会的发展.互联网是一个面向大众的开放系统,对于信息的保密合系统的安全性考虑得并不完备,由此引起得网络安全问题日益严重.如何保护计算机信息的的内容,也即信息内容的保密问题显得越来越重要[1- ...

  • 计算机网络安全试题答案 自考
  • 全国2009年4月自学考试计算机网络安全试题 一.单项选择题(本大题共15小题,每小题2分,共30分) 1. 拒绝服务攻击是对计算机网络的哪种安全属性的破坏 (C ) A. 保密性 B. 完整性 C. 可用性 D. 不可否认性 2. 在P2DR (PPDR )模型中,作为整个计算机网络系统安全行为准 ...

  • 密码学复习重点
  • 密码学复习重点 第二章:1.安全问题分类和对应手段 2.仿射加密算法 mod 26 y=ax+b 3. 单表替换算法 密文猜明文 第三章1.DES (1)Feistel 体制 (2) S 盒 2.DES .AES 与RC4对比:宏观,内部了解:DES .AES 替代置换网络 RC4流算法 替换 56 ...

© 2024 范文中心 | 联系我们 webmaster# onjobs.com.cn