2016-06-12 6 views
1

일반 텍스트를 암호화하고 그로부터 암호를 얻기 위해 데이터 암호화 표준 (DES)을 구현했습니다. DES는 56 비트 키, 64 비트 블록 크기 및 16 라운드를 사용하여 간단한 코드부터 시작합니다. 4 비트 키, 8 비트 블록 크기, 4 라운드 및 하위 키 생성 (즉, 모든 라운드에서 동일한 키 사용)을 사용했습니다. 매 라운드마다 DES에 따라 라운드 함수가 사용됩니다. 제 경우에는 bitwise 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

연산자 자체를 저장할 수 없습니다.

대신 사용자가 텍스트로 연산자를 입력하게하십시오. and, or, .. 및 코드에서 if-statement를 사용하여이 사용자 텍스트를 기반으로 올바른 연산자를 사용하십시오. 의사 코드 :

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