You are on page 1of 6

Cryptography and Network Security 21BECE30007

Practical – 1 : Implement the Caesar cipher with variable key.

Encrypted :-
#include<iostream>
using namespace std;

int main(){
int i,j,k;
string s,t;
int key;
cout<<"Enter the key\n";
cin>>key;
cout<<"Enter the message\n";
cin>>s;

for(i=0;i<s.size();i++){
t+=(s[i]-'A'+key)%26+'A';
}
cout<<"\n\n Encrypted message is "<<t<<'\n';
return 0;
}

Output:-
Enter the key :- 3
Enter the message :- program
Encrypted message is YAXPAJV

Page | 1
Cryptography and Network Security 21BECE30007

Decrypted :-
#include<iostream>
using namespace std;

int main(){
int i,j,k;
string s,t;
int key;
cout<<"Enter the key\n";
cin>>key;
cout<<"Enter the message to decrypt\n";
cin>>s;

for(i=0;i<s.size();i++){
t+=(s[i]-'A'-key+26)%26+'A';
}
cout<<"\n\nDecrypted message is "<<t<<'\n';
return 0;
}

Output:-
Enter the key
3
Enter the message to decrypt
YAXPAJV
Decrypted message is Program

Page | 2
Cryptography and Network Security 21BECE30007

Practical – 2 : Implement the brute-force (exhaustive key search) attack on


Caesar Cipher. Encrypted :-
#include<iostream>
using namespace std;
int main(){
char message[100], ch;
int i, key;
cout << "Enter a message to encrypt: ";
cin.getline(message, 100);
cout << "Enter key: ";
cin >> key;
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}}
cout << "Encrypted message: " << message;
return 0; }

Output:-
Enter a message to encrypt: network
Enter key: 4
Encrypted message: rixasvo

Page | 3
Cryptography and Network Security 21BECE30007

Decrypted:-
#include<iostream>
using namespace std;
int main(){
char message[100], ch;
int i, key;
cout << "Enter a message to decrypt: ";
cin.getline(message, 100);
cout << "Enter key: ";
cin >> key;
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch > 'a'){
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;
}}
cout << "Decrypted message: " << message;
return 0; }

Output:-
Enter a message to decrypt: rixasvo
Enter key: 4
Decrypted message: network

Page | 4
Cryptography and Network Security 21BECE30007

Practical – 5 : Implement the rail fence cipher with variable fence.


Encrypted:-
#include <bits/stdc++.h>
using namespace std;
int main() {
string plain , cipher , cipher_odd;
cin>>plain;
int n = plain.size();
for(int i = 0; i < n; i++){
if(i % 2 == 0) cipher += plain[i];
else cipher_odd += plain[i];
}
cipher.append(cipher_odd);
cout<<cipher;
return 0;
}

Output:-
cipher
cpeihr

Page | 5
Cryptography and Network Security 21BECE30007

Decrypted:-
#include <iostream>
#include <string>
using namespace std;
string decrypt(string cipher) {
string plain;
int n = cipher.size();
int half = n / 2;
for (int i = 0; i < half; i++) {
plain += cipher[i];
plain += cipher[half + i];
}
if (n % 2 == 1)
plain += cipher[n - 1];
return plain;
}
int main() {
string encrypted, decrypted;
cout << "Enter encrypted string: ";
cin >> encrypted;
decrypted = decrypt(encrypted);
cout << "Decrypted string: " << decrypted << endl;
return 0;
}

Output:-
Enter encrypted string: cpeihr
Decrypted string: cipher

Page | 6

You might also like