Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.7 kB
0
Indexable
Never
#include <iostream>
#include <fstream>
#include <string>
// sign in
using namespace std;

string encryptPassword(const string& password);
string decryptPassword(const string& encryptedPassword);
bool seqSearch(const string& username, string& storedPassword);

int main() {
	 ifstream inFile("UserAccounts.txt");
    while (true) {
        string username, password;

        // get username
        cout << "Enter your username: ";
        cin >> username;

        // validation if merong username, kapag meron tutuloy
        string storedPassword;
        if (seqSearch(username, storedPassword)) {
            // ayan password na hinihingi
            cout << "Enter your password: ";
            cin >> password;

            // decryption
            string decryptedPassword = decryptPassword(storedPassword);

            // compare password sa decrypted
            if (password == decryptedPassword) {
                cout << "Sign-in successful!" << endl;
            } else {
                cerr << "Incorrect password. Please try again." << endl;
            }
        } else {
            cerr << "Username not found. Please sign up or check your username." << endl;
        }

        // Ask if the user wants to sign in again
        char choice;
        cout << "Do you want to sign in again? (y/n): ";
        cin >> choice;

        if (choice != 'y' && choice != 'Y') {
            break;
        }
    }

    return 0;
}

//check if username exists
bool seqSearch(const string& username, string& storedPassword) {
    ifstream inFile("UserAccounts.txt");

    if (!inFile) {
        cerr << "Error opening file for reading." << endl;
        exit(1);
    }

    string storedUsername;
    while (inFile >> storedUsername) {
        //based sa user input
        if (storedUsername == username) {
            inFile >> storedPassword; //password retrieval
            inFile.close();
            return true; // Username found
        }

    
    }

    inFile.close();
    return false; // Username not found
}

// encryption
string encryptPassword(const string& password) {
    
    string encryptedPassword = password;
    for (size_t i = 0; i < encryptedPassword.length() / 2; ++i) {
        swap(encryptedPassword[i], encryptedPassword[encryptedPassword.length() - i - 1]);
    }
    return encryptedPassword;
}

// decryption
string decryptPassword(const string& encryptedPassword) {
  
    string decryptedPassword = encryptedPassword;
    for (size_t i = 0; i < decryptedPassword.length() / 2; ++i) {
        swap(decryptedPassword[i], decryptedPassword[decryptedPassword.length() - i - 1]);
    }
    return decryptedPassword;
}
Leave a Comment