-
Notifications
You must be signed in to change notification settings - Fork 0
/
cript_versao4.cpp
127 lines (105 loc) · 2.87 KB
/
cript_versao4.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <cstdio>
#include <cctype>
#include <cstring>
void criptografarTexto(char* texto, int chave)
{
printf("\nTexto:\n%s\n", texto);
int i = 0;
while (texto[i] != '\0') {
texto[i] = (texto[i] + chave);
i++;
}
}
void descriptografarTexto(char* texto, int chave)
{
printf("\nTexto:\n%s\n", texto);
int i = 0;
while (texto[i] != '\0') {
texto[i] = (texto[i] - chave);
i++;
}
}
char* lerArquivo(const char* nomeArquivo)
{
FILE* arquivo_ori;
char texto_ori[1000], linha[1000];
arquivo_ori = fopen(nomeArquivo, "r");
if (arquivo_ori == NULL) {
printf("Erro ao abrir o arquivo.\n");
return nullptr;
}
strcpy(texto_ori, "");
while (fgets(linha, 1000, arquivo_ori) != NULL) {
strcat(texto_ori, linha);
}
fclose(arquivo_ori);
char* conteudo = new char[strlen(texto_ori) + 1];
strcpy(conteudo, texto_ori);
return conteudo;
}
void gravarArquivo(const char* nomeArquivo, const char* conteudo)
{
FILE* arquivo = fopen(nomeArquivo, "w");
if (arquivo == NULL) {
printf("Erro ao abrir o arquivo.\n");
return;
}
fprintf(arquivo, "%s", conteudo);
fclose(arquivo);
}
void mostrarTexto(const char* nomeArquivo)
{
char* texto = lerArquivo(nomeArquivo);
if (texto != nullptr) {
printf("\nTexto:\n%s\n", texto);
delete[] texto;
}
}
int main()
{
int opcao;
const char* nomeArquivo = "texto.txt";
do {
printf("Menu\n");
printf("1 - Criptografar texto\n");
printf("2 - Descriptografar texto\n");
printf("3 - Mostrar texto\n");
printf("9 - Sair\n");
printf("Opcao: ");
scanf("%d", &opcao);
switch (opcao) {
case 1: {
int chave = 3;
char* texto = lerArquivo(nomeArquivo);
if (texto != nullptr) {
criptografarTexto(texto, chave);
printf("\nTexto criptografado:\n%s\n", texto);
gravarArquivo(nomeArquivo, texto);
delete[] texto;
}
break;
}
case 2: {
int chave = 3;
char* texto = lerArquivo(nomeArquivo);
if (texto != nullptr) {
descriptografarTexto(texto, chave);
printf("\nTexto descriptografado:\n%s\n", texto);
gravarArquivo(nomeArquivo, texto);
delete[] texto;
}
break;
}
case 3:
mostrarTexto(nomeArquivo);
break;
case 9:
printf("Saindo...\n");
break;
default:
printf("Opcao invalida. Tente novamente.\n");
break;
}
} while (opcao != 9);
return 0;
}