-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptoRSA.cpp
More file actions
154 lines (138 loc) · 4.61 KB
/
CryptoRSA.cpp
File metadata and controls
154 lines (138 loc) · 4.61 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
#include "arquivo.h"
#include "dicionario.h"
#include "mensagem.h"
#include "dirtools.h"
//#include "C:\Perl64\lib\CORE\EXTERN.h"
//#include "C:\Perl64\lib\CORE\perl.h" <--- do note do Panak
#include "/usr/lib/x86_64-linux-gnu/perl/5.22/CORE/EXTERN.h"
#include "/usr/lib/x86_64-linux-gnu/perl/5.22/CORE/perl.h"
//#include "/usr/lib/perl5/core_perl/CORE/EXTERN.h"
//#include "/usr/lib/perl5/core_perl/CORE/perl.h"
//#include "EXTERN.h"
//#include "perl.h"
using namespace std;
int main(int argc, char **argv, char **env)
{
string opcaoMenu, nomeDoArquivo, senha, nomeDoDicionario;
string melhorSenha;
int numMatches, melhorMatches, tamanhoChave, carry;
while (opcaoMenu != "6"){
// cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
cout << "Bem vindo ao CryptoRSA!" << endl;
cout << "(1) Listar todos os arquivos que se encontram no mesmo diretorio do programa" << endl;
cout << "(2) Abrir e criptografar um arquivo especificado" << endl;
cout << "(3) Abrir e descriptografar um arquivo especificado" << endl;
cout << "(4) Tentar quebrar a senha de um arquivo criptografado" << endl;
cout << "(5) Ler um arquivo e imprimir seu conteudo na tela" << endl;
cout << "(6) Sair" << endl;
cin >> opcaoMenu;
if (opcaoMenu=="1"){
const string caminhoAtual("./");
dirtools meuDiretorio(caminhoAtual);
meuDiretorio.mostrarTxts();
}
if (opcaoMenu=="2"){
cout << "Digite o nome do arquivo" << endl;
cin >> nomeDoArquivo;
mensagem myMsg(nomeDoArquivo, argc, argv, env);
cout << "Digite o tamanho da chave:" << endl;
cin >> tamanhoChave;
myMsg.criptografar(tamanhoChave);
return 0;
}
if (opcaoMenu=="3"){
cout << "Digite o nome do arquivo a ser descriptografado" << endl;
cin >> nomeDoArquivo;
mensagem myMsg(nomeDoArquivo, argc, argv, env);
myMsg.descriptografar();
return 0;
}
if (opcaoMenu=="4"){
string palavra;
senha="a";
int tamMaxSenha=5;
cout << "Digite o nome do arquivo que contém o dicionário de palavras" << endl;
cin >> nomeDoDicionario;
dicionario meuDic(nomeDoDicionario);
melhorMatches = 0;
if (meuDic.aberto==1){
cout << "Digite o nome do arquivo a tentar ser descriptografado" << endl;
cin >> nomeDoArquivo;
mensagem myMsg(nomeDoArquivo, argc, argv, env);
melhorSenha='a';
carry=1;
cout << "Digite o numero maximo de caracteres a serem tentados para a senha:" <<endl;
cin >> tamMaxSenha;
while (senha.length() < tamMaxSenha){
// incrementa a string senha;
//cout << senha << endl;
for (int i=senha.length()-1; i>=0; i--){
if (carry==1){
if (senha[i]=='z'){
senha[i]='a';
carry=1;
}
else {
senha[i]=senha[i] + 1;
carry=0;
}
}
}
if (carry==1){
senha = "a" + senha;
}
carry=1;
////////////////////////////
//tenta descriptografar com ela;
myMsg.descriptografar();
//verifica cada palavra do output, e chama o verificaMatch()
arquivo tentativaSenha ("output"+nomeDoArquivo);
tentativaSenha.abrir("r");
numMatches=0;
while (tentativaSenha.ArqInput >> palavra){
if (meuDic.verificaMatch(palavra)){
numMatches++;
}
}
tentativaSenha.fechar("r");
////////////////////////////////////
//Se muitos outputs, atualiza melhor senha, e trouls
if (numMatches>melhorMatches) {
melhorMatches = numMatches;
melhorSenha = senha;
}
}
cout << "A senha com maior numero de matches com o dicionario fornecido foi:" << endl;
cout << melhorSenha <<endl;
cout << "Com " << melhorMatches << " matches." << endl;
}
else {
cout << "Falha ao abrir o arquivo de dicionário!" << endl;
}
}
if (opcaoMenu=="5"){
cout << "Digite o nome do arquivo a ser lido" << endl;
cin >> nomeDoArquivo;
arquivo leitura(nomeDoArquivo);
if (leitura.abrir("r")){
leitura.ArqInput.seekg(0, std::ios::end);
int tamanhoArquivo = leitura.ArqInput.tellg();
leitura.ArqInput.seekg(0, std::ios::beg);
char *buffer = new char[tamanhoArquivo+1];
leitura.ArqInput.read (buffer, tamanhoArquivo);
string lido=buffer;
cout << "Conteudo do arquivo:" << endl;
cout << lido<< endl;
cout << "Fim do conteudo do arquivo." << endl;
}
else {
cout << "Falha ao abrir o arquivo! Tente novamente" << endl;
}
}
}
return 0;
}