#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const std::string alphabet = "abcdefghijklmnopqrstuvwxyz0123456789 ";
std::size_t find_index(char c)
{
return std::distance(alphabet.begin(), std::find(alphabet.begin(), alphabet.end(), c));
}
std::string encrypt(const std::string& s, const std::string& passphrase)
{
std::string res;
for (std::size_t i = 0; i != s.size(); ++i) {
auto new_index = (find_index(s[i]) + find_index(passphrase[i % passphrase.size()])) % alphabet.size();
res += alphabet[new_index];
}
return res;
}
std::string decrypt(const std::string& s, const std::string& passphrase)
{
std::string res;
for (std::size_t i = 0; i != s.size(); ++i) {
auto new_index = (alphabet.size() + find_index(s[i]) - find_index(passphrase[i % passphrase.size()])) % alphabet.size();
res += alphabet[new_index];
}
return res;
}
std::string create_passphrase(std::size_t n)
{
std::string res;
for (std::size_t i = 0; i != n; ++i) {
res += alphabet[rand() % alphabet.size()];
}
return res;
}
int main()
{
string abecedario = "abcdefghijklmnopqrstuvwxyz0123456789 "; //la cadena que almacena el abecedario predeterminado
string mensaje; //la cadena que almacenara el mensaje
cout << "Ingrese el mensaje: " << endl;
getline(cin, mensaje); //usamos el comando getline para ingresar el mensaje para tener el espacio tambien
srand(time(NULL));
const auto& passphrase = create_passphrase(mensaje.length());
cout << "La clave es: " << endl << passphrase << endl;
string mensajeCifrado = encrypt(mensaje, passphrase);
cout << "El mensaje cifrado es: " << endl;
cout << mensajeCifrado;
cout << endl << "Este es el mensaje: " << endl;
cout << decrypt(mensajeCifrado, passphrase);
}