Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
Aim: To implement Caesar Cipher Encryption - Decryption.
Solution : (Caesar Cipher Encryption)
#include<stdio.h>
#include<ctype.h>
int main()
{
char text[500], ch;
int key;
// taking user input
printf("Enter a message to encrypt: ");
scanf("%s", text);
printf("Enter the key: ");
scanf("%d", & key);
// visiting character by character
for (int i = 0; text[i] != ' '; ++i) {
ch = text[i];
// check for valid character
if (isalnum(ch)) {
// lower case characters
if (islower(ch)) {
ch = (ch - 'a' + key) % 26 + 'a';
}
// uppercase characters
if (isupper(ch)) {
ch = (ch - 'A' + key) % 26 + 'A';
}
// numbers
if (isdigit(ch)) {
ch = (ch - '0' + key) % 10 + '0';
}
}
// invalid character
else {
printf("Invalid Message");
}
// adding encoded answer
text[i] = ch;
}
printf("Encrypted message: %s", text);
return 0;
}
Output
Solution : (Caesar Cipher Decryption)
#include<stdio.h>
#include<ctype.h>
int main()
{
char text[500], ch;
int key;
// taking user input
printf("Enter a message to decrypt: ");
scanf("%s", text);
printf("Enter the key: ");
scanf("%d", & key);
//visiting each character
for (int i = 0; text[i] != ' '; ++i) {
ch = text[i];
// check for valid characters
if (isalnum(ch)) {
// lower case characters
if (islower(ch)) {
ch = (ch - 'a' - key + 26) % 26 + 'a';
}
// uppercase characters
if (isupper(ch)) {
ch = (ch - 'A' - key + 26) % 26 + 'A';
}
// numbers
if (isdigit(ch)) {
ch = (ch - '0' - key + 10) % 10 + '0';
}
}
// invalid characters
else {
printf("Invalid Message");
}
// asding decoded character back
text[i] = ch;
}
printf("Decrypted message: %s", text);
return 0;
}
Output (Screenshot- Decryption):
PRACTICAL -2
Aim: To implement Mono-alphabetic Cipher Encryption – Decryption.
Solution (Mono-alphabetic Cipher Encryption & Decryption):
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define pf printf
#define sf scanf
char dc[26]="XDGSZANYOBTMJCEVFHKWPLQURI";
char ec[26]="abcdefghijklmnopqrstuvwxyz";
void e(char*);
void d(char*);
void main(){
char *p;
clrscr();
pf("nnEnter plain text:");
sf("%s",p);
pf("nAfter Encryption:n-----------------n");
e(p);
pf("n%s",p);
pf("nAfter Decryption:n-----------------n");
d(p);
pf("n%s",p);
getch();
}
void e(char *p){
int l=0;
while(*(p+l) != ' '){
*(p+l)=dc[*(p+l)-97];
l++;
}
}
void d(char *p){
int l=0;
while(*(p+l) != ' '){
int i;
for(i=0;i<26;i++){
if(dc[i]==*(p+l))
break;
}
*(p+l)=ec[i];
l++;
}
}
Output (Screenshot):
PRACTICAL -3
Aim: To implement Hill Cipher Encryption.
#include<stdio.h>
#include<string.h>
int main() {
unsigned int a[3][3] = { { 6, 24, 1 }, { 13, 16, 10 }, { 20, 17, 15 } };
unsigned int b[3][3] = { { 8, 5, 10 }, { 21, 8, 21 }, { 21, 12, 8 } };
int i, j;
unsigned int c[20], d[20];
char msg[20];
int determinant = 0, t = 0;
;
printf("Enter plain textn ");
scanf("%s", msg);
for (i = 0; i < 3; i++) {
c[i] = msg[i] - 65;
printf("%d ", c[i]);
}
for (i = 0; i < 3; i++) {
t = 0;
for (j = 0; j < 3; j++) {
t = t + (a[i][j] * c[j]);
}
d[i] = t % 26;
}
printf("nEncrypted Cipher Text :");
for (i = 0; i < 3; i++)
printf(" %c", d[i] + 65);
for (i = 0; i < 3; i++) {
t = 0;
for (j = 0; j < 3; j++) {
t = t + (b[i][j] * d[j]);
}
c[i] = t % 26;
}
printf("nDecrypted Cipher Text :");
for (i = 0; i < 3; i++)
printf(" %c", c[i] + 65);
return 0;
}
Output (Screenshot):
PRACTICAL -4
Aim: To implement Poly-alphabetic Cipher (Vigener Cipher) Technique.
Solution:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char pt[20]={' '},ct[20]={' '},key[20]={' '},rt[20]={' '};
int i,j;
clrscr();
printf("n enter the plain text:");
scanf("%s",pt);
printf("n enter the key:");
scanf("%s",key);
//length of plaintext equal to length of key
j=0;
for(i=strlen(key);i<strlen(pt);i++)
{
if(j==strlen(key))
{
j=0;
}
key[i]=key[j];
j++;
}
printf("n new key is:%s",key);
//converting plain text to cipher text (encryption)
for(i=0;i<strlen(pt);i++)
{
ct[i]=(((pt[i]-97)+(key[i]-97))%26)+97;
}
printf("n n cipher text is:%s",ct);
//converting cipher text to plain text (decryption)
for(i=0;i<strlen(ct);i++)
{
if(ct[i]<key[i])
{
rt[i]=26+((ct[i]-97)-(key[i]-97))+97;
}
else
rt[i]=(((ct[i]-97)-(key[i]-97))%26)+97;
}
printf("n n plain text is:%s",rt);
getch();
}
Output (Screenshot):
PRACTICAL -5
Aim: To implement Play-Fair Cipher Technique.
Solution:
// Implementation of Playfair Cipher in C program
#include <stdio.h> //header file
#include <stdlib.h> //header file
#include <string.h> //header file
#define SIZE 30
// this function will convert the string to lowercase
void toLowerCase(char plain[], int ps)
{
int i;
for (i = 0; i < ps; i++) {
if (plain[i] > 64 && plain[i] < 91)
plain[i] += 32;
}
}
// this function will remove all the spaces
int removeSpaces(char* plain, int ps)
{
int i, count = 0;
for (i = 0; i < ps; i++)
if (plain[i] != ' ')
plain[count++] = plain[i];
plain[count] = ' ';
return count;
}
// this function will generate the 5x5 grid square
void generateKeyTable(char key[], int ks, char keyT[5][5])
{
int i, j, k, flag = 0, *dicty;
// character hashmap of 26 character that will
// store count of the alphabet.
dicty = (int*)calloc(26, sizeof(int));
for (i = 0; i < ks; i++) {
if (key[i] != 'j')
dicty[key[i] - 97] = 2;
}
dicty['j' - 97] = 1;
i = 0;
j = 0;
for (k = 0; k < ks; k++) {
if (dicty[key[k] - 97] == 2) {
dicty[key[k] - 97] -= 1;
keyT[i][j] = key[k];
j++;
if (j == 5) {
i++;
j = 0;
}
}
}
for (k = 0; k < 26; k++) {
if (dicty[k] == 0) {
keyT[i][j] = (char)(k + 97);
j++;
if (j == 5) {
i++;
j = 0;
}
}
}
}
// this function will search for the characters of a digraph
// in the key and return position of key
void search(char keyT[5][5], char a, char b, int arr[])
{
int i, j;
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
if (keyT[i][j] == a) {
arr[0] = i;
arr[1] = j;
}
else if (keyT[i][j] == b) {
arr[2] = i;
arr[3] = j;
}
}
}
}
// this function will find the modulus with 5
int mod5(int a) { return (a % 5); }
// this function will make the plain text length even
int prepare(char str[], int ptrs)
{
if (ptrs % 2 != 0) {
str[ptrs++] = 'z';
str[ptrs] = ' ';
}
return ptrs;
}
// encryption will done using this function
void encrypt(char str[], char keyT[5][5], int ps)
{
int i, a[4];
for (i = 0; i < ps; i += 2) {
search(keyT, str[i], str[i + 1], a);
if (a[0] == a[2]) {
str[i] = keyT[a[0]][mod5(a[1] + 1)];
str[i + 1] = keyT[a[0]][mod5(a[3] + 1)];
}
else if (a[1] == a[3]) {
str[i] = keyT[mod5(a[0] + 1)][a[1]];
str[i + 1] = keyT[mod5(a[2] + 1)][a[1]];
}
else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
}
// this function will encrypt cipher text using Playfair Cipher algorithm
void encryptByPlayfairCipher(char str[], char key[])
{
char ps, ks, keyT[5][5];
ks = strlen(key);
ks = removeSpaces(key, ks);
toLowerCase(key, ks);
ps = strlen(str);
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
ps = prepare(str, ps);
generateKeyTable(key, ks, keyT);
encrypt(str, keyT, ps);
}
// main code
int main()
{
char str[SIZE], key[SIZE];
// key text
strcpy(key, "Algorithm");
printf("Key text: %sn", key);
// Plaintext
strcpy(str, "Programming");
printf("Plain text: %sn", str);
// encryption using the "Playfair Cipher" algorithmn
encryptByPlayfairCipher(str, key);
printf("Cipher text: %sn", str);
return 0;
}
Output (Screenshot):
PRACTICAL -6
Aim: Write a program to implement Rail-Fence, Simple columnar Encryption Technique.
Solution:
// C++ program to illustrate Rail Fence Cipher
#include <iostream>
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()];
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--;
}
// now we can construct the fill the rail matrix
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++];
// now read the matrix in zig-zag manner to construct
// the resultant text
string result;
row = 0, col = 0;
for (int i=0; i< cipher.length(); i++)
{
// check the direction of flow
if (row == 0)
dir_down = true;
if (row == key-1)
dir_down = false;
// place the marker
if (rail[row][col] != '*')
result.push_back(rail[row][col++]);
// find the next row using direction flag
dir_down?row++: row--;
}
return result;
}
//driver program to check the above functions
int main()
{
cout << encryptRailFence("Vansh", 2) << endl;
cout << encryptRailFence("Shah", 3) << endl;
cout << encryptRailFence("Shahvansh", 3) << endl;
//Now decryption of the same cipher-text
cout << decryptRailFence("Vnhas",2) << endl;
cout << decryptRailFence("Shha",3) << endl;
cout << decryptRailFence("Svhhhasan",3) << endl;
return 0;
}
Output:
Practical – 7
Aim : To implement S-DES algorithm for data encryption.
Solution : (S-DES Algorithm) #include <bits/stdc++.h> using namespace std; string hex2bin(string s)
{
unordered_map<char, string> mp; mp['0'] = "0000";
mp['1'] = "0001";
mp['2'] = "0010";
mp['3'] = "0011";
mp['4'] = "0100";
mp['5'] = "0101";
mp['6'] = "0110";
mp['7'] = "0111";
mp['8'] = "1000";
mp['9'] = "1001";
mp['A'] = "1010";
mp['B'] = "1011";
mp['C'] = "1100";
mp['D'] = "1101";
mp['E'] = "1110";
mp['F'] = "1111";
string bin = "";
for (int i = 0; i < s.size(); i++) { bin += mp[s[i]];
}
return bin;
}
string bin2hex(string s)
{
unordered_map<string, string> mp; mp["0000"] = "0";
mp["0001"] = "1";
mp["0010"] = "2";
mp["0011"] = "3";
mp["0100"] = "4";
mp["0101"] = "5";
mp["0110"] = "6";
mp["0111"] = "7";
mp["1000"] = "8";
mp["1001"] = "9";
mp["1010"] = "A";
mp["1011"] = "B";
mp["1100"] = "C";
mp["1101"] = "D";
mp["1110"] = "E";
mp["1111"] = "F";
string hex = "";
for (int i = 0; i < s.length(); i += 4) { string ch = "";
ch += s[i];
ch += s[i + 1]; ch += s[i + 2]; ch += s[i + 3]; hex += mp[ch];
}
return hex;
}
string permute(string k, int* arr, int n)
{
string per = "";
for (int i = 0; i < n; i++) { per += k[arr[i] - 1];
}
return per;
}
string shift_left(string k, int shifts)
{
string s = "";
for (int i = 0; i < shifts; i++) { for (int j = 1; j < 28; j++) {
s += k[j];
}
s += k[0];
k = s; s = "";
}
return k;
}
string xor_(string a, string b)
{
string ans = "";
for (int i = 0; i < a.size(); i++) { if (a[i] == b[i]) {
ans += "0";
}
else {
ans += "1";
}
}
return ans;
}
string encrypt(string pt, vector<string> rkb, vector<string> rk)
{
pt = hex2bin(pt);
= { 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44,
36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22,
14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57,
49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35,
27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13,
5, 63, 55, 47, 39, 31, 23, 15, 7 };
pt = permute(pt, initial_perm, 64);
cout << "After initial permutation: " << bin2hex(pt)
<< endl;
string left = pt.substr(0, 32);
string right = pt.substr(32, 32);
cout << "After splitting: L0=" << bin2hex(left)
<< " R0=" << bin2hex(right) << endl;
int exp_d[48]
= { 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 };
int s[8][4][16] = {
{ 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5,
9, 0, 7, 0, 15, 7, 4, 14, 2, 13, 1, 10, 6,
12, 11, 9, 5, 3, 8, 4, 1, 14, 8, 13, 6, 2,
11, 15, 12, 9, 7, 3, 10, 5, 0, 15, 12, 8, 2,
4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 },
{ 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12,
0, 5, 10, 3, 13, 4, 7, 15, 2, 8, 14, 12, 0,
1, 10, 6, 9, 11, 5, 0, 14, 7, 11, 10, 4, 13,
1, 5, 8, 12, 6, 9, 3, 2, 15, 13, 8, 10, 1,
3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 },
{ 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12,
7, 11, 4, 2, 8, 13, 7, 0, 9, 3, 4,
6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 13,
6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12,
5, 10, 14, 7, 1, 10, 13, 0, 6, 9, 8,
7, 4, 15, 14, 3, 11, 5, 2, 12 },
{ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11,
12, 4, 15, 13, 8, 11, 5, 6, 15, 0, 3, 4, 7,
2, 12, 1, 10, 14, 9, 10, 6, 9, 0, 12, 11, 7,
13, 15, 1, 3, 14, 5, 2, 8, 4, 3, 15, 0, 6,
10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 },
{ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13,
0, 14, 9, 14, 11, 2, 12, 4, 7, 13, 1, 5, 0,
15, 10, 3, 9, 8, 6, 4, 2, 1, 11, 10, 13, 7,
8, 15, 9, 12, 5, 6, 3, 0, 14, 11, 8, 12, 7,
1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 },
{ 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14,
7, 5, 11, 10, 15, 4, 2, 7, 12, 9, 5, 6, 1,
13, 14, 0, 11, 3, 8, 9, 14, 15, 5, 2, 8, 12,
3, 7, 0, 4, 10, 1, 13, 11, 6, 4, 3, 2, 12,
9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 },
{ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5,
10, 6, 1, 13, 0, 11, 7, 4, 9, 1, 10, 14, 3,
5, 12, 2, 15, 8, 6, 1, 4, 11, 13, 12, 3, 7,
14, 10, 15, 6, 8, 0, 5, 9, 2, 6, 11, 13, 8,
1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 },
{ 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5,
0, 12, 7, 1, 15, 13, 8, 10, 3, 7, 4, 12, 5,
6, 11, 0, 14, 9, 2, 7, 11, 4, 1, 9, 12, 14,
2, 0, 6, 10, 13, 15, 3, 5, 8, 2, 1, 14, 7,
4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 }
};
= { 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23,
26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27,
3, 9, 19, 13, 30, 6, 22, 11, 4, 25 };
cout << endl;
for (int i = 0; i < 16; i++) {
string right_expanded = permute(right, exp_d, 48);
string x = xor_(rkb[i], right_expanded);
string op = "";
for (int i = 0; i < 8; i++) {
int row = 2 * int(x[i * 6] - '0')
+ int(x[i * 6 + 5] - '0');
int col = 8 * int(x[i * 6 + 1] - '0')
+ 4 * int(x[i * 6 + 2] - '0')
+ 2 * int(x[i * 6 + 3] - '0')
+ int(x[i * 6 + 4] - '0');
int val = s[i][row][col]; op += char(val / 8 + '0'); val = val % 8;
op += char(val / 4 + '0'); val = val % 4;
op += char(val / 2 + '0'); val = val % 2;
op += char(val + '0');
}
op = permute(op, per, 32);
x = xor_(op, left); left = x;
if (i != 15) { swap(left, right);
}
cout << "Round " << i + 1 << " " << bin2hex(left)
<< " " << bin2hex(right) << " " << rk[i]
<< endl;
}
string combine = left + right;
int final_perm[64]
= { 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47,
15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22,
62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36,
4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11,
51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58,
26, 33, 1, 41, 9, 49, 17, 57, 25 };
string cipher
= bin2hex(permute(combine, final_perm, 64)); return cipher;
}
int main()
{
string pt, key;
/*cout<<"Enter plain text(in hexadecimal): "; cin>>pt; cout<<"Enter key(in hexadecimal): "; cin>>key;*/
pt = "123456ABCD132536"; key = "AABB09182736CCDD";
// Key Generation key = hex2bin(key); int keyp[56]
= { 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34,
26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3,
60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7,
62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37,
29, 21, 13, 5, 28, 20, 12, 4 };
key = permute(key, keyp, 56);
int shift_table[16] = { 1, 1, 2, 2, 2, 2, 2, 2,
1, 2, 2, 2, 2, 2, 2, 1 };
int key_comp[48] = { 14, 17, 11, 24, 1, 5, 3, 28,
15, 6, 21, 10, 23, 19, 12, 4,
26, 8, 16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55, 30, 40,
51, 45, 33, 48, 44, 49, 39, 56,
34, 53, 46, 42, 50, 36, 29, 32 };
string left = key.substr(0, 28); string right = key.substr(28, 28);
vector<string> rkb; vector<string> rk;
for (int i = 0; i < 16; i++) {
left = shift_left(left, shift_table[i]); right = shift_left(right, shift_table[i]);
string combine = left + right;
string RoundKey = permute(combine, key_comp, 48);
rkb.push_back(RoundKey); rk.push_back(bin2hex(RoundKey));
}
cout << "nEncryption:nn";
string cipher = encrypt(pt, rkb, rk);
cout << "nCipher Text: " << cipher << endl;
cout << "nDecryptionnn"; reverse(rkb.begin(), rkb.end());
reverse(rk.begin(), rk.end());
string text = encrypt(cipher, rkb, rk); cout << "nPlain Text: " << text << endl;
}
OUTPUT:
PRACTICAL - 8
Aim : Write a program to implement RSA asymmetric (public key and private key)-Encryption.
Solution : (RSA asymmetric Encryption) #include <bits/stdc++.h> using namespace std; set<int> prime;
int public_key; int private_key; int n;
void primefiller()
{
vector<bool> seive(250, true); seive[0] = false;
seive[1] = false;
for (int i = 2; i < 250; i++) {
for (int j = i * 2; j < 250; j += i) { seive[j] = false;
}
}
for (int i = 0; i < seive.size(); i++) { if (seive[i])
prime.insert(i);
}
}
int pickrandomprime()
{
int k = rand() % prime.size(); auto it = prime.begin();
while (k--)
it++;
int ret = *it;
prime.erase(it); return ret;
}
void setkeys()
{
int prime1 = pickrandomprime(); int prime2 = pickrandomprime();
n = prime1 * prime2;
int fi = (prime1 - 1) * (prime2 - 1); int e = 2;
while (1) {
if (gcd(e, fi) == 1) break;
e++;
}
public_key = e; int d = 2; while (1) {
if ((d * e) % fi == 1) break;
d++;
}
private_key = d;
}
long long int encrypt(double message)
{
int e = public_key;
long long int encrpyted_text = 1; while (e--) {
encrpyted_text *= message; encrpyted_text %= n;
}
return encrpyted_text;
}
long long int decrypt(int encrpyted_text)
{
int d = private_key;
long long int decrypted = 1; while (d--) {
decrypted *= encrpyted_text;
decrypted %= n;
}
return decrypted;
}
vector<int> encoder(string message)
{
vector<int> form;
for (auto& letter : message) form.push_back(encrypt((int)letter));
return form;
}
string decoder(vector<int> encoded)
{
string s;
for (auto& num : encoded) s += decrypt(num);
return s;
}
int main()
{
primefiller(); setkeys();
string message = "Test Message";
vector<int> coded = encoder(message); cout << "Initial message:n" << message;
cout << "nnThe encoded message(encrypted by public " "key)n";
for (auto& p : coded) cout << p;
cout << "nnThe decoded message(decrypted by private " "key)n";
cout << decoder(coded) << endl; return 0;
}
OUTPUT:
PRACTICAL - 9
Aim : Implement Diffie-Hellman key exchange Method.
Solution:
#include<stdio.h>
long int power(int a,int b,int mod)
{
long long int t; if(b==1) return a;
t=power(a,b/2,mod); if(b%2==0)
return (t*t)%mod; else
return (((t*t)%mod)*a)%mod;
}
long long int calculateKey(int a,int x,int n)
{
return power(a,x,n);
}
int main()
{
int n,g,x,a,y,b;
// both the persons will be agreed upon the common n and g printf("Enter the value of n and g : "); scanf("%d%d",&n,&g);
// first person will choose the x
printf("Enter the value of x for the first person : "); scanf("%d",&x); a=power(g,x,n);
// second person will choose the y
printf("Enter the value of y for the second person : "); scanf("%d",&y); b=power(g,y,n);
printf("key for the first person is : %lldn",power(b,x,n)); printf("key for the second person is : %lldn",power(a,y,n)); return 0;
}
Output:
PRACTICAL -10
Aim : Implement a digital signature algorithm.
Solution:
def euclid(m, n):
if n == 0:
return m
else:
r = m % n
return euclid(n, r)
def exteuclid(a, b):
r1 = a
r2 = b
s1 = int(1)
s2 = int(0)
t1 = int(0)
t2 = int(1)
while r2 > 0:
q = r1//r2
r = r1-q * r2
r1 = r2
r2 = r
s = s1-q * s2
s1 = s2
s2 = s
t = t1-q * t2
t1 = t2
t2 = t
if t1 < 0:
t1 = t1 % a
return (r1, t1)
p = 823
q = 953
n = p * q
Pn = (p-1)*(q-1)
key = []
for i in range(2, Pn):
gcd = euclid(Pn, i)
if gcd == 1:
key.append(i)
e = int(313)
r, d = exteuclid(Pn, e)
if r == 1:
d = int(d)
print("decryption key is: ", d)
else:
print("Multiplicative inverse for
the given encryption key does not
exist. Choose a different encryption key ")
M = 19070
S = (M**d) % n
M1 = (S**e) % n
if M == M1:
print("As M = M1, Accept the
message sent by 159")
else:
print("As M not equal to M1,
Do not accept the message
sent by 159 ")
OUTPUT ➖
PRACTICAL -11
Aim : Perform various encryption-decryption techniques with cryptool.
Solution:
Cryptool is a software tool that supports various encryption-decryption techniques, including symmetric key encryption, asymmetric key encryption, and hash functions. Here's a brief overview of each of these techniques:
Symmetric key encryption: In symmetric key encryption, the same key is used for both encryption and decryption of the data. Cryptool supports various symmetric key encryption algorithms such as AES, DES, Triple DES, and Blowfish.
Asymmetric key encryption: In asymmetric key encryption, two different keys are used for encryption and decryption. The public key is used for encryption, and the private key is used for decryption. Cryptool supports various asymmetric key encryption algorithms such as RSA, ElGamal, and Diffie-Hellman.
Hash functions: Cryptool also supports various hash functions such as MD5, SHA-1, SHA-256, and SHA-512. Hash functions are used to create a fixed-size digest of the input data, which is used to verify the integrity of the data.
To use Cryptool, you can download and install it on your computer. Once installed, you can select the encryption-decryption technique you want to use, and then provide the input data to encrypt or decrypt. Cryptool will then generate the output data using the chosen
encryption-decryption technique.
Please note that encryption-decryption techniques can be complex and require a thorough understanding of the underlying algorithms and security concepts. It's important to use encryption-decryption techniques responsibly and securely.
Playfair Cypher using Cryptool
The Playfair cipher is a polygraphic substitution cipher that encrypts pairs of letters instead of individual letters, making it more difficult to decrypt than a simple substitution cipher. In this cipher, a 5x5 matrix of letters is used to encrypt plaintext. Cryptool is a software tool that can be used to encrypt and decrypt messages using various ciphers, including the Playfair cipher.
Here are the steps to use Cryptool to encrypt and decrypt messages using the Playfair cipher:
Open Cryptool and select "Classic Ciphers" from the list of options.
Click on the "Playfair" cipher option to open the Playfair cipher window.
In the "Key" field, enter a keyword or phrase that will be used to generate the 5x5 matrix of letters. This keyword should contain all the letters of the alphabet except for "J", which is usually replaced with "I".
Enter the plaintext message you want to encrypt in the "Message" field. The plaintext message should only contain letters, and any spaces or punctuation should be removed.
Click on the "Encrypt" button to encrypt the message using the Playfair cipher. The encrypted message will be displayed in the "Cipher" field.
To decrypt a message using the Playfair cipher, enter the ciphertext message in the "Cipher" field and click on the "Decrypt" button. The decrypted message will be displayed in the "Message" field.
You can also experiment with different key phrases to see how they affect the encrypted message. Additionally, you can use Cryptool to perform frequency analysis and other techniques to break the Playfair cipher.
The Playfair cipher is a relatively simple cipher to use and understand, but it can be difficult to decrypt without the key phrase. Using Cryptool to encrypt and decrypt messages with the Playfair cipher can help you understand how it works and how it can be used for secure communication
PRACTICAL -12
Aim : Study and use the Wireshark for the various network protocols.
Solution:
Wireshark is a powerful network protocol analyzer that allows you to capture and examine network traffic in real time. It supports a wide range of network protocols and can be used to troubleshoot network issues, detect security threats, and analyze network performance. Here are some steps to study and use Wireshark for various network protocols:
Install Wireshark: Wireshark is available for download on the official website for Windows, macOS, and Linux. After downloading and installing it, run the application.
Select an interface: Select the network interface that you want to capture traffic from. You can do this by going to Capture > Interfaces and selecting the appropriate interface.
Start capturing: Click on the "Start" button to start capturing network traffic on the selected interface. You will see a list of captured packets in real time.
Filter packets: Wireshark can capture a large amount of network traffic, so it's important to filter out the packets you are interested in. You can use display filters to filter packets by protocol, source or destination IP address, port number, or other criteria.
Analyze packets: Once you have captured and filtered the packets, you can analyze them to troubleshoot network issues or detect security threats. You can view detailed information about each packet, including the source and destination addresses, protocol, payload, and more.
Decode protocols: Wireshark supports a wide range of network protocols, and can decode the packets so you can see the data being transmitted. For example, if you are analyzing HTTP traffic, you can view the HTTP headers and data in the packet payload.
Save and export packets: You can save the captured packets for later analysis, or export them in various formats, such as pcap or CSV.
Some common protocols that you can study and use Wireshark for include:
1. TCP/IP: Wireshark is ideal for analyzing TCP/IP traffic, which is the foundation of the internet and most local area networks.
2. HTTP: Wireshark can capture and analyze HTTP traffic, which is used for web browsing and many other applications.
3. DNS: Wireshark can capture and decode DNS traffic, which is used to resolve domain names to IP addresses.
4. FTP: Wireshark can capture and analyze FTP traffic, which is used to transfer files over the internet.
5. SMTP: Wireshark can capture and analyze SMTP traffic, which is used for sending and receiving email.
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team