1、替代算法:
已知替代变换函数f(a)=ak mod 26,且9k mod 26=15,试计算k
import java.util.Arrays; import javax.swing.*; public class HelloWorld { public static char[] letter={'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'}; public static char[] Encryp_letter={'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'}; //改变序列 public void changeletter(int k){ for(int i=0;i<26;i++){ int b=(i*k)%26; Encryp_letter[i]=letter[b]; } } //查找在序列中的位置 public int selectletter(char[] charArray,char lettervalue){ int index=0; char a=charArray[index]; while(a!=lettervalue){ index++; a=charArray[index]; } return index; } //加密 public String Encryption(String message){ String encry_message=""; for(int i=0;i<message.length();i++){ char result_letter=' '; result_letter=message.charAt(i); result_letter=Encryp_letter[selectletter(letter, result_letter)]; encry_message=encry_message+result_letter; } return encry_message; } //解密 public String Decryption(String message){ String decray_message=""; for(int i=0;i<message.length();i++){ char result_letter=' '; result_letter=message.charAt(i); result_letter=letter[selectletter(Encryp_letter, result_letter)]; decray_message=decray_message+result_letter; } return decray_message; } public static void main(String[] args) { HelloWorld hw=new HelloWorld(); int k=0; double b=0; while(b!=15){ k++; b=(9*k)%26; } hw.changeletter(k); while(true){ String input=JOptionPane.showInputDialog("你希望进行加密(e)还是解密(d)操作?(注:退出输入q)"); if(input.equals("加密")||input.equals("e")){ input=JOptionPane.showInputDialog("您需要加密的单词是?"); String output=hw.Encryption(input); JOptionPane.showMessageDialog(null, input+"加密后:"+output); }else if(input.equals("解密")||input.equals("d")){ input=JOptionPane.showInputDialog("您需要解密的单词是?"); String output=hw.Decryption(input); JOptionPane.showMessageDialog(null, input+"解密后:"+output); }else if(input.equals("q")){ System.exit(0); }else{ JOptionPane.showMessageDialog(null, "对不起,输入错误!"); } } } }
得到结果:加密substitute得到eqtexwxqxy
2、 置换加密法
1) 给定置换表“86427531”,编程实现基于该表的加密运算;
2) 加密字符串“transposition”,输出密文(提示:如字符串长度不是8的倍数,则需在字符串结尾后填充空格,使得其总长度为8的倍数);
View Code
import javax.swing.JOptionPane; public class replacement { public static int[] order = { 8, 6, 4, 2, 7, 5, 3, 1 }; public static void main(String[] args) { replacement rpm = new replacement(); while (true) { String input = JOptionPane .showInputDialog("你希望进行加密(e)还是解密(d)操作?(注:退出输入q)"); if (input.equals("加密") || input.equals("e")) { input = JOptionPane.showInputDialog("您需要加密的单词是?"); String output = rpm.Encryption(input); JOptionPane.showMessageDialog(null, input + "加密后:" + output); } else if (input.equals("解密") || input.equals("d")) { input = JOptionPane.showInputDialog("您需要解密的单词是?"); String output = rpm.Decryption(input); JOptionPane.showMessageDialog(null, input + "解密后:" + output); } else if (input.equals("q")) { System.exit(0); } else { JOptionPane.showMessageDialog(null, "对不起,输入错误!"); } } } // 加密 public String Encryption(String message) { String result = ""; while (message.length() % 8 != 0) { message = message + " "; } int Long = message.length() / 8; String[] messageArray = new String[Long]; for (int i = 0; i < Long; i++) { messageArray[i] = message.substring(i * 8, i * 8 + 8); } for (int i = 1; i <= order.length; i++) { int index = selectletter(order, i); for (int j = 0; j < Long; j++) { result = result + messageArray[j].charAt(index); } } return result; } // 解密 public String Decryption(String message) { String result = ""; while (message.length() % 8 != 0) { message = message + " "; } int Long = message.length() / 8; String[] messageArray = new String[Long]; for (int i = 0; i < Long; i++) { messageArray[i] = ""; for (int j = i; j < message.length();) { messageArray[i] = messageArray[i] + message.charAt(j); j = j + Long; } } // 还原数组 String[] messageResArray = new String[Long]; for (int j = 0; j < Long; j++) { messageResArray[j] = ""; for (int i = 0; i < order.length; i++) { messageResArray[j] = messageResArray[j] + messageArray[j].charAt(order[i] - 1); } } for (int j = 0; j < Long; j++) { result = result + messageResArray[j]; } return result; } // 查找在序列中的位置 public int selectletter(int[] charArray, int lettervalue) { int index = 0; int a = charArray[index]; while (a != lettervalue) { index++; a = charArray[index]; } return index; } }