2016-06-12 37 views
1

我已经实现了数据加密标准(DES)来加密纯文本并从中获取密码。虽然DES使用56位密钥,64位块大小和16个循环,但是从简单的代码开始。我使用4位密钥,8位块大小,4轮并且没有生成子密钥(即,在所有轮次中使用相同的密钥)。根据DES每一轮的每一轮使用一个循环函数。在我的情况下,我使用了按位AND(&)运算符。存储用户输入的按位运算符的数据类型是什么

这里是我的代码的一部分

import java.util.Arrays; 

public class Encrypt { 
    private int key[]={1,0,1,0}; 
    private int dataBlock[]={1,1,1,0,1,0,1,1}; 
    private int dataLeft[]=new int [4]; // for left part the plain text 
    private int dataRight[]=new int [4]; //for right part of the plain text 
    private int dataLeftTemp[]; 
    private int dataRightTemp[]=new int [4]; 
    private int i=2; // as initially two steps are run through automatically 
    private int n=5; // the no of rounds 
    private int cipher[]; 

    public void splitting(){ 
     int j=0;  // for setting the indexes of the dataRight[] 
     for(int i=0;i<dataBlock.length;i++){ 

      if (i<(dataBlock.length/2)){ 

       dataLeft[i]=dataBlock[i]; 

      } 
      //   when i is greater than the half the index of the plain text 
      else{ 

       dataRight[j]=dataBlock[i]; 
       j++; 
      } 
     } 
     // for printing the array------------------------------------------------- 
     System.out.println("DataSet"); 
     for(int i: dataLeft){ 
      System.out.print(i); 
     } 
     for(int i: dataRight){ 
      System.out.print(i); 
     } 
     System.out.println(" "); 
     System.out.println(" "); 
     //------------------------------------------------------------------------ 
    } 

    //==============================round function================================================ 

    public void roundingStart(){ 
     System.out.println("Enter the round function"); 



     for(int i=0;i<4;i++){ 
      //   AND function 
      dataRightTemp[i]=key[i] & dataRight[i]; 
      //    XOR function 
      dataRightTemp[i]=dataRightTemp[i]^dataLeft[i]; 

     } 

     dataLeft=dataRight.clone(); 
     //  printResults(); 

     printFirst(); 
     roundingRest(dataLeft,dataRightTemp); 


    } 
//rest of the code 
} 

这工作得很好。但是,如何更改上面的代码,以便用户可以输入要在循环函数中使用的按位运算符。我试着用Scanner但我不知道该用什么数据类型来存储用户输入的位运算符,以便它可以在行中使用

dataRightTemp[i]=key[i] & dataRight[i]; 

有人能解释我是如何做到这一点?

+1

你将无法存储位运算符本身,而是存储标志。例如,如果标志是1,则使用&,标志是2使用|,如果标志是3使用^等等。 –

回答

2

您不能存储操作员本身。

改为让用户输入操作符为文本:例如, andor,.. 并在您的代码中使用if语句以基于此用户文本使用正确的运算符。伪代码:

if ("and".equalsIgnoreCase(userInput) { 
    dataRightTemp[i]=key[i] & dataRight[i]; 
} else if ("or".equalsIgnoreCase(userInput)) { 
    dataRightTemp[i]=key[i] | dataRight[i]; 
} 
相关问题