恺撒密码&仿射密码

恺撒密码
原理:明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。
在这里插入图片描述
源码如下

package com.mima.method;

import java.awt.Dimension;
import java.awt.Toolkit;
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.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 * 凯撒密码
 * @author jpp
 * 2019年2月27日下午12:51:39
 */
public class Kaisa {
	//秘钥字母表
	final static String[] CHARACTER = {
		"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"		
	};
	//秘钥字母表长度
	final static int N = 26;
	
	/**
	 * 凯撒加密算法
	 * @param plaintext 明文
	 * @param k 秘钥
	 * @return mi 密文
	 */
	public static String encryptionKaisa(String plaintext , int k){
		
		StringBuilder mi = new StringBuilder();
		int t,s;
		
		//对明文的每个字符逐个处理,进行加密

		char[] ming=plaintext.toCharArray();
		for(int i=0;i<ming.length;i++){

			//获取该字节在字母表中的序号
			t=ming[i]-97;
			
			//防止越界
			if (t<0 || t>N) {
				mi.append(ming[i]);
				continue;
				}
			
			//加密
			s=(t+k)%N;
			
			mi.append(CHARACTER[s]);
		}
		
		return mi.toString();
	}
	
	/**
	 * 凯撒密码解密方法 (需输入正确的秘钥k才能解密出正确的明文)
	 * @param cipher 密文
	 * @param k 秘钥
	 * @return
	 */
	public static String decryptionKaisa(String cipher,int k) {
		
		StringBuilder ming = new StringBuilder();
		int t,s;
		
		//对明文的每个字符逐个处理,进行加密
		char[] mi=cipher.toCharArray();
		for(int i=0;i<mi.length;i++){

			//获取该字节在字母表中的序号
			t=mi[i]-97;
			//防止越界
			if (t<0 || t>N) {
				ming.append(mi[i]);
				continue;
			}
			
			//解密
			if (t>=k) {
				s=t-k;
			} else {
				s=N+t-k;
			}
			ming.append(CHARACTER[s]);
		}
		return ming.toString();
	}
	
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		// TODO Auto-generated method stub
		String ming = "security";
		System.out.println("明文:"+ming);
		String mi=encryptionKaisa(ming,3);
		System.out.println("密文:"+mi);
		System.out.println("解密后:"+decryptionKaisa(mi,3));
		
		KaisaView kaisaTestView = new KaisaView();
	}
}

class KaisaView extends JFrame{

	JTextField jtfming;//明文
	JTextField jtfmiyao;//秘钥
	JTextField jtfmi;//密文
	JTextField jtfjiemi;//解密
	JButton jbt_jia;//����
	JButton jbt_jie;//����
	String mi_res;
	String ming_res;
	
	public KaisaView() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int x = (screenSize.width - 485) / 2;
		int y = (screenSize.height - 380) / 2;
		setTitle("凯撒加解密测试");
		setBounds(x, y, 485, 200);//���ڴ�С
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLayout(null);
		
		jbt_jia = new JButton("加密");
		jbt_jia.setBounds(400, 80, 60, 25);
		jbt_jia.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				mi_res= Kaisa.encryptionKaisa(jtfming.getText(), Integer.valueOf(jtfmiyao.getText()));
				System.out.println(mi_res);
				jtfmi.setText(mi_res);
			}
		});
		
		JLabel jl=new JLabel("明文:");
		jl.setBounds(10,5,80,20);
		jtfming=new JTextField("security");
		jtfming.setBounds(100,5,360,25);
		
		JLabel j2=new JLabel("密钥:");
		j2.setBounds(10,30,80,20);
		jtfmiyao=new JTextField("3");
		jtfmiyao.setBounds(100,30,360,25);
		
		jbt_jie = new JButton("解密");
		jbt_jie.setBounds(400, 130, 60, 25);
		jbt_jie.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
				ming_res = Kaisa.decryptionKaisa(jtfmi.getText(), Integer.valueOf(jtfmiyao.getText()));
				jtfjiemi.setText(ming_res);
			}
		});
		
		JLabel jl1=new JLabel("加密后↓↓↓");
		jl1.setBounds(10,55,100,20);
		jtfmi=new JTextField("");
		jtfmi.setBounds(10,80,360,25);

		JLabel jl2=new JLabel("解密后↓↓↓");
		jl2.setBounds(10,105,100,20);
		jtfjiemi=new JTextField("");
		jtfjiemi.setBounds(10,130,360,25);
		
		add(jbt_jia);
		add(jbt_jie);
		add(jl);
		add(j2);
		add(jl1);
		add(jl2);
		add(jtfming);
		add(jtfmi);
		add(jtfmiyao);
		add(jtfjiemi);
		setVisible(true);
	}
}

仿射密码
加密函数:s=(3*t+5)%N
解密函数:s=((t-5+26)*9)%N
在这里插入图片描述

package com.mima.method;

import java.awt.Dimension;
import java.awt.Toolkit;
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.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class Fangshe {

	//秘钥字母表
	final static String[] CHARACTER = {
		"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"		
	};
	//秘钥字母表长度
	final static int N = 26;
	
	public static String encryptionFangshe(String plaintest){
		
		StringBuilder mi = new StringBuilder();
		int t,s;
		
		//对明文的每个字符逐个处理,进行加密

		char[] ming=plaintest.toCharArray();
		for(int i=0;i<ming.length;i++){

			//获取该字节在字母表中的序号
			t=ming[i]-97;//明文编号
			
			//防止越界
			if (t<0 || t>N) {
				mi.append(ming[i]);
				continue;
				}
			
			//加密
			s=(3*t+5)%N;//密文编号
			
			mi.append(CHARACTER[s]);
		}
		
		return mi.toString();
	}
	
	
	/**
	 * Fangshe密码解密函数
	 * @param plaintest 密文
	 * @return
	 */
	public static String decryptionFangshe(String plaintest){
		
		StringBuilder mi = new StringBuilder();
		int t,s;
		
		//对明文的每个字符逐个处理,进行加密

		char[] ming=plaintest.toCharArray();
		for(int i=0;i<ming.length;i++){

			//获取该字节在字母表中的序号
			t=ming[i]-97;//明文编号
			
			//防止越界
			if (t<0 || t>N) {
				mi.append(ming[i]);
				continue;
				}
			
			//解密
			s=((t-5+26)*9)%N;//密文编号
			mi.append(CHARACTER[s]);
		}
		
		return mi.toString();
	}
	
	
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		// TODO Auto-generated method stub
		String ming = "security";
		System.out.println("明文:"+ming);
		String mi=encryptionFangshe(ming);
		System.out.println("密文:"+mi);
		System.out.println("解密后:"+decryptionFangshe(mi));
		
		FangsheView fangsheView = new FangsheView();
	}
}

class FangsheView extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JTextField jtfming;//明文
	JTextField jtfmiyao;//密钥
	JTextField jtfmi;//密文
	JTextField jtfjiemi;//解密
	JButton jbt_jia;//加密
	JButton jbt_jie;//解密
	String mi_res;
	String ming_res;
	
	public FangsheView() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int x = (screenSize.width - 485) / 2;
		int y = (screenSize.height) / 2;
		setTitle("仿射算法加解密测试");
		setBounds(x, y, 485, 200);//窗口大小
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLayout(null);
		
		jbt_jia = new JButton("加密");
		jbt_jia.setBounds(400, 80, 60, 25);
		jbt_jia.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				mi_res= Fangshe.encryptionFangshe(jtfming.getText());
				System.out.println(mi_res);
				jtfmi.setText(mi_res);
			}
		});
		
		JLabel jl=new JLabel("请输入明文:");
		jl.setBounds(10,5,80,20);
		jtfming=new JTextField("security");
		jtfming.setBounds(100,5,360,25);
		
//		JLabel j2=new JLabel("请输入秘钥:");
//		j2.setBounds(10,30,80,20);
//		jtfmiyao=new JTextField("3");
//		jtfmiyao.setBounds(100,30,360,25);
		
		jbt_jie = new JButton("解密");
		jbt_jie.setBounds(400, 130, 60, 25);
		jbt_jie.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
				ming_res = Fangshe.decryptionFangshe(jtfmi.getText());
				jtfjiemi.setText(ming_res);
			}
		});
		
		JLabel jl1=new JLabel("密文如下:");
		jl1.setBounds(10,55,100,20);
		jtfmi=new JTextField("");
		jtfmi.setBounds(10,80,360,25);

		JLabel jl2=new JLabel("解密结果:");
		jl2.setBounds(10,105,100,20);
		jtfjiemi=new JTextField("");
		jtfjiemi.setBounds(10,130,360,25);
		
		add(jbt_jia);
		add(jbt_jie);
		add(jl);
	//	add(j2);
		add(jl1);
		add(jl2);
		add(jtfming);
		add(jtfmi);
	//	add(jtfmiyao);
		add(jtfjiemi);
		setVisible(true);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值