Instantly share code, notes, and snippets.

What would you like to do?
This program is a random encryption model (REM).
The user is first asked to create a username with a password.
The password is randomly encrypted in a values file, and a keys file stores the matching encryption key.
Then the user is asked for a username. The program will check the password he enters matches this username.
REM was designed by Damien Lefebvre in France. Copyright © 2016.
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
string ask_username();
string ask_password();
template <typename T>
string make_str (T input);
bool username_exists(string& username);
int main() {
//part 1
cout << "Create your profile." << endl;
bool used;
bool statement = false;
string username;
do {
if (statement)
cout << "This username already exists." << endl;
username = ask_username();
used = username_exists(username);
statement = true;
} while (used);
//opening the keys file
ofstream keys_file("keys.txt", ofstream::app);
//adding the user to the keys file
keys_file << username << endl;
//opening the values file
ofstream values_file("values.txt", ofstream::app);
//adding the user to the values file
values_file << username << endl;
//setting this user's password
string password = ask_password();
//we initialize an input string stream with the password string
istringstream iss1(password);
int number_of_char = 500;
int rand_num;
char rand_char;
vector<string> file_content;
//fills the file content vector with random numbers, leaving place for the password
for (size_t i=0; i < number_of_char - password.size(); ++i) {
rand_num = rand() % (122-48+1) + 48; //choosing characters in the range of numbers and letters, 48 to 122
rand_char = (char) rand_num;
char password_char;
size_t rand_val_pos;
vector<int> keys;
//randomyl inserting every character of the password in the file content vector
//pushing back their position into the keys vector
while (iss1 >> password_char) {
rand_val_pos = rand() % number_of_char;
file_content.insert(begin(file_content)+rand_val_pos, make_str(password_char));
//printing every item of the keys vector into the keys file
for (auto& item : keys) {
keys_file << item << "\t";
keys_file << endl;
//printing every item of the file content vector into the values file
for (auto& item : file_content) {
values_file << item;
values_file << endl;
//part 2
cout << "Open a profile." << endl;
bool unfound;
//resetting the statement
statement = false;
//do-while loop to retrieve username and check it already exists
do {
if (statement)
cout << "This username does not exist, please try again." << endl;
username = ask_username();
unfound = !username_exists(username);
statement = true;
} while (unfound);
bool unsuccessful;
//do-while loop to retrieve password and check it matches with the username
do {
unsuccessful = true;
password = ask_password();
ifstream reading_keys_file("keys.txt", ofstream::in);
string keys_file_item;
int key;
vector<int> keys_vector;
while (reading_keys_file >> keys_file_item) {
if (keys_file_item == username) {
while (reading_keys_file >> key) {
reverse(begin(keys_vector), end(keys_vector));
string values_file_item;
char value;
vector<char> values_vector;
ifstream reading_values_file("values.txt", ofstream::in);
while (reading_values_file >> values_file_item) {
if (values_file_item == username) {
//transfering the content to the values vector
for (size_t i=0; i<number_of_char; ++i) {
reading_values_file >> value;
string potential_password;
size_t temp_key;
for (auto temp_key : keys_vector) {
potential_password += values_vector[temp_key];
reverse(begin(potential_password), end(potential_password));
if (potential_password == password) {
cout << "Welcome, " << username << "." << endl;
unsuccessful = false;
else {
cout << "Incorrect password, please try again." << endl;
} while (unsuccessful);
return 0;
string ask_username() {
string temp_username;
cout << "Enter your username: ";
cin >> temp_username;
return temp_username;
string ask_password() {
string temp_password;
cout << "Enter your password: ";
cin >> temp_password;
return temp_password;
template <typename T>
string make_str (T input) {
stringstream ss;
ss << input;
return ss.str();
bool username_exists(string& username) {
bool result = false;
string check_item;
ifstream check_keys_file("keys.txt", ofstream::in);
//reading through the keys file
while (check_keys_file >> check_item) {
if (check_item == username) {
result = true;
return result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment