Professional Documents
Culture Documents
Crypto Lab 21124028
Crypto Lab 21124028
CRYPTOGRAPHY LAB
B.TECH IT- III YEAR (5th SEMESTER) ITPC- 327
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;
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;
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;
}
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;
OUTPUT :