This repository was archived by the owner on Sep 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypt.cpp
More file actions
72 lines (59 loc) · 2.77 KB
/
crypt.cpp
File metadata and controls
72 lines (59 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "StdAfx.h"
#include <ctype.h> // Для работы с функциями tolower() и toupper()
#include <locale.h> // Подключаем русскую локализацию текста в файле-исходнике
#include "crypt.h"
FILE *source, *_source, *Ckey, *CCrypt, *CDecrypt;
crypt::crypt()
{
source = fopen("C:\\Users\\Roma\\Desktop\\text.txt", "r"); // Файл-исходник
_source = fopen("C:\\Users\\Roma\\Desktop\\text_.txt", "w+"); // Промежуточный файл
Ckey = fopen("C:\\Users\\Roma\\Desktop\\key.txt", "w+"); // Файл с ключём
CCrypt = fopen("C:\\Users\\Roma\\Desktop\\crypt.txt", "w+"); // Файл с зашифрованым текстом
CDecrypt = fopen("C:\\Users\\Roma\\Desktop\\decrypt.txt", "w+"); // Файл с расшифрованым текстом
}
crypt::~crypt()
{
fcloseall; // Закрываем все потоки
}
void crypt::setKeyToLowReg(int Length, char *M)
{
for (int i = 0; i < Length; i++) {
fprintf(Ckey, "%c", (char)tolower(M[i])); //Переводим символы ключа в нижний регистр
}
}
int crypt::FindLengthFile()
{
fseek(source, 0, SEEK_END); // Устанавливаем позицию индикатора-указателя в конец файла
int lSize = ftell(source); // Считываем позицию индикатора-указателя
fseek(source, 0, SEEK_SET); // Устанавливаем позицию индикатора-указателя в начало файла
return lSize;
}
int crypt::ReadFile(char* Array, int FileLength)
{
const int k = fread(Array, 1, FileLength, source); // Возвращает k успешно прочитаных байт из файла
return k;
}
void crypt::setStringToUpperReg(int Len, char* Array)
{
for (int i = 0; i < Len; i++) {
fprintf(_source, "%c", (char)toupper(Array[i])); // Переводим исходный текст в верхний регистр
}
}
void crypt::Encrypting(int FileLength, int KeyLength, char* Array, char* Key)
{
for (int i = 0, j = 0; i < FileLength; i++) {
Array[i] ^= Key[j]; // Array[i] = Array[i] ^ Key[j]
j < KeyLength - 1 ? j++ : j = 0; // Условие цикличности ключа
fputc(Array[i], CCrypt);
// Записываем шифросимвол в файл crypt.txt
}
}
void crypt::Decrypting(int FileLength, int KeyLength, char* Array, char* Key)
{
for (int i = 0, j = 0; i < FileLength; i++) {
Array[i] ^= Key[j];
j < KeyLength - 1 ? j++ : j = 0;
fprintf(CDecrypt, "%c", (char)toupper(Array[i]));
// Записываем расшифрованый символ в decrypt.txt
}
}