0% found this document useful (0 votes)
35 views17 pages

DR B R Ambedkar National Institute of Technology, Jalandhar: Itpc-327 Cryptography Lab

The document discusses implementing various authentication methods in C++ including: 1) Simple password based authentication that stores usernames and passwords in a text file and authenticates by matching inputs. 2) Hashed password authentication that hashes passwords before storage and authentication. 3) Salted hashed password authentication that adds a randomly generated salt string to each password before hashing and storage. 4) Nonce challenge-response authentication that generates a random number challenge, hashes it at the client and verifies the response. 5) Timestamp challenge-response authentication that likely incorporates timestamps into the challenge-response protocol.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views17 pages

DR B R Ambedkar National Institute of Technology, Jalandhar: Itpc-327 Cryptography Lab

The document discusses implementing various authentication methods in C++ including: 1) Simple password based authentication that stores usernames and passwords in a text file and authenticates by matching inputs. 2) Hashed password authentication that hashes passwords before storage and authentication. 3) Salted hashed password authentication that adds a randomly generated salt string to each password before hashing and storage. 4) Nonce challenge-response authentication that generates a random number challenge, hashes it at the client and verifies the response. 5) Timestamp challenge-response authentication that likely incorporates timestamps into the challenge-response protocol.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Dr B R Ambedkar National Institute of

Technology, Jalandhar

ITPC-327 CRYPTOGRAPHY LAB


B. TECH IT – 3rd YEAR (5th SEMESTER)

Dr Prateek

Submitted by
Submitted to: Name : Madhur Aggarwal
Roll No 21124062
Department of Information Technology
S.NO NAME OF THE EXPERIMENT SUBMISSION DATE SIGN
Simple Password Based Authentication

AIM:
Implementing Simple Password Based Authentication In C++

CODE:
#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
using namespace std;

void registerUser(){
cout<<"Registering A New User: "<<endl;
fstream newfile;
newfile.open("./basic_database.txt",ios::app);
if(newfile.is_open()){
string username, password;
cout<<"Enter Username: "; cin>>username;
cout<<"Enter Password: "; cin>>password;
newfile<<username<<" "<<password<<endl;
newfile.close();
}
}

bool checkUser(){
cout<<"Authenticating the user: "<<endl;
fstream newfile;
newfile.open("./basic_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==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:
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;
int N=1e9;

long long int generate_random(){


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

long long int hashfunction(long long int challenge){


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

void check_response(long long int challenge, long long int response){


cout<<endl<<"At Verifier Side: "<<endl;
if(response == hashfunction(challenge)){
cout<<"Claimant has been verified."<<endl;
}
else cout<<"Claimant Not Verified"<<endl;
}

long long int invoke_verifier(string name){


cout<<"Claimant "<<name<<" to be verified."<<endl;
return generate_random();
}

void claimant(string name){


long long int challenge = invoke_verifier(name);
cout<<endl<<"At Client Side: "<<endl;
cout<<"Challenge Received By Claimant: "<<challenge<<endl;
long long int response = hashfunction(challenge);
cout<<"Response Sent By Claimant: "<<response<<endl;
check_response(challenge,response);
}

int main()
{claimant("Madhur");
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("Madhur");
return 0;
}
Output:
Bidirectional Challenge – Response Auth.

AIM:
Implementing Bidirectional Challenge Response Authentication in C++

CODE:
#include <iostream>
#include <string>
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