多表代换密码首先将明文M 分为由n 个字母组成的分组, , … ,对每个分组的加密为
≡ + ( ), = , , …
其中,(A,B)是密钥,A 是 × 的可逆矩阵,满足gcd(|A|,N)=1,( |A|是A 的行列式), = (, , … ), =
(, , … ), = (, , … ),对密文的解密为
≡ −( − )( ), = , , …
java实现多表代换加密
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; public class Mangboot { private static Map<Integer, String> maps = new HashMap<Integer,String>(); public static int getKey(Map map,String value){ int key = 0; Iterator it = map.entrySet().iterator(); while(it.hasNext()){ Map.Entry entry = (Entry) it.next(); Object obj = entry.getValue(); if(obj!=null&&obj.equals(value)){ key=(int)entry.getKey(); } } return key; } /** * 对字符和数字进行一一映射 */ public static void fuzhi(){ String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for(int i=0;i<str.length();i++){ maps.put(i, str.substring(i,i+1)); } } /** * 加密的主要算法 * @param a 输入的A矩阵(密钥) * @param d 需要加密的密文转换后的矩阵 * @param c 保存加密后的结果 * @param b 矩阵B(密钥) * @param n 矩阵的维数 */ public static void addmatrixMultiply(int[][]a,int [][]d,int [][]c,int b[][],int n){ for(int i=0;i<n;i++) for(int j = 0;j <1;j++){ int sum = a[i][0]*d[0][j]; for(int k = 1;k<n;k++) sum +=a[i][k]*d[k][j]; c[i][j] = sum+b[i][j]; } } public static void main(String[] args) { fuzhi(); Scanner scanner = new Scanner(System.in); System.out.println("请输入你要加密的明文:"); String str = scanner.nextLine(); System.out.println("请输入密钥"); System.out.println("请输入A矩阵的维数:"); int n = scanner.nextInt(); System.out.println("开始创建A矩阵:"); int a[][] = new int[n][n]; for(int i =0;i<n;i++){ for(int j= 0;j<n;j++){ a[i][j] = scanner.nextInt(); } } System.out.println("开始创建B矩阵:"); int b[][] = new int[n][1]; int c[][] = new int[n][1]; int d[][] = new int[n][1]; for(int j= 0;j<n;j++){ b[j][0] = scanner.nextInt(); } System.out.println("获得的密文为:"); for(int i = 0;i<str.length()/n;i++){ String sub = str.substring(i*n, (i+1)*n); for(int j= 0;j<n;j++){ d[j][0] = getKey(maps, sub.substring(j, j+1)); } addmatrixMultiply(a, d, c,b, n); for(int j= 0;j<n;j++){ System.out.print(maps.get(c[j][0]%26)); } } } }
java实现多表代换解密
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; public class Mangbootjiemi { private static Map<Integer, String> maps = new HashMap<Integer, String>(); public static int getKey(Map map, String value) { int key = 0; Iterator it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Entry) it.next(); Object obj = entry.getValue(); if (obj != null && obj.equals(value)) { key = (int) entry.getKey(); } } return key; } public static void fuzhi() { String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for (int i = 0; i < str.length(); i++) { maps.put(i, str.substring(i, i + 1)); } } public static void addmatrixMultiply(int[][] a, int[][] d, int[][] c, int b[][], int n) { for (int i = 0; i < n; i++) for (int j = 0; j < 1; j++) { int sum = a[i][0] * d[0][j]; for (int k = 1; k < n; k++) sum += a[i][k] * d[k][j]; c[i][j] = sum; } } public static void main(String[] args) { fuzhi(); Scanner scanner = new Scanner(System.in); System.out.println("请输入你要解密的密文:"); String str = scanner.nextLine(); System.out.println("请输入密钥"); System.out.println("请输入A-1矩阵的维数:"); int n = scanner.nextInt(); System.out.println("开始创建A-1矩阵:"); int a[][] = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i][j] = scanner.nextInt(); } } System.out.println("开始创建B矩阵:"); int b[][] = new int[n][1]; int c[][] = new int[n][1]; int d[][] = new int[n][1]; for (int j = 0; j < n; j++) { b[j][0] = scanner.nextInt(); } System.out.println("获得的明文为:"); for (int i = 0; i < str.length() / n; i++) { String sub = str.substring(i * n, (i + 1) * n); for (int j = 0; j < n; j++) { d[j][0] = getKey(maps, sub.substring(j, j + 1)) - b[j][0]; } addmatrixMultiply(a, d, c, b, n); for (int j = 0; j < n; j++) { if (c[j][0]%26 < 0) { System.out.print( maps.get(( c[j][0]% 26) + 26)); }else System.out.print(maps.get(c[j][0] % 26)); } } } }