You are on page 1of 19

Dr.

B R Ambedkar National Institute of Technology


Grand Trunk Road, Barnala - Amritsar Bypass Road,
Jalandhar, Punjab 144011

CRYPTOGRAPHY LAB
B.TECH IT- III YEAR (5th SEMESTER) ITPC- 327

Submitted To- Submitted By-


Dr. Parteek kumar ChhawinderPreet
Department of Information Technology 21124028
SIMPLE PASSWORD BASED AUTHENTICATION

AIM:
Implementing Simple Password Based Authentication In C++
Code :
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
int main()
{
// Create a map to store username-password pairs
map<string, string> credentials;
// Loop to input credentials for 20 students
int n;
cout << "enter number of students " << endl;
cin >> n;
for (int i = 0; i < n; ++i)
{
string username, password;

// Input username for student


cout << "Enter username for student " << i + 1 << ": ";
cin >> username;

// Input password for the entered username


cout << "Enter password for " << username << ": ";
cin >> password;

// Store the username-password pair in the map


credentials[username] = password;
}
// Open a file named "credentials.txt" for writing
ofstream outFile("credentials.txt");
if (outFile.is_open())
{
// Write the credentials to the file
for (const auto &pair : credentials)
{
outFile << pair.first << " " << pair.second << endl;
}
// Close the output file
outFile.close();
}
else
{
cout << "Error opening file for writing." << endl;
return 1;
}
// Open the same file "credentials.txt" for reading
ifstream inFile("credentials.txt");
if (!inFile.is_open())
{
cout << "Error opening file for reading." << endl;
return 1;
}
bool exitRequested = false;
while (!exitRequested)
{
string inputUsername, inputPassword;
bool authenticated = false;
// Input username from the user
cout << "Enter username (or 'exit' to quit): ";
cin >> inputUsername;
if (inputUsername == "exit")
{
exitRequested = true;
continue;
}
// Input password from the user
cout << "Enter password: ";
cin >> inputPassword;
string line;
string savedUsername, savedPassword;
// Loop through each line in the file
while (getline(inFile, line))
{
istringstream iss(line); // create a string stream from the line
// Parse the saved username and password from the line
iss >> savedUsername >> savedPassword; // extract values from the line

// Check if input username and password match any saved credentials


if (inputUsername == savedUsername && inputPassword == savedPassword)
{
authenticated = true;
break;
}
}
// Check if authentication was successful or not
if (authenticated)
{
cout << "User authenticated." << endl;
}
else
{
bool usernameMatched = false;
// Loop through each line in the file to check if the input username matches any saved
username
inFile.clear();
inFile.seekg(0);
while (getline(inFile, line))
{
istringstream iss(line);
iss >> savedUsername >> savedPassword;

if (inputUsername == savedUsername)
{
usernameMatched = true;
break;
}
}
if (usernameMatched)
{
if (inputPassword != savedPassword)
{
cout << "Password not matched." << endl;
}
else
{
cout << "Permission denied." << endl;
}
}
else
{
cout << "Username not recognized." << endl;
}
}
// Clear the stream and go back to the beginning of the file for the next attempt
inFile.clear();
inFile.seekg(0);
}
inFile.close();
return 0;
}

OUTPUT :
HASHED PASSWORD BASED AUTHENTICATION
AIM:
Implementing Hashed Password Based Authentication In C++
Code :
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string hashfunction(string password){
hash<string> hasher;
return to_string(hasher(to_string(hasher(password))));
}
void registerUser(){
cout<<"Registering A New User: "<<endl;
fstream newfile;
newfile.open("./basic_hashed_database.txt",ios::app);
if(newfile.is_open()){
string username, password;
cout<<"Enter Username: "; cin>>username;
cout<<"Enter Password: "; cin>>password;
newfile<<username<<" "<<hashfunction(password)<<endl;
cout<<"User Registerted."<<endl;
newfile.close();
}
}
bool checkUser(){
cout<<"Authenticating the user: "<<endl;
fstream newfile;
newfile.open("./basic_hashed_database.txt",ios::in);
if(newfile.is_open()){
string word;
bool username = true;
string input_username, input_password;
cout<<"Enter Username: "; cin>>input_username;
cout<<"Enter Password: "; cin>>input_password;

bool found=false;

while(newfile>>word){
if(username) {
username=!username;
if(word!=input_username){
found=false;
continue;
}
else found=true;
}
else{
username=!username;
if(!found){
continue;
}
else{
if(word==hashfunction(input_password)) return 1;
else return 0;
}
}
}
}
return false;
}

int main(){
cout<<"Program Codes: "<<endl;
cout<<"Use \n1 to Register New User\n2 to Validate Existing User\n0 to Exit"<<endl<<endl;

int input;
do{
cout<<"Enter Program Code: ";
cin>>input;
switch(input){
case 1: registerUser(); break;
case 2: if(checkUser()) cout<<"Correct UserName & Password "<<endl;
else cout<<"Incorrect Username Or Password "<<endl;
break;
case 0: cout<<"Program Terminated."<<endl; return 0;
default: cout<<"Incorrect Input, Enter again"<<endl; break;
}
cout<<endl;
}
while(input);
return 0;
}

OUTPUT :
SALTING HASHED PASSWORD BASED AUTHENTICATION
AIM:
Implementing Salting Hashed Password Based Authentication In C++
Code :
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string hashfunction(string password){
hash<string> hasher;
return to_string(hasher(to_string(hasher(password))));
}
string generateSalt(){
string ans="";
int x=rand()%50;
for(int i=0; i<x; i++){
for(int j=0; j<26; j++){
if(rand()%2 && rand()%2){
char ch = 'a'+j;
ans+= ch;
}
}
}
return ans;
}
void registerUser(){
cout<<"Registering A New User: "<<endl;
fstream newfile;
newfile.open("./salt_hashed_database.txt",ios::app);
if(newfile.is_open()){
string username, password;
string salt = generateSalt();
cout<<"Enter Username: "; cin>>username;
cout<<"Enter Password: "; cin>>password;
newfile<<username<<" "<<salt<<" "<<hashfunction(password+salt)<<endl;
cout<<"User Registerted."<<endl;
newfile.close();
}
}
bool checkUser(){
cout<<"Authenticating the user: "<<endl;
fstream newfile;
newfile.open("./salt_hashed_database.txt",ios::in);
if(newfile.is_open()){
string word;
string salt;
bool username = true, slt=false, pass=false;
string input_username, input_password;
cout<<"Enter Username: "; cin>>input_username;
cout<<"Enter Password: "; cin>>input_password;
bool found=false;
while(newfile>>word){
if(username) {
username=!username;
slt = !slt;
if(word!=input_username){
found=false;
continue;
}
else found=true;
}
else if(slt){
slt = !slt;
pass = !pass;
if(!found) continue;
salt = word;
}
else if(pass){
pass = !pass;
username=!username;

if(!found){
continue;
}
else{
if(word==hashfunction(input_password+salt)) return 1;
else return 0;
}
}
}
}
return false;
}
int main(){
cout<<"Program Codes: "<<endl;
cout<<"Use \n1 to Register New User\n2 to Validate Existing User\n0 to Exit"<<endl<<endl;
int input;
do{
cout<<"Enter Program Code: ";
cin>>input;
switch(input){
case 1: registerUser(); break;
case 2: if(checkUser()) cout<<"Correct UserName & Password "<<endl;
else cout<<"Incorrect Username Or Password "<<endl;
break;
case 0: cout<<"Program Terminated."<<endl; return 0;
default: cout<<"Incorrect Input, Enter again"<<endl; break;
}
cout<<endl;
}
while(input);
return 0;
}

OUTPUT :
NONCE CHALLENGE-RESPONSE BASED Auth.
AIM:
Implementing Nonce Challenge Response Based Authentication in C++
Code :
#include <iostream>
#include <string>
using namespace std;
// Function to perform XOR encryption on plaintext using a key
string encryptXOR(const string &plaintext, const string &key)
{
string ciphertext = plaintext;
for (size_t i = 0; i < plaintext.size(); ++i)
{
// XOR each character in plaintext with the corresponding character in the key
// Use modulus (%) to repeat the key if it's shorter than the plaintext
ciphertext[i] ^= key[i % key.size()];
}
return ciphertext;
}
// Function to perform XOR decryption on ciphertext using a key (reverses encryption)
string decryptXOR(const string &ciphertext, const string &key)
{
// XOR decryption is the same as encryption
return encryptXOR(ciphertext, key);
}
// Function to generate a random nonce (a random string)
string generateNonce()
{
string nonce(16, '\0'); // Initialize a string of 16 characters with null characters
for (int i = 0; i < 16; ++i)
{
// Fill the nonce with random characters (0-255)
nonce[i] = static_cast<char>(rand() % 256);
}
return nonce;
}

int main()
{
string key; // Symmetric key for encryption and decryption
cout << "Enter the symmetric key: " << endl;
cin >> key;
string clientname;
cout << "Enter the client name: " << endl;
cin >> clientname;
cout << "The client sends its name to the server: " << clientname << endl;
// Generate a random nonce
string serverNonce = generateNonce();
cout << "The randomly generated nonce is: " << endl;
cout << serverNonce << endl;
// Encrypt the nonce with the key
string encryptedNonce = encryptXOR(serverNonce, key);
cout << "Client: Nonce encypted: " << encryptedNonce << endl;
// Simulate receiving the encrypted nonce (in this case, just copying it)
string receivedNonce = encryptedNonce;
// Decrypt the received nonce using the key
string decryptedNonce = decryptXOR(receivedNonce, key);
cout << "server: Received and decrypted nonce: " << decryptedNonce << endl;
// Check if the decrypted nonce matches the original nonce
if (decryptedNonce == serverNonce)
{
cout << "Server: Nonce verified. Access granted." << endl;
}
else
{
cout << "Server: Nonce verification failed. Access denied." << endl;
}
return 0;
}
OUTPUT :
TIMESTAMP CHALLENGE-RESPONSE BASED Auth.
AIM:
Implementing TimeStamp Challenge Response Based Authentication in
C++
Code :
#include <iostream>
#include <string>
#include <ctime>
using namespace std;

long long int hashfunction(string challenge){


hash<string> hasher;
return (long long int)hasher(challenge);
}

void verifier(string name, string timestamp, long long int response){


cout<<endl<<"At Verifier Side: "<<endl;
if(hashfunction(timestamp)==response){
cout<<name<<" has been verified"<<endl;
}
else cout<<"Access Denied"<<endl;
}

void claimant(string name){


cout<<"To Verify "<<name<<endl;

cout<<endl<<"At Client Side: "<<endl;


time_t now = time(0);
string timestamp = to_string(now);
char* date_time = ctime(&now);
cout<<"The current date and time is: "<<date_time;
cout<<"The Timestamp is: "<<timestamp<<endl;

long long int response = hashfunction(timestamp);


cout<<"The Response Generated Is (hashed timestamp): "<<response<<endl;

verifier(name,timestamp,response);
}
int main()
{claimant("Saloni");
return 0;
}

OUTPUT :
BIDIRECTIONAL CHALLENGE-RESPONSE BASED Auth.
AIM:
Implementing BiDirectional Challenge Response Based Authentication
in C++
Code :

#include<bits/stdc++.h>
#include <string>
#include <math.h>
using namespace std;
int N=1e9;

class verifier{
private:
long long int verifier_challenge;
string claimant_name;

public:
verifier(string name){
claimant_name = name;
cout<<"Verifying "<<claimant_name<<endl;
}

long long int generate_challenge(){


srand(time(NULL));
long long int ans = rand()%N;
while(ans<INT_MAX){
ans*=(rand()%N);
}
verifier_challenge = ans;
return ans;
}

string generate_response(long long int Rc){


hash<string> hasher;
return to_string(hasher(to_string(Rc)));
}

string checkResponse(string s){


hash<string> hasher;
bool b = (s==to_string(hasher(to_string(verifier_challenge))));
if(b) return "Correct Response, Authenticated Successfully.";
else return "Incorrect Response, Access Denied.";
}
};

class claimant{
private:
long long int claimant_challenge;
string name;
public:
claimant(string name){
this->name = name;
}

long long int generate_challenge(){


srand(time(NULL));
long long int ans = rand()%N;
while(ans<INT_MAX){
ans*=(rand()%N);
}
claimant_challenge = ans;
return ans;
}

string generate_response(long long int Rv){


hash<string> hasher;
return to_string(hasher(to_string(Rv)));
}

string checkResponse(string s){


hash<string> hasher;
bool b= (s==to_string(hasher(to_string(claimant_challenge))));
if(b) return "Correct Response, Authenticated Successfully.";
else return "Incorrect Response, Access Denied.";
}
};

void bidirectional_auth(){
cout<<"Enter Name of Claimant: ";
string s; cin>>s;

auto Claimant= new claimant(s);


auto Verifier= new verifier(s);

cout<<"Challenge From Verifier To Claimant: ";


auto Rv = Verifier->generate_challenge();
cout<<Rv<<endl;

cout<<"Response From Verifier is: ";


auto ClaimantResponse = Claimant->generate_response(Rv);
cout<<ClaimantResponse<<endl;

cout<<"Checking Response From Claimint: ";


cout<<Verifier->checkResponse(ClaimantResponse)<<endl;

cout<<endl;

cout<<"Challenge From Claimant To Verifier: ";


auto Rc = Claimant->generate_challenge();
cout<<Rc<<endl;

cout<<"Response From Verifier Is: ";


auto VerifierResponse = Verifier->generate_response(Rc);
cout<<VerifierResponse<<endl;

cout<<"Checking Response From Verifier: ";


cout<<Claimant->checkResponse(VerifierResponse)<<endl;

cout<<endl<<"Hence, Both Claimant And Verifier Are Authenticated"<<endl;


}
int main()
{bidirectional_auth();
return 0;
}

OUTPUT :

You might also like