Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define size_x 6
- #define size_y 4
- #define N 16
- void bintohex(int input[] , char output[])
- {
- int i,j,k,loop,value;
- for(i = 0 , k = 0 ; i < N ; i += 4)
- {
- for(loop = 3 , j = i , value = 0 ; loop >= 0 ; j++ , --loop)
- value += input[j] << loop;
- //printf("%d\n",value);
- if (value < 10)
- output[k++] = value + 0x30;
- else
- {
- value -= 10;
- output[k++] = value + 0x30 + 0x11;
- }
- }
- for(i = 0 ; i < k ; i++)
- {
- printf("%c",output[i]);
- }
- puts("");
- }
- void hextobin(char (*p)[size_y] , int output[])
- {
- char *c;
- int i,j,k,value,loop;
- for(c = &p[0][0] , j = 3 , loop = 0 ; loop < 4 ; c++ , j += 4 , loop++)
- {
- if (*c >= 'A' && *c <= 'F')
- value = *c - 0x0 - 7;
- else
- value = *c - 0x0;
- for(k = 0 , i = j ; k <= 3 ; k++ , i--)
- {
- *(output+i) = value % 2;
- value /= 2;
- }
- }
- /*
- for(i = 0 ; i < N ; i++)
- printf("%d ",*(output+i));
- printf("\n");
- */
- }
- void Subkey(int input1[] , int input2[] , int output[])
- {
- int i;
- for(i = 0 ; i < N ; i++)
- output[i] = input1[i] ^ input2[i];
- /*
- printf("Subkey ");
- for(i = 0 ; i < N ; i++)
- printf("%d ",output[i]);
- puts("");
- */
- }
- void sBox(int input[] , char S[] , char output[])
- {
- int i,j,k,loop;
- int value;
- for(i = 0 , k = 0 ; i < N ; i += 4 , k++)
- {
- for(loop = 3 , j = i , value = 0 ; loop >= 0 ; j++ , --loop)
- {
- value += input[j] << loop;
- }
- output[k] = S[value];
- }
- }
- void pBox(int input[] , int P[] , int output[])
- {
- int i,j,k,loop;
- int value;
- int num;
- //puts("P-Box ");
- for(i = 0 ; i < N ; i++)
- {
- output[P[i]] = input[i];
- }
- /*
- for(i = 0 ; i < N ; i++)
- {
- printf("%d",output[i]);
- }
- puts("");
- */
- }
- int main()
- {
- char S1[N] = {'E','4','D','1','2','F','B','8','3','A','6','C','5','9','0','7'};
- char S2[N] = {'E','3','4','8','1','C','A','F','7','D','9','6','B','2','0','5'};
- int P[N] = {0,4,8,12,1,5,9,13,2,6,10,14,3,7,11,15};
- int sub[N]; /* Substitution results */
- int per[N]; /* Permutation results */
- int n1[N],n2[N]; /* Substitution input */
- int round;
- char input[6][4]; /* Divide sequence to input array */
- char ch[4]; /* Temporaily sBox output */
- char p[100]; /* Original sequence array */
- char *c;
- int i,j,k;
- int cond; /* Determine this program is Encrypt or Decrypt */
- //printf("Encryption/Decryption please enter (1/0): ");
- //scanf("%d",&cond);
- while(1)
- {
- scanf("%d",&cond);
- getchar();
- gets(p);
- for(c = p , i = 0 , j = 0 ; *c ; c++ )
- {
- if (*c == ' ')
- continue;
- input[i][j++] = *c;
- if (j % 4 == 0)
- {
- j = 0;
- i++;
- }
- }
- if (cond == 1)
- {
- for(round = 0 , i = 0 , j = 0 ; round < 3 ; round++)
- {
- if (round == 0)
- {
- hextobin(&input[i] , n1);
- hextobin(&input[++i] , n2);
- }
- else
- {
- hextobin(&input[++i] , n2);
- }
- Subkey(n1 , n2 , sub);
- sBox(sub , S1 , ch);
- for(j = 0 ; j < size_y ; j++)
- *(*(input + i) + j) = ch[j];
- hextobin(&input[i] , per);
- pBox(per , P , n1);
- }
- hextobin(&input[++i] , n2);
- Subkey(n1 , n2 , sub);
- sBox(sub , S1 ,ch);
- hextobin(&input[++i] , n2);
- for(j = 0 ; j < size_y ; j++)
- *(*(input + i) + j) = ch[j];
- hextobin(&input[i] , per);
- Subkey(per , n2 , sub);
- bintohex(sub , ch);
- }
- else if (cond == 2)
- {
- hextobin(&input[0] , n1);
- hextobin(&input[--i] , n2);
- Subkey(n1 , n2 , sub);
- sBox(sub , S2 , ch);
- for(j = 0 ; j < size_y ; j++)
- {
- *(*(input + 0) + j) = ch[j];
- }
- hextobin(&input[--i] , n2);
- hextobin(&input[0] , n1);
- Subkey(n1 , n2 , sub);
- for(--i ; i > 0 ; --i)
- {
- pBox(sub , P , per);
- sBox(per , S2 , ch);
- for(j = 0 ; j < size_y ; j++)
- *(*(input + 0) + j) = ch[j];
- hextobin(&input[0] , n1);
- hextobin(&input[i] , n2);
- Subkey(n1 , n2 , sub);
- }
- bintohex(sub , ch);
- }
- else
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement