其实公式已经有了,真的很简单的,我帮你实现一下吧
package cn.denghui.encrypt;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Jiami extends JFrame implements ActionListener
{
JPanel p_north, p_center, p_south, panel1, panel2, panel3;
JTextField jtf_north, jtf_center, jtf_south;
JButton b1, b2;
public Jiami()
{
p_north = new JPanel();
p_center = new JPanel();
p_south = new JPanel();
jtf_north = new JTextField(10);
jtf_center = new JTextField(10);
jtf_south = new JTextField(10);
b1 = new JButton("加密");
b1.addActionListener(this);
b2 = new JButton("退出");
b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(1);
}
});
p_north.add(new JLabel("明文:"));
p_north.add(jtf_north);
p_center.add(new JLabel("密钥:"));
p_center.add(jtf_center);
p_center.add(b1);
p_center.add(b2);
p_south.add(new JLabel("密文:"));
p_south.add(jtf_south);
this.add(p_north, BorderLayout.NORTH);
this.add(p_center, BorderLayout.CENTER);
this.add(p_south, BorderLayout.SOUTH);
this.setSize(200, 170);
this.setResizable(false);
this.setLocation(200, 200);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(b1))
{
encrypt(); //点击加密按扭进行加密
}
}
//---------------------------------------以下-------------
public void encrypt()
{
//获取字段中的字据
String str = jtf_north.getText();
int key =0;
try
{
key = Integer.parseInt(jtf_center.getText()); //由于这句可能转换异常
}catch(NumberFormatException e){
System.out.println("类型转换异常");
}
StringBuffer buf = new StringBuffer();
//依次将字符串的字符依据key转成密文
for(char ch:str.toCharArray()){
ch=(char)((ch-'a'+key)%26+'a');
buf.append(ch);
}
jtf_south.setText(buf.toString());
}
//---------------------------------------以上-------------
public static void main(String[] args)
{
new Jiami();
}
}
/*
至于解密,因为你这公式密文与明文不是一对一的关系(比如明文1和K 密钥1 都是对应L这个密文),所以一个密文可能对应多个明文,具体的公式----->ch = (char)((ch-'a')+26*n -1+'a'); 或 ch = (char)((ch-'a')-26*n -1+'a'); 其中n未知(1,2,3..),所以我猜你们老师没叫你们写解密的吧
恺撒密码的加密与解密的一种简易模式 加密ch=ch+key 解密:ch=ch-key
*/
=========================异或实现对称加解密===============================
package cn.denghui.encrypt;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Jiami extends JFrame implements ActionListener
{
JPanel p_north, p_center, p_south, panel1, panel2, panel3;
JTextField jtf_north, jtf_center, jtf_south;
JButton b1, b2;
public Jiami()
{
p_north = new JPanel();
p_center = new JPanel();
p_south = new JPanel();
jtf_north = new JTextField(10);
jtf_center = new JTextField(10);
jtf_south = new JTextField(10);
b1 = new JButton("加密解密");
b1.addActionListener(this);
b2 = new JButton("退出");
b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(1);
}
});
p_north.add(new JLabel("明文:"));
p_north.add(jtf_north);
p_center.add(new JLabel("密钥:"));
p_center.add(jtf_center);
p_center.add(b1);
p_center.add(b2);
p_south.add(new JLabel("密文:"));
p_south.add(jtf_south);
this.add(p_north, BorderLayout.NORTH);
this.add(p_center, BorderLayout.CENTER);
this.add(p_south, BorderLayout.SOUTH);
this.setSize(200, 170);
this.setResizable(false);
this.setLocation(200, 200);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(b1))
{
String str = encrypt(jtf_north.getText(),jtf_center.getText()); //点击加密按扭进行加密
jtf_south.setText(str);
}
}
//---------------------------------------以下-------------
//简单字符串对称加密解密
public static String encrypt(String data,String key)
{
byte[] bs = data.getBytes(); //将字符串按系统默认编码GBK编码成字节码
int b = Integer.parseInt(key); //将字符串转换成数字
b = b%127; //过滤超过ASCII表范围的字符
for (int i = 0; i < bs.length; i++)
{
bs[i]=(byte) (bs[i]^b); //关键地方:将每个字节与密钥异或,生成对应的密文字节
//比如 4^16^16 = 4;
}
return new String(bs);
}
//---------------------------------------以上-------------
public static void main(String[] args)
{
new Jiami();
}
}