信息安全_置换加密算法_矩阵换位加…

来源:互联网 发布:ubuntu crontab 日志 编辑:程序博客网 时间:2024/06/08 08:01
【题意描述】

置换密码算法的原理是不改变明文字符,而是按照某一规则重新排列消息中的比特或字符顺序,才而实现明文信息的加密。置换密码有时又称为换位密码。

矩阵换位法是实现置换密码的一种常用方法。它将明文中的字母按照给定的顺序安排在一个矩阵中,然后用根据密钥提供的顺序重新组合矩阵中的字母,从而形成密文。例如,明文为attack begins at five,密钥为cipher,将明文按照每行6个字母的形式排在矩阵中,形成如下形式:

信息安全_置换加密算法_矩阵换位加密算法_加密和解密

根据密钥cipher中各个字母在字母表中出现的先后顺序,给定一个置换:

信息安全_置换加密算法_矩阵换位加密算法_加密和解密

 

由密钥cipher通过getTheMatrix方法得到f矩阵。

根据上面的置换,将原有居住中的字母按照第1列、第4裂、第5裂、第3裂、第2列、第6列的顺序排列,则有下面的形式:

信息安全_置换加密算法_矩阵换位加密算法_加密和解密 

这里经过了一点处理就是当输入的字母数不是密钥长度的整数倍时,在输入的备加密字符串加上几个“#”来完成补全,然后参加到加密的过程中去,从而得到如下的密文:

a siteaetb vci#agt#knf#

其解密过程是根据密钥的字母数作为列数,将密文按照列、行的顺序写出,再根据由   密钥给出的矩阵置换产生新的矩阵,从而恢复明文。

      本实验中也使用了上述的测试用例,主要经过两次依据矩阵的变换,都是使用矩阵f中的下面一行中的数值所在列来替换上面一行数值表示的列的值,从而来改变起序列、完成加密。

解密中用到groupedSource[i][j]=temp[i][secretMatrix[secretMatrix[j]]];

来完成将原来经过矩阵转变来的字符序列逆转,从而得到起加密前的矩阵。

【代码部分】

package Practice1;

import java.util.Arrays;
import java.util.Scanner;


public class ReplaceDemo {
 private String sourceString = "attack begins atfive";
 private String keyString = "cipher";
 
 private int[] secretMatrix;
 private char groupedSource[][];
 
 private void getSource(){//Get the sourceString
  Scanner scan = newScanner(System.in);
  System.out.print("Please inputthe string what you want to encrypt:\t");
  sourceString =scan.nextLine();
  System.out.print("Please inputthe key string for you:\t");
  keyString = scan.next();
 }
 
 public ReplaceDemo(){//constructor for thisclass
  getSource();
 }
 
 private void groupSourceString(){//make groupsfor sourceString according by the keyString
  int sourceLen =sourceString.length();
  int keyLen =keyString.length();
  groupedSource = newchar[sourceLen/keyLen+1][keyLen];
  if (sourceLen%keyLen!=0){
   for (int i =0;i < (keyLen - sourceLen%keyLen);i++){
    sourceString+= "#";
   }
  }
  sourceLen =sourceString.length();
  for(int i = 6,j=0;i<= sourceLen;i+=keyLen,j++){
    groupedSource[j]= sourceString.substring(i-keyLen, i).toCharArray();
  }
 }
 
 private void getTheMatrix(){//get the Matrix forthe encrypt
  char[] temp01 =keyString.toCharArray();
  secretMatrix = newint[keyString.length()];
  Arrays.sort(temp01);
  for (int i = 0;i< keyString.length();i++){
   for (int j =0;j<keyString.length();j++){
    if(keyString.toCharArray()[i]==temp01[j]){
     secretMatrix[i]= j;
    }
   }
  }
  }
 
 private void changeSourceMatix(){
  groupSourceString();
  char temp[][] = newchar[groupedSource.length][keyString.length()];
  for (int i = 0;i< groupedSource.length;i++){
   for (int j =0;j < keyString.length();j++){
    temp[i][j]= groupedSource[i][j];
   }
  }//请教Java老师
  
  for (int i = 0;i< groupedSource.length - 1;i++){
   for (int j =0;j < keyString.length();j++){
    groupedSource[i][j]= temp[i][secretMatrix[j]];
   }
  }
 }
 
 private void getEncryptedString(){//get thematrix which will be printed.
  groupSourceString();
  changeSourceMatix();
  char temp[][] = newchar[groupedSource.length][keyString.length()];
  for (int i = 0;i< groupedSource.length;i++){
   for (int j =0;j < keyString.length();j++){
    temp[i][j]= groupedSource[i][j];
   }
  }
  
  for (int i = 0;i< groupedSource.length - 1;i++){
   for (int j =0;j < keyString.length();j++){
    groupedSource[i][j]= temp[i][secretMatrix[j]];
   }
  }
 }
 
 public void print1(){//print the code haveEncrypted
  getEncryptedString();
  for(int i = 0;i< keyString.length();i++){
   for(int j =0;j < groupedSource.length-1;j++){
    System.out.print(groupedSource[j][i]);
   }
  }
  System.out.println();
 }
 
 //decryption process
 private void groupEncryptedString(){
  int sourceLen =sourceString.length();
  int keyLen =keyString.length();
  char[][] temp=  new char[keyLen][sourceLen/keyLen+1];
  for (int i = 0;i< keyLen;i++){
   temp[i] =sourceString.substring(i*sourceLen/keyLen,(i+1)*sourceLen/keyLen).toCharArray();
  }
  groupedSource = newchar[sourceLen/keyLen+1][keyLen];
  for(int j = 0; j< sourceLen/keyLen;j++){
   for (int i =0;i < keyLen;i++){
    groupedSource[j][i]= temp[i][j];
   }
  }
 }
 
 private void getDecryptString(){//get the matrixwhich will be printed.
  groupEncryptedString();
  char temp[][] = newchar[groupedSource.length][keyString.length()];
  for (int i = 0;i< groupedSource.length;i++){
   for (int j =0;j < keyString.length();j++){
    temp[i][j]= groupedSource[i][j];
   }
  }
  
  for (int i = 0;i< groupedSource.length - 1;i++){
   for (int j =0;j < keyString.length();j++){
    groupedSource[i][j]= temp[i][secretMatrix[secretMatrix[j]]];
   }
  }
 }
 
 public void print2(){//print the code haveEncrypted
  getDecryptString();
  for(int j = 0;j< groupedSource.length-1;j++){
   for(int i =0;i < keyString.length();i++){
    System.out.print(groupedSource[j][i]);
   }
  }
  System.out.println();
 }
 
 public static void main(String args[]){
  ReplaceDemo RD = null;
  Scanner scanin = null;
  System.out.println("pleaseselect the method that you want to operate:\t(select “0” forencrypte and select “1” for decrypt.)");
  scanin = newScanner(System.in);
  int selected =scanin.nextInt();
  while(selected==0 ||selected==1){
   RD = newReplaceDemo();
   RD.getTheMatrix();
   if (selected== 0){
    RD.print1();
   }
   else{
    RD.print2();
   }
   System.out.println("pleaseselect the method that you want to operate:\t(select “0” forencrypte and select “1” for decrypt.)");
   scanin = newScanner(System.in);
   selected =scanin.nextInt();
  }
  System.out.println("Youroperate is over!Thank you!");
 }
}

【测试用例】如下图:

 信息安全_置换加密算法_矩阵换位加密算法_加密和解密

原创粉丝点击