Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <string.h>
  4.  
  5.     int ZeroBegin=0;
  6.     int ZeroNumber=0;
  7.  
  8. void Shifrate(int* _Key, char* _InputName, char* _OutputName, int Type)
  9. {
  10.     bool flag=true;
  11.     bool flag1=false;
  12.     int ZeroBegin1=0;
  13.     char Buffer[4];
  14.     FILE* InputFile; //Исходный файл
  15.     FILE* OutputFile; //Зашифрованный файл
  16.     InputFile = fopen(_InputName, "rb");
  17.     if (InputFile!=NULL)
  18.     {
  19.     OutputFile = fopen(_OutputName, "wb");
  20.     unsigned long mask = 0;
  21.     unsigned long CatchBit = 0;
  22.     unsigned long result = 0;
  23.  
  24.     //Считываем в буфер 4 байта(размер ключа)
  25.     while( int BytesRead = fread(Buffer, 1, 4, InputFile) )
  26.     {
  27.         result = 0;
  28.         if( BytesRead < 4 ) //Если считано меньше 4 байт(конец файла)
  29.         {
  30.             if ((flag==true)&&(Type==1))
  31.             {
  32.             ZeroBegin=ftell(InputFile);
  33.             flag=false;
  34.             }
  35.             for( int i = BytesRead; i < 4; i++ )
  36.             {
  37.                 Buffer[i] = 0; //Дополняем нулями
  38.                 if (Type==1)
  39.                 {
  40.                 ZeroNumber++;
  41.                 }
  42.             }
  43.         }
  44.  
  45.         ZeroBegin1=ftell(InputFile);
  46.        
  47.         for (int i = 0; i < 32; i++)
  48.         {
  49.             mask = 0x80000000>>_Key[i];
  50.             CatchBit = *((unsigned long*)Buffer) & mask;
  51.             if(_Key[i]>i)
  52.             {
  53.                 CatchBit = CatchBit << (_Key[i]-i);
  54.             } else
  55.             {
  56.                 CatchBit = CatchBit >> (i - _Key[i]);
  57.             }
  58.             result = result|CatchBit;
  59.         }
  60.         if (Type==0)
  61.         {
  62.             ZeroBegin1=ZeroBegin1-ZeroNumber;
  63.             if (ZeroBegin1==ZeroBegin)
  64.             {
  65.                 fwrite(&result, 4-ZeroNumber, 1, OutputFile);
  66.                 flag1=true;
  67.             }
  68.         }
  69.         if (flag1!=true)
  70.         {
  71.             fwrite(&result, 4, 1, OutputFile);
  72.         }
  73.     }
  74.     printf("\tEncoding successful\n");
  75.     fclose(InputFile);
  76.     fclose(OutputFile);
  77.     Sleep(200);
  78.     }
  79.     else
  80.         {
  81.         printf("This input file does not exists");
  82.         Sleep(200);
  83.         }
  84. }
  85.  
  86. void main()
  87. {
  88. //  int key[]={ 24, 10, 2, 8, 21, 26, 28, 0, 18, 12,
  89. //              11, 25, 5, 22, 1, 16, 3, 9, 13, 4,
  90. //              19, 17, 6, 15, 14, 20, 7, 23, 30, 31, 27, 29}; // левый
  91.     int key[] = { 30, 12, 19, 27, 24, 9, 14, 2,
  92.                     22, 11, 13, 0, 17, 21, 29, 31, 28,
  93.                     23, 5, 25, 18, 1, 26, 10, 20, 16,
  94.                     6, 8, 7, 4, 15, 3 }; // мой
  95.    
  96.     int unkey[32];
  97.    
  98.     //Генерация обратного ключа
  99.    
  100.     while(1)
  101.     {
  102.         for(int i = 0; i < 32; i++)
  103.         {
  104.             for(int j = 0; j < 32; j++)
  105.             {
  106.                 if (j==key[i])
  107.                 {
  108.                 unkey[j]=i;
  109.                 break;
  110.                 }
  111.             }
  112.         }
  113.         break;
  114.     }
  115.  
  116.     //Вызов функции шифрования
  117.     Shifrate(key, "input.txt", "shifrilla.txt", 1);
  118.     Shifrate(unkey, "shifrilla.txt", "output10.txt", 0);
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement