0% found this document useful (0 votes)
114 views

Crypto Lab Solution

The document discusses 5 encryption techniques: Caesar cipher, Vigenere cipher, simple substitution cipher, rail fence cipher, and Hill cipher. For each technique, source code in C++ or Python is provided to implement the encryption and decryption algorithms. The code takes a plaintext message and encryption key as input, applies the encryption algorithm, and outputs the ciphertext.

Uploaded by

prabinkarki
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)
114 views

Crypto Lab Solution

The document discusses 5 encryption techniques: Caesar cipher, Vigenere cipher, simple substitution cipher, rail fence cipher, and Hill cipher. For each technique, source code in C++ or Python is provided to implement the encryption and decryption algorithms. The code takes a plaintext message and encryption key as input, applies the encryption algorithm, and outputs the ciphertext.

Uploaded by

prabinkarki
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/ 19

1

1. Write a program to implement Caesar Cipher.

Source Code :

// A C++ program to illustrate Caesar Cipher Technique

#include <iostream>
using namespace std;

string encrypt(string text, int s)


{
string result = "";
for (int i=0;i<text.length();i++)
{
if (isupper(text[i]))
result += char(int(text[i]+s-65)%26 +65);

else
result += char(int(text[i]+s-97)%26 +97);
}

return result;
}
int main()
{
string text="PRABINKARKI";
int s = 4;
cout << "Text : " << text;
cout << "\nShift: " << s;
cout << "\nCipher: " << encrypt(text, s);
return 0;
}

Output :

Prabin Karki
2

2. Write a program to implement vigenere cipher.

Source Code :

// C++ code to implement Vigenere Cipher

#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++)


{
char x = (str[i] + key[i]) %26;

x += 'A';

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

string originalText(string cipher_text, string key)


{
string orig_text;

Prabin Karki
3

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


{
char x = (cipher_text[i] - key[i] + 26) %26;

x += 'A';
orig_text.push_back(x);
}
return orig_text;
}

int main()
{
string str = "PRABINKARKI";
string keyword = "SIMPLE";

string key = generateKey(str, keyword);


string cipher_text = cipherText(str, key);

cout << "Ciphertext : "


<< cipher_text << "\n";

cout << "Original/Decrypted Text : "


<< originalText(cipher_text, key);
return 0;
}

Output :

Prabin Karki
4

3. Write a program to implement simple substitution cipher.

Source Code :

# Python program to demonstrate


# Substitution Cipher

import string

dict1 = {}
key = 4

all_letters= string.ascii_letters

for i in range(len(all_letters)):
dict1[all_letters[i]] = all_letters[(i+key)%len(all_letters)]

plain_txt= "I am Prabin Karki"


cipher_txt=[]

for char in plain_txt:


if char in all_letters:
temp = dict1[char]
cipher_txt.append(temp)
else:
temp =char
cipher_txt.append(temp)

cipher_txt= "".join(cipher_txt)
print("Cipher Text is: ",cipher_txt)

dict2 = {}
for i in range(len(all_letters)):
dict2[all_letters[i]] = all_letters[(i-key)%(len(all_letters))]

decrypt_txt = []

for char in cipher_txt:


if char in all_letters:
temp = dict2[char]
decrypt_txt.append(temp)

Prabin Karki
5

else:
temp = char
decrypt_txt.append(temp)

decrypt_txt = "".join(decrypt_txt)
print("Recovered plain text :", decrypt_txt)

Output :

Prabin Karki
6

4. Write a program to implement railfence cipher.

Source Code :

// C++ program to illustrate Rail Fence Cipher


// Encryption and Decryption

#include <bits/stdc++.h>
using namespace std;

string encryptRailFence(string text, int key)


{
char rail[key][(text.length())];

for (int i=0; i < key; i++)


for (int j = 0; j < text.length(); j++)
rail[i][j] = '\n';

bool dir_down = false;


int row = 0, col = 0;

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


{
if (row == 0 || row == key-1)
dir_down = !dir_down;

rail[row][col++] = text[i];

dir_down?row++ : row--;
}

for (int i=0; i < key; i++)


for (int j=0; j < text.length(); j++)
if (rail[i][j]!='\n')
result.push_back(rail[i][j]);

return result;
}

string decryptRailFence(string cipher, int key)


{
char rail[key][cipher.length()];

Prabin Karki
7

for (int i=0; i < key; i++)


for (int j=0; j < cipher.length(); j++)
rail[i][j] = '\n';

bool dir_down;

int row = 0, col = 0;

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


{
if (row == 0)
dir_down = true;
if (row == key-1)
dir_down = false;

rail[row][col++] = '*';

dir_down?row++ : row--;
}

int index = 0;
for (int i=0; i<key; i++)
for (int j=0; j<cipher.length(); j++)
if (rail[i][j] == '*' && index<cipher.length())
rail[i][j] = cipher[index++];

string result;

row = 0, col = 0;
for (int i=0; i< cipher.length(); i++)
{
if (row == 0)
dir_down = true;
if (row == key-1)
dir_down = false;

if (rail[row][col] != '*')
result.push_back(rail[row][col++]);

dir_down?row++: row--;
}
return result;
}

Prabin Karki
8

int main()
{
cout << encryptRailFence("attack at once", 2) << endl;
cout << encryptRailFence("Prabinkarki ", 3) << endl;
cout << encryptRailFence("defend the east wall", 3) << endl;

cout << decryptRailFence("Hkit Kadns",3) << endl;


cout << decryptRailFence("atc toctaka ne",2) << endl;
cout << decryptRailFence("dnhaweedtees alf tl",3) << endl;

return 0;
}

Output :

Prabin Karki
9

5. Write a program to implement hill cipher.

Source Code :

// C++ code to implement Hill Cipher

#include <iostream>
using namespace std;

void getKeyMatrix(string key, int keyMatrix[][3])


{
int k = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
keyMatrix[i][j] = (key[k]) % 65;
k++;
}
}
}
void encrypt(int cipherMatrix[][1],
int keyMatrix[][3],
int messageVector[][1])
{
int x, i, j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 1; j++)
{
cipherMatrix[i][j] = 0;

for (x = 0; x < 3; x++)


{
cipherMatrix[i][j] +=
keyMatrix[i][x] * messageVector[x][j];
}

cipherMatrix[i][j] = cipherMatrix[i][j] % 26;


}
}
}

Prabin Karki
10

void HillCipher(string message, string key)


{
int keyMatrix[3][3];
getKeyMatrix(key, keyMatrix);

int messageVector[3][1];

for (int i = 0; i < 3; i++)


messageVector[i][0] = (message[i]) % 65;

int cipherMatrix[3][1];

encrypt(cipherMatrix, keyMatrix, messageVector);

string CipherText;

for (int i = 0; i < 3; i++)


CipherText += cipherMatrix[i][0] + 65;

cout << " Ciphertext:" << CipherText;


}

// Driver function for above code


int main()
{
string message = "ACT";

string key = "GYBNQKURP";

HillCipher(message, key);

return 0;
}

Output :

Prabin Karki
11

6. Write a program to compute GCD of two integers using Euclidean


algorithm.

Source Code :

// C++ program to demonstrate


// Basic Euclidean Algorithm

#include <bits/stdc++.h>
using namespace std;

int gcd(int a, int b)


{
if (a == 0)
return b;
return gcd(b % a, a);
}

int main()
{
int a = 10, b = 15;
cout << "GCD(" << a << ", "
<< b << ") = " << gcd(a, b)
<< endl;
a = 35, b = 10;
cout << "GCD(" << a << ", "
<< b << ") = " << gcd(a, b)
<< endl;
a = 31, b = 2;
cout << "GCD(" << a << ", "
<< b << ") = " << gcd(a, b)
<< endl;
return 0;
}

Output :

Prabin Karki
12

7. Write a program to check whether a number is multiplicative inverse


of another number or not using brute force approach.

Source Code :

// C++ program to find modular


// inverse of a under modulo m

#include <iostream>
using namespace std;

int modInverse(int a, int m)


{
for (int x = 1; x < m; x++)
if (((a%m) * (x%m)) % m == 1)
return x;
}

int main()
{
int a = 3, m = 11;

cout << modInverse(a, m);


return 0;
}

Output :

Prabin Karki
13

8. Write a program to implement Extended Euclidean algorithm.

Source Code :

//​C++ program to implement Extended Euclidean algorithm.

#include <bits/stdc++.h>
using namespace std;
int gcdExtended(int a, int b, int *x, int *y) {
if (a == 0) {
*x = 0;
*y = 1;
return b;
}
int x1, y1;
int gcd = gcdExtended(b%a, a, &x1, &y1);
*x = y1 - (b/a) * x1;
*y = x1;
return gcd;
}
int main() {
int x, y;
int a = 35, b = 15;
cout<<"gcd "<<gcdExtended(a, b, &x, &y);
return 0;
}

Output :

Prabin Karki
14

9. Write a program to check whether a given number is prime or not.

Source Code :

// C program to check if a
// number is prime

#include <stdio.h>
#include<math.h>
int main()
{
int n, i, flag = 1;

printf("Enter a number: \n");

scanf("%d", &n);

for (i = 2; i <= sqrt(n); i++) {

if (n % i == 0) {
flag = 0;
break;
}
}

if(n<=1)
flag=0;
else if(n==2)
flag=1;

if (flag == 1) {
printf("%d is a prime number", n);
}
else {
printf("%d is not a prime number", n);
}

return 0;
}

Prabin Karki
15

Output :

Prabin Karki
16

10.Write a program to perform primality checking using Rabin-Miller


algorithm.

Source Code :

// C++ program Miller-Rabin primality test

#include <bits/stdc++.h>
using namespace std;

int power(int x, unsigned int y, int p)


{
int res = 1;
x = x % p;

while (y > 0)
{
if (y & 1)
res = (res*x) % p;

y = y>>1;
x = (x*x) % p;
}
return res;
}

bool miillerTest(int d, int n)


{
int a = 2 + rand() % (n - 4);

int x = power(a, d, n);

if (x == 1 || x == n-1)
return true;

while (d != n-1)
{
x = (x * x) % n;
d *= 2;

if (x == 1) return false;
if (x == n-1) return true;
}

Prabin Karki
17

return false;
}

bool isPrime(int n, int k)


{
if (n <= 1 || n == 4) return false;
if (n <= 3) return true;

int d = n - 1;
while (d % 2 == 0)
d /= 2;

for (int i = 0; i < k; i++)


if (!miillerTest(d, n))
return false;

return true;
}

int main()
{
int k = 4;

cout << "All primes smaller than 100: \n";


for (int n = 1; n < 100; n++)
if (isPrime(n, k))
cout << n << " ";

return 0;
}

Output :

Prabin Karki
18

11.Write a program to compute Totient of a number.

Source Code :

// A simple C++ program to calculate


// Euler's Totient Function

#include <iostream>
using namespace std;

int gcd(int a, int b)


{
if (a == 0)
return b;
return gcd(b % a, a);
}

int phi(unsigned int n)


{
unsigned int result = 1;
for (int i = 2; i < n; i++)
if (gcd(i, n) == 1)
result++;
return result;
}

int main()
{
int n;
for (n = 1; n <= 10; n++)
cout << "phi("<<n<<") = " << phi(n) << endl;
return 0;
}

Prabin Karki
19

Output :

Prabin Karki

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy