[go: up one dir, main page]

0% found this document useful (0 votes)
9 views12 pages

Crypto Lab Assessment 01

The document contains code implementations for three different encryption algorithms: Caesar Cipher, Playfair Cipher, and Vigenere Cipher. Each section includes the code for the algorithm, input handling, and the output of the encrypted and decrypted texts. The code is written in C++ and demonstrates how to prepare the plaintext, generate key matrices, and perform encryption and decryption.

Uploaded by

Shirsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

Crypto Lab Assessment 01

The document contains code implementations for three different encryption algorithms: Caesar Cipher, Playfair Cipher, and Vigenere Cipher. Each section includes the code for the algorithm, input handling, and the output of the encrypted and decrypted texts. The code is written in C++ and demonstrates how to prepare the plaintext, generate key matrices, and perform encryption and decryption.

Uploaded by

Shirsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

LAB ASSESSMENT – 1

NAME: SHIRSH HARISHANKAR BHAKTA


REG NO: 22BCE3527

Q.1] CAESER CIPHER


CODE:
#include<iostream>
using namespace std;
int main(){
int n;
string pt,ct="",dt="";
cin>>pt>>n;

for(int i =0; i < pt.length(); i++){


if(pt[i]>='a' && pt[i]<='z'){
ct+=(pt[i] -'a' + n)%26 + 'a';
dt+=(ct[i] -'a' - n)%26 + 'a';
}
else if(pt[i]>='A' && pt[i]<='Z'){
ct+=(pt[i] -'A' + n)%26 + 'A';
dt+=(ct[i] -'A' - n)%26 + 'A';
}
}

cout<<ct<<endl<<dt;

return 0;
}
OUTPUT:
Q.2] PLAYFAIR CIPHER
CODE:
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;

// Function to prepare the 5x5 key matrix


void prepareKeyMatrix(string key, vector<vector<char>>& keyMatrix) {
string tempKey = "";
bool charExists[26] = {false};

// Remove duplicate characters and ignore 'J'


for (char ch : key) {
ch = toupper(ch);
if (ch == 'J') ch = 'I';
if (!charExists[ch - 'A'] && isalpha(ch)) {
tempKey += ch;
charExists[ch - 'A'] = true;
}
}

// Add the remaining characters (A-Z except J)


for (char ch = 'A'; ch <= 'Z'; ++ch) {
if (!charExists[ch - 'A'] && ch != 'J') {
tempKey += ch;
}
}
// Fill the key matrix
int k = 0;
for (int i = 0; i < 5; ++i) {
vector<char> row;
for (int j = 0; j < 5; ++j) {
row.push_back(tempKey[k++]);
}
keyMatrix.push_back(row);
}
}

// Function to find the position of a character in the key matrix


void findPosition(const vector<vector<char>>& keyMatrix, char ch, int& row, int& col) {
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
if (keyMatrix[i][j] == ch) {
row = i;
col = j;
return;
}
}
}
}
// Function to prepare the plaintext for encryption
string preparePlaintext(string plaintext) {
string preparedText = "";
for (char ch : plaintext) {
if (isalpha(ch)) {
ch = toupper(ch);
if (ch == 'J') ch = 'I'; // Treat J as I
preparedText += ch;
}
}

// Insert filler 'X' between duplicate letters and make length even
for (size_t i = 0; i < preparedText.length() - 1; i += 2) {
if (preparedText[i] == preparedText[i + 1]) {
preparedText.insert(i + 1, "X");
}
}
if (preparedText.length() % 2 != 0) {
preparedText += 'X';
}
return preparedText;
}

// Function to encrypt the plaintext using the Playfair cipher


string encrypt(string plaintext, const vector<vector<char>>& keyMatrix) {
string ciphertext = "";
for (size_t i = 0; i < plaintext.length(); i += 2) {
char first = plaintext[i];
char second = plaintext[i + 1];
int row1, col1, row2, col2;

findPosition(keyMatrix, first, row1, col1);


findPosition(keyMatrix, second, row2, col2);

if (row1 == row2) {
// Same row: shift columns right
ciphertext += keyMatrix[row1][(col1 + 1) % 5];
ciphertext += keyMatrix[row2][(col2 + 1) % 5];
} else if (col1 == col2) {
// Same column: shift rows down
ciphertext += keyMatrix[(row1 + 1) % 5][col1];
ciphertext += keyMatrix[(row2 + 1) % 5][col2];
} else {
// Rectangle: swap columns
ciphertext += keyMatrix[row1][col2];
ciphertext += keyMatrix[row2][col1];
}
}
return ciphertext;
}

int main() {
string plaintext, key;

// Input plaintext and key


getline(cin, plaintext);
getline(cin, key);

// Prepare the key matrix


vector<vector<char>> keyMatrix;
prepareKeyMatrix(key, keyMatrix);

// Prepare the plaintext


string modifiedPlaintext = preparePlaintext(plaintext);
string nm="";
for (char ch : modifiedPlaintext) {
nm += tolower(ch);
}

// Encrypt the plaintext


string ciphertext = encrypt(modifiedPlaintext, keyMatrix);

// Output results
cout << ciphertext << endl;
cout << nm << endl;

return 0;
}
OUTPUT:
Q.3] VIGENERE CIPHER
CODE:
#include <bits/stdc++.h>
using namespace std;
string generateKey(string str, string key){
int x = str.size();
for (int i = 0;; i++) {
if (x == i)
i = 0;
if (key.size() == str.size())
break;
key.push_back(key[i]);
}
return key;
}
string cipherText(string str, string key)
{
string cipher_text;
for (int i = 0; i < str.size(); i++) {
// converting in range 0-25
char x = (str[i] + key[i]) % 26;
// convert into alphabets(ASCII)
x += 'A';

cipher_text.push_back(x);
}
return cipher_text;
}

string originalText(string cipher_text, string key)


{
string orig_text;

for (int i = 0; i < cipher_text.size(); i++) {


// converting in range 0-25
char x = (cipher_text[i] - key[i] + 26) % 26;
// convert into alphabets(ASCII)
x += 'A';
orig_text.push_back(x);
}
return orig_text;
}
int main()
{
string str;
string keyword;
cin>>str>>keyword;
if (any_of(str.begin(), str.end(), ::islower))
transform(str.begin(), str.end(), str.begin(),
::toupper);
if (any_of(keyword.begin(), keyword.end(), ::islower))
transform(keyword.begin(), keyword.end(),
keyword.begin(), ::toupper);

string key = generateKey(str, keyword);


string cipher_text = cipherText(str, key);

cout << cipher_text << "\n";

cout << originalText(cipher_text, key);


return 0;
}

OUTPUT:

You might also like