-
Notifications
You must be signed in to change notification settings - Fork 0
/
versao3.cpp
119 lines (97 loc) · 2.71 KB
/
versao3.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
#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);
}
int main()
{
int opcao;
char* texto;
do {
printf("Selecione a opcao:\n");
printf("1. Criptografar texto\n");
printf("2. Descriptografar texto\n");
printf("3. Sair\n");
printf("Opcao: ");
scanf("%d", &opcao);
switch (opcao) {
case 1: {
int chave;
printf("Digite a chave de criptografia: ");
scanf("%d", &chave);
texto = lerArquivo("texto.txt");
if (texto != nullptr) {
criptografarTexto(texto, chave);
printf("\nTexto criptografado:\n%s\n", texto);
gravarArquivo("texto.txt", texto);
delete[] texto;
}
break;
}
case 2: {
int chave;
printf("Digite a chave de descriptografia: ");
scanf("%d", &chave);
texto = lerArquivo("texto.txt");
if (texto != nullptr) {
descriptografarTexto(texto, chave);
printf("\nTexto descriptografado:\n%s\n", texto);
gravarArquivo("texto.txt", texto);
delete[] texto;
}
break;
}
case 3:
printf("Saindo...\n");
break;
default:
printf("Opcao invalida. Tente novamente.\n");
break;
}
} while (opcao != 3);
return 0;
}