Advertisement
Guest User

SPN

a guest
Apr 19th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define size_x 6
  4. #define size_y 4
  5. #define N 16
  6.  
  7. void bintohex(int input[] , char output[])
  8. {
  9.     int i,j,k,loop,value;
  10.     for(i = 0 , k = 0 ; i < N ; i += 4)
  11.     {
  12.         for(loop = 3 , j = i , value = 0 ; loop >= 0  ; j++ , --loop)
  13.             value += input[j] << loop;
  14.         //printf("%d\n",value);
  15.         if (value < 10)
  16.             output[k++] = value + 0x30;
  17.         else
  18.         {
  19.             value -= 10;
  20.             output[k++] = value + 0x30 + 0x11;
  21.         }
  22.     }
  23.     for(i = 0 ; i < k ; i++)
  24.     {
  25.         printf("%c",output[i]);
  26.     }
  27.     puts("");
  28.    
  29. }
  30. void hextobin(char (*p)[size_y] , int output[])
  31. {
  32.     char *c;
  33.     int i,j,k,value,loop;
  34.     for(c = &p[0][0] , j = 3 , loop = 0  ; loop < 4  ; c++ , j += 4 , loop++)
  35.     {
  36.         if (*c >= 'A' && *c <= 'F')
  37.             value = *c - 0x0  - 7;
  38.         else
  39.             value = *c - 0x0;
  40.         for(k = 0 , i = j ; k <= 3 ; k++ , i--)
  41.         {
  42.             *(output+i) = value % 2;
  43.             value /= 2;
  44.         }
  45.     }
  46.     /*
  47.     for(i = 0 ; i < N ; i++)
  48.         printf("%d ",*(output+i));
  49.     printf("\n");
  50.     */
  51. }
  52. void Subkey(int input1[] , int input2[] , int output[])
  53. {
  54.     int i;
  55.     for(i = 0 ; i < N ; i++)
  56.         output[i] = input1[i] ^ input2[i];
  57.     /*
  58.     printf("Subkey   ");
  59.     for(i = 0 ; i < N ; i++)
  60.         printf("%d ",output[i]);
  61.     puts("");  
  62.     */
  63. }
  64.  
  65. void sBox(int input[] , char S[] , char output[])
  66. {
  67.     int i,j,k,loop;
  68.     int value;
  69.     for(i = 0 , k = 0 ; i < N ; i += 4 , k++)
  70.     {
  71.         for(loop = 3 , j = i , value = 0 ; loop >= 0  ; j++ , --loop)
  72.         {
  73.             value += input[j] << loop;
  74.         }
  75.         output[k] = S[value];
  76.     }
  77. }
  78. void pBox(int input[] , int P[] , int output[])
  79. {  
  80.     int i,j,k,loop;
  81.     int value;
  82.     int num;
  83.     //puts("P-Box       ");
  84.     for(i = 0 ; i < N ; i++)
  85.     {
  86.         output[P[i]] = input[i];
  87.     }
  88.     /*
  89.     for(i = 0 ; i < N ; i++)
  90.     {
  91.         printf("%d",output[i]);
  92.     }
  93.     puts("");
  94.     */
  95. }
  96. int main()
  97. {
  98.     char S1[N] = {'E','4','D','1','2','F','B','8','3','A','6','C','5','9','0','7'};
  99.     char S2[N] = {'E','3','4','8','1','C','A','F','7','D','9','6','B','2','0','5'};
  100.     int P[N] = {0,4,8,12,1,5,9,13,2,6,10,14,3,7,11,15};
  101.     int sub[N];         /* Substitution results           */
  102.     int per[N];         /* Permutation results            */
  103.     int n1[N],n2[N];    /* Substitution input             */
  104.     int round;      
  105.     char input[6][4];   /* Divide sequence to input array */
  106.     char ch[4];     /* Temporaily sBox output         */
  107.     char p[100];        /* Original sequence array        */
  108.     char *c;
  109.     int i,j,k;
  110.     int cond;        /* Determine this program is Encrypt or Decrypt */
  111.     //printf("Encryption/Decryption please enter (1/0): ");
  112.     //scanf("%d",&cond);
  113.     while(1)
  114.     {    
  115.         scanf("%d",&cond);
  116.         getchar();
  117.         gets(p);
  118.         for(c = p , i = 0 , j = 0 ; *c ; c++ )
  119.         {
  120.             if (*c == ' ')
  121.                 continue;
  122.             input[i][j++] = *c;
  123.             if (j % 4 == 0)
  124.             {
  125.                 j = 0;
  126.                 i++;
  127.             }
  128.         }
  129.         if (cond == 1)
  130.         {
  131.             for(round = 0 , i = 0 , j = 0 ; round < 3  ;  round++)
  132.             {
  133.                 if (round == 0)
  134.                 {
  135.                     hextobin(&input[i] , n1);
  136.                     hextobin(&input[++i] , n2);
  137.                 }
  138.                 else
  139.                 {
  140.                     hextobin(&input[++i] , n2);
  141.                 }
  142.                 Subkey(n1 , n2 , sub);
  143.                 sBox(sub , S1 , ch);
  144.                 for(j = 0 ; j < size_y ; j++)
  145.                     *(*(input + i) + j) = ch[j];
  146.                 hextobin(&input[i] , per);
  147.                 pBox(per , P , n1);
  148.             }
  149.             hextobin(&input[++i] , n2);
  150.             Subkey(n1 , n2 , sub);
  151.             sBox(sub , S1 ,ch);
  152.             hextobin(&input[++i] , n2);
  153.             for(j = 0 ; j < size_y ; j++)
  154.                 *(*(input + i) + j) = ch[j];
  155.             hextobin(&input[i] , per);
  156.             Subkey(per , n2 , sub);
  157.             bintohex(sub , ch);
  158.         }
  159.         else if (cond == 2)
  160.         {
  161.             hextobin(&input[0] , n1);
  162.             hextobin(&input[--i] , n2);
  163.             Subkey(n1 , n2 , sub);
  164.             sBox(sub , S2 , ch);
  165.             for(j = 0 ; j < size_y ; j++)
  166.             {
  167.                 *(*(input + 0) + j) = ch[j];
  168.             }
  169.             hextobin(&input[--i] , n2);
  170.             hextobin(&input[0] , n1);
  171.             Subkey(n1 , n2 , sub);
  172.             for(--i ; i > 0 ; --i)
  173.             {
  174.                 pBox(sub , P , per);
  175.                 sBox(per , S2 , ch);
  176.                 for(j = 0 ;  j < size_y ; j++)
  177.                     *(*(input + 0) + j) = ch[j];
  178.                 hextobin(&input[0] , n1);
  179.                 hextobin(&input[i] , n2);
  180.                 Subkey(n1 , n2 , sub);
  181.             }
  182.             bintohex(sub , ch);
  183.         }
  184.         else
  185.             break;
  186.     }
  187.    
  188.     return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement