DR B R Ambedkar National Institute of Technology, Jalandhar: Itpc-327 Cryptography Lab
DR B R Ambedkar National Institute of Technology, Jalandhar: Itpc-327 Cryptography Lab
DR B R Ambedkar National Institute of Technology, Jalandhar: Itpc-327 Cryptography Lab
Technology, Jalandhar
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;
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 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;
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;
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;
}
class claimant{
private:
long long int claimant_challenge;
string name;
public:
claimant(string name){
this->name = name;
}
void bidirectional_auth(){
cout<<"Enter Name of Claimant: ";
string s; cin>>s;
cout<<endl;
int main()
{bidirectional_auth();
return 0;
}
Output: