Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <windows.h>
- #include <string.h>
- int ZeroBegin=0;
- int ZeroNumber=0;
- void Shifrate(int* _Key, char* _InputName, char* _OutputName, int Type)
- {
- bool flag=true;
- bool flag1=false;
- int ZeroBegin1=0;
- char Buffer[4];
- FILE* InputFile; //Исходный файл
- FILE* OutputFile; //Зашифрованный файл
- InputFile = fopen(_InputName, "rb");
- if (InputFile!=NULL)
- {
- OutputFile = fopen(_OutputName, "wb");
- unsigned long mask = 0;
- unsigned long CatchBit = 0;
- unsigned long result = 0;
- //Считываем в буфер 4 байта(размер ключа)
- while( int BytesRead = fread(Buffer, 1, 4, InputFile) )
- {
- result = 0;
- if( BytesRead < 4 ) //Если считано меньше 4 байт(конец файла)
- {
- if ((flag==true)&&(Type==1))
- {
- ZeroBegin=ftell(InputFile);
- flag=false;
- }
- for( int i = BytesRead; i < 4; i++ )
- {
- Buffer[i] = 0; //Дополняем нулями
- if (Type==1)
- {
- ZeroNumber++;
- }
- }
- }
- ZeroBegin1=ftell(InputFile);
- for (int i = 0; i < 32; i++)
- {
- mask = 0x80000000>>_Key[i];
- CatchBit = *((unsigned long*)Buffer) & mask;
- if(_Key[i]>i)
- {
- CatchBit = CatchBit << (_Key[i]-i);
- } else
- {
- CatchBit = CatchBit >> (i - _Key[i]);
- }
- result = result|CatchBit;
- }
- if (Type==0)
- {
- ZeroBegin1=ZeroBegin1-ZeroNumber;
- if (ZeroBegin1==ZeroBegin)
- {
- fwrite(&result, 4-ZeroNumber, 1, OutputFile);
- flag1=true;
- }
- }
- if (flag1!=true)
- {
- fwrite(&result, 4, 1, OutputFile);
- }
- }
- printf("\tEncoding successful\n");
- fclose(InputFile);
- fclose(OutputFile);
- Sleep(200);
- }
- else
- {
- printf("This input file does not exists");
- Sleep(200);
- }
- }
- void main()
- {
- // int key[]={ 24, 10, 2, 8, 21, 26, 28, 0, 18, 12,
- // 11, 25, 5, 22, 1, 16, 3, 9, 13, 4,
- // 19, 17, 6, 15, 14, 20, 7, 23, 30, 31, 27, 29}; // левый
- int key[] = { 30, 12, 19, 27, 24, 9, 14, 2,
- 22, 11, 13, 0, 17, 21, 29, 31, 28,
- 23, 5, 25, 18, 1, 26, 10, 20, 16,
- 6, 8, 7, 4, 15, 3 }; // мой
- int unkey[32];
- //Генерация обратного ключа
- while(1)
- {
- for(int i = 0; i < 32; i++)
- {
- for(int j = 0; j < 32; j++)
- {
- if (j==key[i])
- {
- unkey[j]=i;
- break;
- }
- }
- }
- break;
- }
- //Вызов функции шифрования
- Shifrate(key, "input.txt", "shifrilla.txt", 1);
- Shifrate(unkey, "shifrilla.txt", "output10.txt", 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement