Javaでアフィニティパスワード、vigenereとHillパスワードを実現して、ユークリッドアルゴリズムで1つの数の26に対する逆を実現することを含みます


この2,3日から情报セキュリティの暗号学の少しの知识を学んで、接触し始めたのは模写のパスワードで、vigenereとHillのパスワード、この3つのパスワードはとっくに淘汰されましたが、しかしやはり自分で书いてみたいと思って、Javaで书いて、コードはとても简単で、みんなは见てみることができます
       シミュレーションパスワード:
package vigenere;

import java.util.Scanner;

public class fangshe {

	private static final int number = 26;
	/*
	 *                  ,                 26  ,
	 * encrypt      ,deciphering      , Euclid                 26   
	 */

	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		System.out.println("please input the message(please input the lowercase)");
		String s = input.nextLine();//     ,       ,     
		System.out.println("please input the a");
		int a = input.nextInt();//          a
		System.out.println("please input the b");
		int b = input.nextInt();//          b
		String ch2 = encrypt(s, a, b);//       ,    
		System.out.println(ch2);
		// deciphering
		int a2 = Euclid(a, number);//           a  。   a2;
		System.out.println("a2=" + a2);
		String ch3 = deciphering(ch2, a2, b);//       ,    
		System.out.println("ch3=" + ch3);

	}

	public static String encrypt(String s, int a, int b) {//        
		char[] ch = s.toCharArray();
		int length = ch.length;//     
		int[] in = new int[length];
		for (int i = 0; i < ch.length; i++) {
			in[i] = ch[i] - 97;//   ascii  0-25  
		}
		for (int i = 0; i < length; i++) {
			in[i] = (in[i] * a + b) % 26;//     
		}
		for (int i = 0; i < ch.length; i++) {
			ch[i] = (char) (in[i] + 97);//        
		}
		return String.valueOf(ch);//         String      ,  

	}

	public static int Euclid(int a, int munber) {//  a      
		int a1 = 1, a2 = 0, a3 = number;
		int b1 = 0, b2 = 1, b3 = a;
		int t1, t2, t3;
		int q;
		q = a3 / b3;
		System.out.println("q=" + q);
		t1 = a1 - q * b1;
		t2 = a2 - q * b2;
		t3 = a3 - q * b3;
		while (t3 != 1) {
			a1 = b1;
			a2 = b2;
			a3 = b3;
			b1 = t1;
			b2 = t2;
			b3 = t3;
			q = a3 / b3;
			t1 = a1 - q * b1;
			t2 = a2 - q * b2;
			t3 = a3 - q * b3;
		}
		int a4 = t2 % munber;
		if (a4 < 0)
			a4 += munber;//   a4  0,   26  
		return a4;//   a  
	}

	public static String deciphering(String s, int a, int b) {//      
		char[] ch = s.toCharArray();
		int length = ch.length;//     
		int[] in = new int[length];
		for (int i = 0; i < ch.length; i++) {
			in[i] = ch[i] - 97;//   ascii  0-25  
		}
		for (int i = 0; i < length; i++) {
			in[i] = ((in[i] - b) * a) % 26;//     
			if (in[i] < 0)
				in[i] += 26;
		}
		for (int i = 0; i < ch.length; i++) {
			ch[i] = (char) (in[i] + 97);//        
		}
		return String.valueOf(ch);//         String      ,  
	}

}
 vigenereパスワード
package vigenere;

import java.util.Scanner;

public class vigenere {
	/*
	 *     vigenere           
	 * encrypt      ,deciphering      
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		System.out.println("please input the message(please input the lowercase)");
		String s = input.nextLine();//     ,       ,     
		System.out.println("please input the Encryptionkey.");
		String s2 = input.nextLine();//     ,       ,     
		String s3 = encrypt(s, s2);//      ,    
		System.out.println(s3);
		String s4=deciphering(s3,s2);//      ,    
		System.out.println(s4);
	}

	public static String encrypt(String s1, String s2) {
		char[] ch1 = s1.toCharArray();
		int length = ch1.length;//     
		int[] in1 = new int[length];
		for (int i = 0; i < ch1.length; i++) {
			in1[i] = ch1[i] - 97;//   ascii  0-25  
		}
		char[] ch2 = s2.toCharArray();
		int length2 = ch2.length;//     
		int[] in2 = new int[length2];
		for (int i = 0; i < ch2.length; i++) {
			in2[i] = ch2[i] - 97;//   ascii  0-25  
		}
		int j = 0;
		for (int i = 0; i < length; i++) {
			if (j == length2)
				j = 0;
			in1[i] = (in1[i] + in2[j]) % 26;//    ,mod26
			j++;
		}
		for (int i = 0; i < length; i++) {
			ch1[i] = (char) (in1[i] + 97);//        
		}
		return String.valueOf(ch1);//         String      ,  
	}

	public static String deciphering(String s1, String s2) {
		char[] ch1 = s1.toCharArray();
		int length = ch1.length;//     
		int[] in1 = new int[length];
		for (int i = 0; i < ch1.length; i++) {
			in1[i] = ch1[i] - 97;//   ascii  0-25  
		}
		char[] ch2 = s2.toCharArray();
		int length2 = ch2.length;//     
		int[] in2 = new int[length2];
		for (int i = 0; i < ch2.length; i++) {
			in2[i] = ch2[i] - 97;//   ascii  0-25  
		}
		int j = 0;
		for (int i = 0; i < length; i++) {
			if (j == length2)
				j = 0;
			in1[i] = (in1[i] - in2[j]) % 26;//    ,mod26
			if (in1[i] < 0)
				in1[i] += 26;//      ,   26  
			j++;
		}
		for (int i = 0; i < length; i++) {
			ch1[i] = (char) (in1[i] + 97);//        
		}
		return String.valueOf(ch1);//         String      ,  
	}
}
Hillパスワード:
package vigenere;

import java.util.Scanner;

public class hill {
	private static final int number = 2;
	/*
	 *     Hill                       ,encrypt           
	 *       ,             2*2 
	 */

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		System.out.println(
				"please input the message(please input the lowercase and the length of maessage is the multiple of the 2 )");
		String s = input.nextLine();//     ,       ,    2    ,     
		System.out.println("please input the Encryptionkey  matrix");//      ,2*2  
		int[][] in = new int[2][2];
		int[][] in2 = new int[2][2];
		in[0][0] = input.nextInt();
		in[0][1] = input.nextInt();
		in[1][0] = input.nextInt();
		in[1][1] = input.nextInt();
		String s1 = encrypt(s, in);//     
		System.out.println(s1);
		//       
		in2[0][0] = in[1][1];
		in2[0][1] = -in[0][1];
		in2[1][0] = -in[1][0];
		in2[1][1] = in[0][0];
		String s2 = encrypt(s1, in2);//     
		System.out.println(s2);
	}

	public static String encrypt(String s, int[][] a) {//              
		char[] ch = s.toCharArray();
		int length = ch.length;//     
		int[] in = new int[length];
		int[] in1 = new int[length];
		for (int i = 0; i < ch.length; i++) {
			in[i] = ch[i] - 97;//   ascii  0-25  
		}
		for (int i = 0; i < length; i = i + 2) {//      
			in1[i] = (in[i] * a[0][0] + in[i + 1] * a[1][0]) % 26;
			in1[i + 1] = (in[i] * a[0][1] + in[i + 1] * a[1][1]) % 26;
			if (in1[i] < 0)
				in1[i] += 26;
			if (in1[i + 1] < 0)
				in1[i + 1] += 26;
		}
		for (int i = 0; i < ch.length; i++) {
			ch[i] = (char) (in1[i] + 97);//        
		}
		return String.valueOf(ch);//         String      ,  
	}
}