[Java] java矩阵换位加密 →→→→→进入此内容的聊天室

来自 , 2019-03-25, 写在 Java, 查看 150 次.
URL http://www.code666.cn/view/a02ef838
  1. package sun.netsecurity.matrixtran;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.math.BigInteger;
  7.  
  8. public class MatrixTransposition {
  9.        
  10.         private static BufferedReader br;
  11.        
  12.         static {
  13.                 InputStreamReader reader = new InputStreamReader(System.in);
  14.                 br = new BufferedReader(reader);
  15.         }
  16.  
  17.         /**
  18.          * @param args
  19.          * @throws IOException
  20.          */
  21.         public static void main(String[] args) throws IOException {
  22.  
  23.                 char[] plaintext = inputPlaintext();
  24.                 Integer key = inputKey();
  25.                
  26.                 br.close();
  27.  
  28.                 String ciphertext = encrypt(plaintext, key);
  29.                 System.out.println("明文加密后为: " + ciphertext);
  30.  
  31.                 String plaintextStr = decrypt(ciphertext.toCharArray(), plaintext, key);
  32.                 System.out.println("密文解密后为: " + plaintextStr);
  33.         }
  34.  
  35.         /**
  36.          * 将明文字符数组分组
  37.          *
  38.          * @param plaintext
  39.          *            明文字符数组
  40.          * @param key
  41.          *            密钥
  42.          * @return
  43.          */
  44.         static char[][] groupPlaintext(char[] plaintext, Integer key) {
  45.                 char[][] plaintextGroup = null;
  46.                 Integer length = plaintext.length;
  47.  
  48.                 if (length % key != 0) {// 当字符数组的长度不是密钥的整数倍时,用字符'$'填补
  49.                         String plaintextStr = new String(plaintext);
  50.  
  51.                         BigInteger lengthBI = new BigInteger(length.toString());
  52.                         BigInteger keyBI = new BigInteger(key.toString());
  53.                         BigInteger mod = lengthBI.mod(keyBI);
  54.  
  55.                         int fillNumber = keyBI.intValue() - mod.intValue();
  56.                         for (int i = 0; i < fillNumber; i++) {
  57.                                 plaintextStr = plaintextStr + "$";
  58.                         }
  59.                         plaintext = plaintextStr.toCharArray();
  60.                         length = plaintext.length;
  61.                 }
  62.  
  63.                 int groupAmount = length / key;
  64.                 plaintextGroup = new char[groupAmount][key];
  65.                 for (int i = 0; i < groupAmount; i++) {
  66.                         for (int j = 0; j < key; j++) {
  67.                                 // 将明文字符数组分割为二维数组
  68.                                 plaintextGroup[i][j] = plaintext[i * key + j];
  69.                         }
  70.                 }
  71.  
  72.                 return plaintextGroup;
  73.         }
  74.  
  75.         /**
  76.          * 将密文分组
  77.          *
  78.          * @param ciphertext
  79.          *            密文
  80.          * @param key
  81.          *            密钥
  82.          * @return
  83.          */
  84.         static char[][] groupCiphertext(char[] ciphertext, Integer key) {
  85.                 char[][] ciphertextGroup = null;
  86.                 int length = ciphertext.length;
  87.  
  88.                 int groupAmount = length / key;
  89.                 ciphertextGroup = new char[groupAmount][key];
  90.                 for (int i = 0; i < groupAmount; i++) {
  91.                         for (int j = 0; j < key; j++) {
  92.                                 ciphertextGroup[i][j] = ciphertext[i * key + j];
  93.                         }
  94.                 }
  95.  
  96.                 return ciphertextGroup;
  97.         }
  98.  
  99.         /**
  100.          * 按照加密规则,将分割后的各个子字符数组分别进行移位
  101.          *
  102.          * @param group
  103.          *            每一个子字符数组
  104.          * @return
  105.          */
  106.         static char[] changePlaintext(char[] group) {
  107.                 char[] newGroup = new char[group.length];
  108.                 for (int i = 0; i < group.length; i++) {
  109.                         if (i == 0) {
  110.                                 newGroup[0] = group[group.length - 1];
  111.                                 continue;
  112.                         }
  113.  
  114.                         newGroup[i] = group[i - 1];
  115.                 }
  116.                 return newGroup;
  117.         }
  118.  
  119.         /**
  120.          *
  121.          * @param group
  122.          *            按照加密规则,将密文分割后的各个子字符数组按照相反的方式分别进行移位
  123.          * @return
  124.          */
  125.         static char[] changeCiphertext(char[] group) {
  126.                 char[] newGroup = new char[group.length];
  127.                 for (int i = 0; i < group.length; i++) {
  128.                         if (i == group.length - 1) {
  129.                                 newGroup[group.length - 1] = group[0];
  130.                                 break;
  131.                         }
  132.  
  133.                         newGroup[i] = group[i + 1];
  134.                 }
  135.                 return newGroup;
  136.         }
  137.  
  138.         static String getCipherFromGroup(char[][] plaintextGroup) {
  139.                 String ciphertext = "";
  140.  
  141.                 int rowAccount = plaintextGroup.length;
  142.                 int lowAccount = plaintextGroup[0].length;
  143.  
  144.                 for (int i = 0; i < rowAccount; i++) {
  145.                         char[] rowChar = new char[lowAccount];
  146.                         for (int j = 0; j < lowAccount; j++) {
  147.                                 rowChar[j] = plaintextGroup[i][j];
  148.                         }
  149.                         ciphertext = ciphertext + new String(rowChar);
  150.                 }
  151.  
  152.                 return ciphertext;
  153.         }
  154.  
  155.         static String getPlaintextFromGroup(char[][] ciphertextGroup) {
  156.                 String plaintext = "";
  157.  
  158.                 int rowAccount = ciphertextGroup.length;
  159.                 int lowAccount = ciphertextGroup[0].length;
  160.  
  161.                 for (int i = 0; i < rowAccount; i++) {
  162.                         char[] rowChar = new char[lowAccount];
  163.                         for (int j = 0; j < lowAccount; j++) {
  164.                                 rowChar[j] = ciphertextGroup[i][j];
  165.                         }
  166.                         plaintext = plaintext + new String(rowChar);
  167.                 }
  168.  
  169.                 return plaintext;
  170.         }
  171.  
  172.         // 从键盘输入明文
  173.         static char[] inputPlaintext() {
  174.  
  175.                 System.out.println("请输入明文:");
  176.  
  177.                 char[] plaintextChar = null;
  178.  
  179.                 try {
  180.                         String plaintext = null;
  181.                         plaintext = br.readLine();
  182.                         plaintextChar = plaintext.toCharArray();
  183.                 } catch (Exception e) {
  184.                         e.printStackTrace();
  185.                 }
  186.  
  187.                 return plaintextChar;
  188.         }
  189.  
  190.         // 从键盘获得密钥
  191.         static Integer inputKey() {
  192.  
  193.                 System.out.println("请输入密钥:");
  194.  
  195.                 Integer key = null;
  196.  
  197.                 try {
  198.                         String keyString = null;
  199.                         keyString = br.readLine();
  200.                         key = new Integer(keyString);
  201.                 } catch (Exception e) {
  202.                         e.printStackTrace();
  203.                 }
  204.  
  205.                 return key;
  206.         }
  207.  
  208.         /**
  209.          * 对明文进行加密
  210.          *
  211.          * @param plaintext
  212.          *            明文
  213.          * @param key
  214.          *            密钥
  215.          * @return
  216.          */
  217.         static String encrypt(char[] plaintext, Integer key) {
  218.                 String ciphertext;
  219.                 char[][] plaintextGroup;
  220.  
  221.                 // 调用groupPlaintext(plaintext, key)方法对明文分组
  222.                 plaintextGroup = groupPlaintext(plaintext, key);
  223.                 for (int i = 0; i < plaintextGroup.length; i++) {
  224.                         // 调用changePlaintext(char[])进行位变换
  225.                         char[] rowText = changePlaintext(plaintextGroup[i]);
  226.                         plaintextGroup[i] = rowText;
  227.                 }
  228.  
  229.                 //
  230.                 ciphertext = getCipherFromGroup(plaintextGroup);
  231.  
  232.                 return ciphertext;
  233.         }
  234.        
  235.         /**
  236.          * 对密文进行解密
  237.          * @param ciphertext 密文
  238.          * @param plaintext  明文
  239.          * @param key        密钥
  240.          * @return
  241.          */
  242.         static String decrypt(char[] ciphertext, char[] plaintext, Integer key) {
  243.                 String plaintextStr;
  244.                 char[][] ciphertextGroup;
  245.  
  246.                 ciphertextGroup = groupCiphertext(ciphertext, key);
  247.                 for (int i = 0; i < ciphertextGroup.length; i++) {
  248.                         char[] rowText = changeCiphertext(ciphertextGroup[i]);
  249.                         ciphertextGroup[i] = rowText;
  250.                 }
  251.                
  252.                 plaintextStr = getPlaintextFromGroup(ciphertextGroup);
  253.  
  254.                 Integer plaintextLength = plaintext.length;
  255.                 BigInteger pLenghBI = new BigInteger(plaintextLength.toString());
  256.                 BigInteger keyBI = new BigInteger(key.toString());
  257.                 BigInteger mod = pLenghBI.mod(keyBI);
  258.                 if (mod.intValue() != 0) {
  259.                         plaintextStr = plaintextStr.substring(0, plaintextStr.length()
  260.                                         - (key - mod.intValue()));
  261.                 }
  262.  
  263.                 return plaintextStr;
  264.         }
  265. }
  266.  
  267. //java/5901

回复 "java矩阵换位加密"

这儿你可以回复上面这条便签

captcha