'Is there a way I can decrypt and encrypt by verifying that the values entered are equal to the variable "E" and "d" of my Function VoidAlgorithmRSA?
I just started 2 months ago
And this program will be part of my grades from one of my courses in college. My end goal is trying to apply the public key to encrypt a message and apply the private key to decrypt the same message. I've been thinking how to solve it but im just overthinking to much.
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <sstream>
using namespace std;
//Declaracion de funciones
void encrypt(int);
void decrypt(int);
void welcome();
string choose();
int numberKey();
void toLower(string & makeLower);
void goAgain();
void algoritmoRSA();
//Variables no globales
int main()
{
welcome();
string choiceNum = choose();
if (choiceNum == "claves") {
algoritmoRSA();
}
else if (choiceNum == "encriptar")
{
int keyNum = numberKey(); //Integra el valor para pasar a la siguiente funcion
// 1-36
encrypt(keyNum);
choiceNum.clear();
goAgain();
}
else if (choiceNum == "desencriptar") {
int keyNum = numberKey(); //Integra el valor para pasar a la siguiente funcion
// 1-36
decrypt(keyNum);
choiceNum.clear();;
goAgain();
}
else if (choiceNum == "exit") {
exit(0);
}
else {
choiceNum.clear();
cout << "\nError\n";
}
goAgain();
return 0;
}
void algoritmoRSA()
{
int p, q;
cout << endl;
cout << " ingresa p,q (p, q son numeros primos) " << endl;
cin >> p >> q;
int m = p * q;
int n = (p - 1) * (q - 1);
int E;
cout << endl;
cout << endl;
cout << " ingresa e (e y " << n << " son primos) y 1 < e < " << n << endl;
cin >> E;
int d;
for (d = 1;; d++)
{
if (d * E % n == 1)
break;
}
cout << endl << endl;
cout << "{ " << m << "," << E << " }" << " es la clave privada " << endl;
cout << "{ " << m << "," << d << " }" << " es la clave publica " << endl;
}
/* Function que muestra el mensaje de bienvenida al usuario
*
*/
void welcome() {
cout << "\n ******************************************* ";
cout << "\n * * ";
cout << "\n * ~~~~ Criptografia - Grupo 07 ~~~~ * ";
cout << "\n * * ";
cout << "\n * Autores: ";
cout << "\n * Jhames Patrick Fher Leon Chavez - u202118734 * ";
cout << "\n * Leandro Nahuel Perez Arteaga - u201911037 * ";
cout << "\n * Frank Edward Vegas García - u202118069 * ";
cout << "\n * Carlos Alberto Cardoza Najarro - u201819510 * ";
cout << "\n * Gustavo Alexander Cervantes Cahua - u20191a267* ";
cout << "\n * * ";
cout << "\n ******************************************* ";
cout << endl;
}
// Funcion que le permite al usuario la funcion que desea hacer // *returna userInput
string choose() {
try {
string userInput;
cout << endl;
cout << "Hola, por favor escoja la accion que desea realizar! (encriptar, desencriptar, claves, exit)" << endl;
cout << endl;
cin >> userInput;
while (userInput != "encriptar" && userInput != "desencriptar" && userInput != "claves" && userInput != "exit")
{
cout << "Usted ingreso una opcion incorrecta. \n";
cout << "Por favor intente de nuevo. \n";
cout << "Ingrese la opcion correcta: ";
cin.clear();
cin >> userInput;
}
return userInput; // encriptar, desencriptar, conocer las posibles claves o exit.
}
catch (exception e) {
cout << "La funcion escogida no podra ser ejecutada";
return NULL;
}
}
//Funcion que acepta una llave para el programa despues * retorna keyNum
int numberKey() {
try {
int keyNum; // Integra el valor para el input de la llave
// 1-36
cout << "Por favor ingrese un numero con el que desea encriptar o desencriptar el mensaje: ";
cin >> keyNum;
while (keyNum > 36 || keyNum < 0 || (!cin << keyNum))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Usted ingreso un numero invalido. \n";
cout << "Ingrese un numero apropiado para la llave que este entre el siguiente intervalo: (1-36)" << endl;
cin >> keyNum;
}
return keyNum;
}
catch (exception e) {
cout << "La funcion numero de llave no podra ser ejecutada";
return NULL;
}
}
// Function que encripta el mensaje basado en el mensaje del usuario
void encrypt(int keyNum)
{
try {
vector<string> library = { "a","b", "c", "d", "e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z", "0","1","2","3","4","5","6","7","8","9" };
string wordStorage; // String para guardar las letras minusculas
// Cualquier mensaje puede ser escrito
cout << "Por favor ingrese el mensaje que desea encriptar: ";
string getMessage; // Cualquier mensaje
cin.get();
getline(cin, getMessage);
cout << endl << "Su texto traducido es: ";
for (int i = 0; i < getMessage.size(); i++)
{
wordStorage = getMessage[i];
toLower(wordStorage);
for (int h = 0; h < library.size(); h++) {
if (library[h] == wordStorage && ((h + keyNum) < 36)) {
int y = h + keyNum;
cout << library[y];
}
else if (library[h] == wordStorage && ((h + keyNum) > 36)) {
int o = h + keyNum - 36;
cout << library[o];
}
else if (library[h] != wordStorage && h < 1) {
if (find(library.begin(), library.end(), wordStorage) != library.end())
cout << "";
else
cout << wordStorage;
}
}
}
cout << endl << endl;
}
catch (exception e) {
cout << " La funcion de encriptado no pudo ser ejecutada";
}
}
//Funcion que desencripta el mensaje que se evaluara con la llave
void decrypt(int keyNum)
{
try {
vector<string> library = { "a","b", "c", "d", "e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z", "0","1","2","3","4","5","6","7","8","9" };
string getMessage, wordStorage;
cout << "Por favor ingrese el mensaje a desecriptar: ";
string inputMessage;
cin.get();
getline(cin, getMessage);
cout << endl << "Su texto traducido es: ";
for (int i = 0; i < getMessage.size(); i++)
{
wordStorage = getMessage[i];
toLower(wordStorage);
for (int h = 0; h < library.size(); h++) {
if (library[h] == wordStorage && ((h - keyNum) < 36)) {
int y = h - keyNum; //int value muestra el valor si aparece en el vector
cout << library[y];
}
else if (library[h] == wordStorage && ((h - keyNum) > 36)) {
int o = h - keyNum + 36; // int muestra el valor si se enumera más allá de los limites de la matriz
cout << library[o];
}
else if (library[h] != wordStorage && h < 1) {
if (find(library.begin(), library.end(), wordStorage) != library.end())
cout << "";
else
cout << wordStorage; //Muestra char si no esta en la matriz
}
}
}
cout << endl;
}
catch (exception e) {
cout << "La funcion de desencriptar no podra ser ejecutada";
}
}
//Funcion que convierte strings a letras minusculas
void toLower(string & makeLower)
{
try {
for (int i = 0; i < makeLower.length(); i++) //Empieza desde 0 y sigue subiendo hasta que el largo del string (Incrementa por 1 loop)
{
makeLower[i] = tolower(makeLower[i]);//Obtiene el caracter de la cadena y usar la funcion prefabricada tolower de la biblioteca cctype
}
}
catch (exception e) {
cout << "Para bajar la funcion no se pudo implementar";
}
}
//Funcion que le pregunta al usuario si quiere hacer algo mas y corre el programa nuevamente de ser asi
void goAgain() {
try {
string choiceNumb = choose();
if (choiceNumb == "encriptar")
{
int keyNum = numberKey();
encrypt(keyNum);
goAgain();
}
else if (choiceNumb == "desencriptar") {
int keyNum = numberKey();
decrypt(keyNum);
goAgain();
}
else if (choiceNumb == "exit") {
exit(0);
}
else {
cout << "\nError\n";
}
goAgain();
}
catch (exception e) {
cout << " La funcion para volver a hacerlo no podra ser ejecutada";
}
}
Thanks for trying to help!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
