2012-02-07 2 views
2

현재 암호화 스키마 DES를 구현하려고하고 있지만 초기에 문제가 발생했습니다. 이것은 내가 프로그램에서 비트 단위 조작을 수행 한 최초의 사례이며 C에 대해서도 능숙하지 못합니다. 나는 순열과 그 역함수를 적용하고 그 결과는 입력과 동일하지 않다.DES - 비트 및 역순의 순열

내가하려고하는 것은 64 비트 블록에 초기 순열과 역 순열을 적용하는 것입니다. 배열 입력에서 암호화하려는 64 비트 블록을 가지고 있습니다. 순열 테이블 IP에 따르면 첫 번째 바이트에서 첫 번째 비트를 가져 와서 순열의 비트 58로 지정합니다. 비트 2는 비트 50으로 전송되는 식입니다. 순열 후에 결과는 반으로 나뉘고 양쪽은 서로 바뀝니다. 이렇게하면 같은 알고리즘을 사용하지만 IPinverse 테이블을 사용하여 다시 넣을 수 있습니다.

include <stdio.h> 
include <stdlib.h> 

static unsigned char Positions[8] = {1,2,4,8,16,32,64,128}; 

int main() 
{ 
    unsigned char input[8] = {'a','b','c','d','e','f','g','h'}; 
    unsigned char permutation[8]; 
    unsigned char inverse[8]; 
    int i; 
    for (i = 0; i < 8; i++) { 
     permutation[i] = 0; 
     inverse[i] = 0; 
    } 

    int IP[8][8] ={{58,50,42,34,26,18,10,2}, 
          {60,52,44,36,28,20,12,4}, 
          {62,54,46,38,30,22,14,6}, 
          {64,56,48,40,32,24,16,8}, 
          {57,49,41,33,25,17, 9, 1}, 
          {59,51,43,35,27,19,11,3}, 
          {61,53,45,37,29,21,13,5}, 
          {63,55,47,39,31,23,15,7}}; 

    int IPinverse[8][8] ={{40,8,48,16,56,24,64,32}, 
             {39,7,47,15,55,23,63,31}, 
             {38,6,46,14,54,22,62,30}, 
             {37,5,45,13,53,21,61,29}, 
             {36,4,44,12,52,20,60,28}, 
             {35,3,43,11,51,19,59,27}, 
             {34,2,42,10,50,18,58,26}, 
             {33, 1,41, 9,49,17,57,25}}; 

    printf("\n Before: \n"); 
    for (i = 0; i < 8; i++) { 
     printf(" %c", input[i]); 
    } 

    // Initial permutation 
    int bit, newpos; 
    unsigned char desiredbit; 
    for (bit = 0; bit < 64; bit++) { 
     // Get the location for where the bit will be sent and translate it to array index 
     newpos = ((int)IP[bit/8][bit%8])-1; 
     // examine the bit we're currently considering 
     desiredbit = input[bit/8] & Positions[bit%8]; 
     // if equal to zero that means no change necessary 
     if (desiredbit != 0) { 
       // else it was a 1 and we need to set the appropriate bit to 1 
       desiredbit = Positions[newpos%8]; 
       permutation[newpos/8] = desiredbit^permutation[newpos/8]; 
     } 
    } 

    printf("\n Permutation: \n"); 
    for (i = 0; i < 8; i++) { 
     printf(" %c", permutation[i]); 
    } 

    // Perform swap 
    unsigned char tempcopy[4] = {0,0,0,0}; 
    int j; 
    for (j = 0; j < 4; j++) { 
     tempcopy[j] = permutation[j+4]; 
    } 
    for (j = 0; j < 4; j++) { 
     permutation[j+4] = permutation[j]; 
     permutation[j] = tempcopy[j]; 
    } 

    // Reverse Permutation, remember to swap left side with right 
    for (bit = 0; bit < 64; bit++) { 
     newpos = ((int)IPinverse[bit/8][bit%8])-1; 
     desiredbit = permutation[bit/8] & Positions[bit%8]; 
     if (desiredbit != 0) { 
       desiredbit = Positions[newpos%8]; 
       inverse[newpos/8] = desiredbit^inverse[newpos/8]; 
     } 
    } 

printf("\n Reverse Permutation: \n"); 
    for (i = 0; i < 8; i++) { 
     printf(" %c", inverse[i]); 
    } 

    return 0; 

}

+0

무엇이 옳은가요? 어디에서 실패하기 시작합니까? – KevinDTimm

답변

1
  1. 귀하의 순열 64 일에서 인덱스를 포함하지만, 당신이 그들을 사용하는 방법, 그들은해야한다 0 스왑 무엇
  2. 63? permute, swap, permute back을하면 같은 장소에 도착하지 않을 것입니다.
  3. 순열과 역이 참으로 옳은지 확인해야합니다. 나는 모든 숫자를 검토하고 검증하지 않을 것입니다.
+0

1. 인덱스는 모두 1에서 뺀 것을 볼 수 있으므로 0-63으로 바꿀 수 있습니다. 2. 스왑은 버그로 밝혀졌습니다. 초기 순열에 16 라운드 암호화가 적용된 경우 최종 순열에 사용해야합니다. 스왑을 제거하면 문제가 해결되었습니다. 3. 테이블이 정확합니다. DES 암호화에 사용 된 테이블의 정확한 사본입니다. [link] http://en.wikipedia.org/wiki/DES_supplementary_material#Final_permutation_.28IP-1.29 – Steinin

+0

1 in 3가 끝났습니다. – ugoren