LINUX.ORG.RU

История изменений

Исправление fero, (текущая версия) :

#include <stdio.h>


unsigned key[8]= {2542612654, 13689323, 1518563127, 3489012892, 2519843475, 3183980271, 1013633557, 104063607};
   
unsigned tab[8][16]=
   {{14, 4, 13, 13, 8, 15, 12, 0, 14, 8, 14, 6, 4, 0, 7, 15},
   {4, 8, 15, 4, 14, 10, 6, 1, 4, 5, 2, 3, 11, 12, 12, 3},
   {10, 3, 11, 7, 9, 15, 6, 0, 11, 0, 8, 2, 15, 7, 1, 10},
   {1, 11, 7, 3, 15, 13, 1, 3, 0, 10, 5, 1, 2, 10, 12, 12},
   {11, 15, 7, 11, 12, 15, 0, 1, 8, 5, 3, 11, 0, 1, 15, 3},
   {9, 4, 10, 4, 3, 14, 13, 5, 15, 7, 5, 12, 15, 14, 7, 9},
   {14, 11, 11, 10, 8, 14, 10, 1, 7, 15, 1, 0, 8, 4, 9, 5},
   {9, 12, 0, 5, 2, 7, 6, 13, 2, 5, 10, 11, 13, 13, 1, 12}};

//поблочная замена по таблице 
unsigned subst(unsigned N1)
 {
    unsigned i, t;
    unsigned mask= 0, umask;

    mask--;
    mask=15; //11111111111111111111111111110000
    umask= ~mask; //00000000000000000000000000001111

    for(i= 0; i < 8; ++i)
     {
       t= N1 >> 4*i; 
       t&= mask;
      
       t=tab[i][t];
       N1&= ~(umask << 4*i);
       N1|= t << 4*i;
       
     }
    return N1;
 }
//циклический сдвиг на 11 бит влево
unsigned shr(unsigned N1)
 {
    unsigned t= N1;

    N1= N1 << 11;
    N1|= t >> 21;

    return N1;
 }

void main_step(unsigned* pN1, unsigned* pN2, unsigned X)
 {
    unsigned S, N1, N2;

    N1= *pN1;
    N2= *pN2;
 
    S=N1+X;

    S= subst(S);
    S= shr(S);
    S^= N2;

    *pN1= S;
    *pN2= N1;
 }
//цикл зашифрования
void base_loop(unsigned* pN1, unsigned* pN2)
 {
    
    unsigned N1, N2;
    int i,j;

    N1= *pN1;
    N2= *pN2;

    for(i= 0; i < 3; ++i)
     {
        for(j= 0; j < 8; ++j) main_step(&N1,&N2, key[j]);
     }
    
    for(i= 7; i>= 0; --i) main_step(&N1,&N2, key[i]);

    *pN1= N1;
    *pN2= N2;
 }
//цикл расшифрования
void dbase_loop(unsigned* pN1, unsigned* pN2)
 {
    
    unsigned N1, N2;
    int i,j;

    N1= *pN1;
    N2= *pN2;

    for(i= 0; i < 8; ++i) main_step(&N1,&N2, key[i]);

    for(i= 0; i < 3; ++i)
     {
        for(j= 7; j >= 0; --j) main_step(&N1,&N2, key[j]);
     }
    
    *pN1= N1;
    *pN2= N2;
 }
int main()
 {

    unsigned N1, N2;
    FILE *xxx;
    xxx= fopen("xxx", "w+");
  
    char a[]= "abc";
    char b[]= "123";

    N1= (unsigned)a;
    N2= (unsigned)b; 

    base_loop(&N1,&N2);
    dbase_loop(&N1,&N2);

    fwrite(N1, 4, 1, xxx);
    fwrite(N2, 4, 1, xxx);


    return 0;
 }

Стыдно но пощу. Вещь тестовая, таблица и ключ намертво заварены в файл и пока что она шифрует/расшифрует один блок и то с помощью закомментирования :) Прошу посмотреть по диагонали, указать в каких местах я отстреливаю себе ноги и дать советов мудрых.

Исходная версия fero, :

#include <stdio.h>


unsigned key[8]= {2542612654, 13689323, 1518563127, 3489012892, 2519843475, 3183980271, 1013633557, 104063607};
   
unsigned tab[8][16]=
   {{14, 4, 13, 13, 8, 15, 12, 0, 14, 8, 14, 6, 4, 0, 7, 15},
   {4, 8, 15, 4, 14, 10, 6, 1, 4, 5, 2, 3, 11, 12, 12, 3},
   {10, 3, 11, 7, 9, 15, 6, 0, 11, 0, 8, 2, 15, 7, 1, 10},
   {1, 11, 7, 3, 15, 13, 1, 3, 0, 10, 5, 1, 2, 10, 12, 12},
   {11, 15, 7, 11, 12, 15, 0, 1, 8, 5, 3, 11, 0, 1, 15, 3},
   {9, 4, 10, 4, 3, 14, 13, 5, 15, 7, 5, 12, 15, 14, 7, 9},
   {14, 11, 11, 10, 8, 14, 10, 1, 7, 15, 1, 0, 8, 4, 9, 5},
   {9, 12, 0, 5, 2, 7, 6, 13, 2, 5, 10, 11, 13, 13, 1, 12}};

unsigned subst(unsigned N1)
 {
    unsigned i, t;
    unsigned mask= 0, umask;

    mask--;
    mask=15; //11111111111111111111111111110000
    umask= ~mask; //00000000000000000000000000001111

    for(i= 0; i < 8; ++i)
     {
       t= N1 >> 4*i; 
       t&= mask;
      
       t=tab[i][t];
       N1&= ~(umask << 4*i);
       N1|= t << 4*i;
       
     }
    return N1;
 }

unsigned shr(unsigned N1)
 {
    unsigned t= N1;

    N1= N1 << 11;
    N1|= t >> 21;

    return N1;
 }

void main_step(unsigned* pN1, unsigned* pN2, unsigned X)
 {
    unsigned S, N1, N2;

    N1= *pN1;
    N2= *pN2;
 
    S=N1+X;

    S= subst(S);
    S= shr(S);
    S^= N2;

    *pN1= S;
    *pN2= N1;
 }

void base_loop(unsigned* pN1, unsigned* pN2)
 {
    
    unsigned N1, N2;
    int i,j;

    N1= *pN1;
    N2= *pN2;

    for(i= 0; i < 3; ++i)
     {
        for(j= 0; j < 8; ++j) main_step(&N1,&N2, key[j]);
     }
    
    for(i= 7; i>= 0; --i) main_step(&N1,&N2, key[i]);

    *pN1= N1;
    *pN2= N2;
 }

void dbase_loop(unsigned* pN1, unsigned* pN2)
 {
    
    unsigned N1, N2;
    int i,j;

    N1= *pN1;
    N2= *pN2;

    for(i= 0; i < 8; ++i) main_step(&N1,&N2, key[i]);

    for(i= 0; i < 3; ++i)
     {
        for(j= 7; j >= 0; --j) main_step(&N1,&N2, key[j]);
     }
    
    *pN1= N1;
    *pN2= N2;
 }
int main()
 {

    unsigned N1, N2;
    FILE *xxx;
    xxx= fopen("xxx", "w+");
  
    char a[]= "abc";
    char b[]= "123";

    N1= (unsigned)a;
    N2= (unsigned)b; 

    base_loop(&N1,&N2);
    dbase_loop(&N1,&N2);

    fwrite(N1, 4, 1, xxx);
    fwrite(N2, 4, 1, xxx);


    return 0;
 }

Стыдно но пощу. Вещь тестовая, таблица и ключ намертво заварены в файл и пока что она шифрует/расшифрует один блок и то с помощью закомментирования :) Прошу посмотреть по диагонали, указать в каких местах я отстреливаю себе ноги и дать советов мудрых.