信息安全技术-(java实现)凯撒密码及仿射密码的加密解密

仿射密码和凯撒密码用的是同一个界面,所以重复的地方比较多,所以凯撒密码只贴出逻辑部分的代码

凯撒密码:

逻辑代码(加密解密函数):


	/**
	 * 明文进行加密
	 * 
	 * @param word 明文
	 * @param x    位移的长度
	 * @return 密文
	 */
	public String EncryWord(String word, int x) {
		StringBuilder sb = new StringBuilder();
		char[] charArray = word.toCharArray();
		for (int i = 0; i < charArray.length; i++) {
			if (charArray[i] >= 'a' && charArray[i] <= 'z') {
				int f=(charArray[i]-'a'+x)%26+'a';
				char a=(char)f;
				sb.append(a);
			} else {
				int f=(charArray[i]-'A'+x)%26+'A';
				char a=(char)f;
				sb.append(a);			
			}
		}
		return sb.toString();
	}

	/**
	 * 密文进行解密
	 */
	public static String DecryWord(String word, int x) {
		StringBuilder sb = new StringBuilder();
		char[] charArray = word.toCharArray();
		for (int i = 0; i < charArray.length; i++) {
			if (charArray[i] >= 'a' && charArray[i] <= 'z') {
				int f=(charArray[i]-'a'+26-x)%26+'a';
				char a=(char)f;
				sb.append(a);
			} else {
				int f=(charArray[i]-'A'+26-x)%26+'A';
				char a=(char)f;
				sb.append(a);
			}
		}
		return sb.toString();
	}
}

 

 

 仿射密码:

package top.clearlight.cipher;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Swing extends JFrame {
	/**
	 * 版本序列号
	 */
	private static final long serialVersionUID = 1L;

	public static int flag = 0; // 按钮的选择标志

	JPanel jp1, jp2, jp3, jp4, jp5;
	JLabel jlb1;
	JButton jb1, jb2, jb3, jb4;
	JTextField jtf1, jtf3, jtf4;
	JTextArea jta = null;

	public static void main(String[] args) {
		new Swing();
	}

	public Swing() {
		jp1 = new JPanel();
		jp2 = new JPanel();
		jp3 = new JPanel();
		jp4 = new JPanel();
		jp5 = new JPanel();

		jlb1 = new JLabel("明文");

		jb1 = new JButton("加密");
		jb2 = new JButton("解密");

		jtf1 = new JTextField(10);


		jb3 = new JButton("生成");
		jb4 = new JButton("重置");

		jta = new JTextArea(3, 13);
		jta.setLineWrap(true); // 自动换行
		jta.setBackground(Color.LIGHT_GRAY);

		this.setLayout(new GridLayout(5, 1)); // 网格布局,5行一列

		jp1.add(jlb1);
		jp1.add(jtf1);


		jp3.add(jb1);
		jp3.add(jb2);

		jp4.add(jb4);

		jp4.add(jb3);

		jp5.add(jta);

		this.add(jp3);
		this.add(jp1);
		this.add(jp2);
		this.add(jp4);
		this.add(jp5);

		this.setSize(290, 243); // 设置窗口大小

		int windowWidth = this.getWidth(); // 获得窗口宽
		int windowHeight = this.getHeight(); // 获得窗口高
		Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包
		Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸
		int screenWidth = screenSize.width; // 获取屏幕的宽
		int screenHeight = screenSize.height; // 获取屏幕的高
		this.setLocation(screenWidth / 2 - windowWidth / 2, screenHeight / 2 - windowHeight / 2);// 设置窗口居中显示

		this.setTitle("凯撒密码");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
//		UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); // 设置主题
		this.setResizable(false); // 窗口大小不可变

		listenerButton();
	}

	/**
	 * 按钮监听
	 */
	public void listenerButton() {

		jb1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				flag = 0;
				jlb1.setText("明文");
				jtf1.setText("");
				jta.setText("");
			}
		});

		jb2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				flag = 1;
				jlb1.setText("密文");

				if (jta.getText().length() != 0) {
					jtf1.setText(jta.getText());
				}
				jta.setText("");
			}
		});

		jb3.addActionListener(new ActionListener() {
			private Component parent;

			@Override
			public void actionPerformed(ActionEvent e) {
				String word = jtf1.getText();

				if (word.length() == 0) {
					JOptionPane.showMessageDialog(parent, "请输入" + jlb1.getText());
					return;
				}

				for (int i = 0; i < word.length(); i++) {
					char ch = word.charAt(i);
					if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))) {
//						System.out.println("非法字符 , 请重新输入");
						JOptionPane.showMessageDialog(parent, "非法字符 , 请重新输入!");
						jtf1.setText("");
						return;
					}
				}

				try {
					
					int x = 5;

					

					String s = flag == 0 ? EncryWord(word, x) : DecryWord(word, x);
					jta.setText(s);

					SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
					sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");// a为am/pm的标记
					Date date = new Date();// 获取当前时间

					String str = flag == 0 ? "加密 = " : "解密 = ";

					String path = System.getProperty("user.dir"); // 获取文件的存储路径
					
					FileWriter fw = new FileWriter("encrypt.txt", true);
					fw.write(sdf.format(date) + "\r\n");
					fw.write(jlb1.getText() + " = " + word + "\r\n");
					fw.write("Key  = " + x + "\r\n");
					fw.write(str + s + "\r\n\r\n");
					fw.close();
					JOptionPane.showMessageDialog(parent, "已保存到" + path);
					System.out.println(s);
				} catch (Exception m) {
					JOptionPane.showMessageDialog(parent, "Key只限数字");
					
				}
			}
		});

		jb4.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				jtf1.setText("");
				jta.setText("");
			}
		});
	}

	/**
	 * 明文进行加密
	 * 
	 * @param word 明文
	 * @param x    5
	 * @return 密文
	 */
	public String EncryWord(String word, int x) {//加密没有什么技术含量,直接套公式就行了
		StringBuilder sb = new StringBuilder();
		char[] charArray = word.toCharArray();
		for (int i = 0; i < charArray.length; i++) {
			if (charArray[i] >= 'a' && charArray[i] <= 'z') {
				int f=(3*(charArray[i]-'a')+x)%26+'a';
				char a=(char)f;
				sb.append(a);
			} else {
				int f=(3*(charArray[i]-'A')+x)%26+'A';
				char a=(char)f;
				sb.append(a);			
			}
		}
		return sb.toString();
	}

	/**
	 * 密文进行解密
	 */
	public static String DecryWord(String word, int x) {
//解密比加密稍微复杂一点,所以用了循环来解决加多少个26的问题
		StringBuilder sb = new StringBuilder();
		char[] charArray = word.toCharArray();
		for (int i = 0; i < charArray.length; i++) {
			if (charArray[i] >= 'a' && charArray[i] <= 'z') {
				double f;
				for(int k=0; ;++k){
					f=(charArray[i]-'a'+k*26-x)/3.0;
					if(f<0)//当要解密的字符是c的时候,会出现特殊情况,即f=-1.0,是整数,但是最后得到的字符不是字母,所以处理一下 
						continue;
					int f2=(charArray[i]-'a'+k*26-x)/3;
					if(f==(double)f2)
						break;
				}
				char a=(char)(f%26+'a');
				sb.append(a);
			} else {
				double f;
				for(int k=0; ;++k){
					f=(charArray[i]-'A'+k*26-x)/3.0;
					if(f<0)
						continue;
					int f2=(charArray[i]-'A'+k*26-x)/3;
					if(f==(double)f2)
						break;
				}
				char a=(char)(f%26+'A');
				sb.append(a);}
		}
		return sb.toString();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值