Uploaded by Kathan Parekh

insPractPPT

advertisement
Information and Network Security
102046708
Practical:1
AIM: To implement Caesar cipher encryption-decryption.
#include<iostream>
#include<string.h>
using namespace std;
int main() {
cout<<"Enter the message:\n";
char msg[100];
cin.getline(msg,100);
int i, j, length,choice,key;
cout << "Enter key: ";
cin >> key;
length = strlen(msg);
cout<<"Enter your choice \n1. Encryption \n2. Decryption \n";
cin>>choice;
if (choice==1){
char ch;
for(int i = 0; msg[i] != '\0'; ++i) {
ch = msg[i];
if (ch >= 'a' && ch <= 'z'){
ch = ch + key;
if (ch > 'z') {
ch = ch - 'z' + 'a' - 1;}
msg[i] = ch;}
else if (ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if (ch > 'Z'){ch = ch - 'Z' + 'A' - 1;}
msg[i] = ch;}
}
printf("Encrypted message: %s", msg);}
else if (choice == 2) {
12002040501027
1
Information and Network Security
char ch;
102046708
for(int i = 0; msg[i] != '\0'; ++i) {
ch = msg[i];
if(ch >= 'a' && ch <= 'z') {
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;}
msg[i] = ch;}
else if(ch >= 'A' && ch <= 'Z') {
ch = ch - key;
if(ch < 'A') {
ch = ch + 'Z' - 'A' + 1;}
msg[i] = ch;
}}
cout << "Decrypted message: " << msg;}}
OUTPUT:
Enter the message:WELCOMETOgcet
Enter key: 3
Enter your choice
1. Encryption
2. Decryption
1
Encrypted message: ZHOFRPHWRjfhw
Enter the message:
ZHOFRPHWRjfhw
Enter key: 3
Enter your choice
1. Encryption
2. Decryption
2
Decrypted message: WELCOMETOgcet
12002040501027
2
Information and Network Security
102046708
Practical:2
AIM: To implement Monoalphabetic cipher encryption-decryption.
#include <bits/stdc++.h>
using namespace std;
unordered_map<char,char> newMap;
void helper(string a, string b)
{
newMap.clear();
for(int i=0; i<a.size(); i++)
{
newMap.insert(make_pair(a[i],b[i]));
}
}
string decrypt(string msg)
{
string plaintext;
for(int i=0; i<msg.size(); i++)
{
plaintext.push_back(newMap[msg[i]]); }
return plaintext;
}
string encrypt(string msg) {
string ciphertext;
for(int i=0; i<msg.size(); i++)
{
ciphertext.push_back(newMap[msg[i]]); }
return ciphertext;
}
int main()
{
string alphabet = "abcdefghijklmnopqrstuvwxyz";
string substitution = "qwertyuiopasdfghjklzxcvbnm";
12002040501027
3
Information and Network Security
102046708
string msg = "welcometogcet";
helper(alphabet, substitution);
string cipher = encrypt(msg);
cout<<"Encrypted Cipher Text: "<<cipher<<endl;
helper(substitution, alphabet);
string
plain
=
decrypt(cipher);
cout<<"Decrypted Original Text: "<<plain<<endl;
}
OUTPUT:
Encrypted Cipher Text: vtsegdtzguetz
Decrypted Original Text: welcometogcet
12002040501027
4
Information and Network Security
102046708
Practical:3
AIM: To implement Playfair cipher encryption-decryption.
#include <bits/stdc++.h>
using namespace std;
#define SIZE 30
void toLowerCase(char plain[], int ps)
{
int i;
for (i = 0; i < ps; i++) {
if (plain[i] > 64 && plain[i] < 91)
plain[i] += 32; }}
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] = '\0';
return count;
}
void generateKeyTable(char key[], int ks, char keyT[5][5])
{
int i, j, k, flag = 0;
int dicty[26] = { 0 };
for (i = 0; i < ks; i++) {
if (key[i] != 'j')
dicty[key[i] - 97] = 2; }
dicty['j' - 97] = 1;
i = 0;
12002040501027
5
Information and Network Security
j = 0;
102046708
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;}}}}
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;
12002040501027
6
Information and Network Security
arr[3] = j;}}}}
102046708
int mod5(int a) { return (a % 5); }
int prepare(char str[], int ptrs)
{
if (ptrs % 2 != 0) {
str[ptrs++] = 'z';
str[ptrs] = '\0';
}
return ptrs;
}
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]];
}
}
}
void encryptByPlayfairCipher(char str[], char key[])
12002040501027
7
Information and Network Security
{
102046708
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);
}
int mod5decrypt(int a)
{if (a < 0)
a += 5;
return (a % 5);}
void decrypt(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]][mod5decrypt(a[1] - 1)];
str[i + 1] = keyT[a[0]][mod5decrypt(a[3] - 1)];
}
else if (a[1] == a[3]) {
str[i] = keyT[mod5decrypt(a[0] - 1)][a[1]];
str[i + 1] = keyT[mod5decrypt(a[2] - 1)][a[1]];}
else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];}}}
void decryptByPlayfairCipher(char str[], char key[])
12002040501027
8
Information and Network Security
{
102046708
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);
generateKeyTable(key, ks, keyT);
decrypt(str, keyT, ps);}
int main()
{
char str[SIZE], key[SIZE];
strcpy(key, "Monarchy");
cout << "Key text: " << key << "\n";
strcpy(str, "instruments");
cout << "Plain text: " << str << "\n";
encryptByPlayfairCipher(str, key);
cout << "Cipher text: " << str << "\n";
decryptByPlayfairCipher(str, key);
cout << "Plain text: " << str << "\n";
return 0;
}
OUTPUT:
Key text: Monarchy
Plain text: instruments
Cipher text: gatlmzclrqtx
Plain text: instruments
12002040501027
9
Information and Network Security
12002040501027
102046708
10
Information and Network Security
102046708
Practical:4
AIM: To implement Polyalphabetic cipher encryption-decryption.
#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)
{
12002040501027
11
Information and Network Security
string orig_text;
102046708
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 = "WELCOMETOGCET";
string keyword = "COMPUTER";
string key = generateKey(str, keyword);
string cipher_text = cipherText(str, key);
cout << "Ciphertext : "
<< cipher_text << "\n";
cout << "Decrypted Text : "
<< originalText(cipher_text, key);
return 0;
}
OUTPUT:
Ciphertext : YSXRIFIKQUOTN
Decrypted Text : WELCOMETOGCET
12002040501027
12
Information and Network Security
102046708
Vernam Cipher
#include<bits/stdc++.h>
using namespace std;
int main(){
int t,n,i,j,k,sum=0;
string m;
cout<<"Enter the message"<<'\n';
cin>>m;
string key;
cout<<"Enter the key"<<'\n';
cin>>key;
int mod = key.size();
j=0;
for(i=key.size();i<m.size();i++){
key+=key[j%mod];
j++;
}
string enc="";
for(i=0;i<m.size();i++){
enc += (key[i]-'A'+m[i]-'A')%26+'A';
}
cout<<"Encrypted message: "<<enc<<'\n';
for(i=key.size();i<enc.size();i++){
key+=key[j%mod];
j++;
}
string dec="";
for(i=0;i<enc.size();i++){
12002040501027
13
Information and Network Security
dec += (enc[i]-key[i]+26)%26+'A';
102046708
}
cout<<"Decrypted message: "<<dec<<'\n';
return 0;
}
OUTPUT:
Enter the message
HELLOWORLD
Enter the key
TEST
Encrypted message: AIDEHAGKEH
Decrypted message: HELLOWORLD
12002040501027
14
Information and Network Security
102046708
Practical:5
AIM: To implement Hill cipher encryption-decryption.
#include<iostream>
#include<math.h>
using namespace std;
float encrypt[3][1], decrypt[3][1], a[3][3], b[3][3], mes[3][1], c[3][3];
void encryption();
void decryption();
void getKeyMessage();
void inverse();
int main() {
getKeyMessage();
encryption();
decryption();
}
void encryption() {
int i, j, k;
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
encrypt[i][j] = encrypt[i][j] + a[i][k] * mes[k][j];
cout<<"\nEncrypted string is: ";
for(i = 0; i < 3; i++)
cout<<(char)(fmod(encrypt[i][0], 26) + 97);
}
void decryption() {
int i, j, k;
inverse();
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
12002040501027
15
Information and Network Security
for(k = 0; k < 3; k++)
102046708
decrypt[i][j] = decrypt[i][j] + b[i][k] * encrypt[k][j];
cout<<"\nDecrypted string is: ";
for(i = 0; i < 3; i++)
cout<<(char)(fmod(decrypt[i][0], 26) + 97);
cout<<"\n";}
void getKeyMessage() {
int i, j;
char msg[3];
cout<<"Enter 3x3 matrix for key (It should be inversible):\n";
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
cin>>a[i][j];
c[i][j] = a[i][j];
}
cout<<"\nEnter a 3 letter string: ";
cin>>msg;
for(i = 0; i < 3; i++)
mes[i][0] = msg[i] - 97;
}
void inverse() {
int i, j, k;
float p, q;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
if(i == j)
b[i][j]=1;
else
b[i][j]=0;
}
12002040501027
16
Information and Network Security
for(k = 0; k < 3; k++) {
102046708
for(i = 0; i < 3; i++) {
p = c[i][k];
q = c[k][k];
for(j = 0; j < 3; j++) {
if(i != k) {
c[i][j] = c[i][j]*q - p*c[k][j];
b[i][j] = b[i][j]*q - p*b[k][j];}}}}
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
b[i][j] = b[i][j] / c[i][i];
cout<<"\n\nInverse Matrix is:\n";
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
cout<<b[i][j]<<" ";
cout<<"\n";}
}
OUTPUT:
Enter 3x3 matrix for key (It should be inversible):
6 24 1
13 16 10
20 17 15
Enter a 3 letter string: get
Encrypted string is: vuf
Inverse Matrix is:
0.15873 -0.777778 0.507937
0.0113379 0.15873 -0.106576
-0.22449 0.857143 -0.489796
Decrypted string is: get
12002040501027
17
Information and Network Security
12002040501027
102046708
18
Information and Network Security
102046708
Practical:6
AIM: To implement Rail Fence and Columnar transposition cipher encryption-decryption.
Rail Fence
#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--;
}
string result;
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()];
12002040501027
19
Information and Network Security
for (int i=0; i < key; i++)
102046708
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--;
12002040501027
20
Information and Network Security
}
102046708
return result;
}
int main()
{
cout << encryptRailFence("computer", 2) << endl;
cout << encryptRailFence("welcometogcet", 3) << endl;
cout << encryptRailFence("cpdepartment", 3) << endl;
cout << decryptRailFence("cmueoptr",2) << endl;
cout << decryptRailFence("wootecmtgelec",3) << endl;
cout << decryptRailFence("cpmpeatetdrn",3) << endl;
return 0;
}
OUTPUT:
cmueoptr
wootecmtgelec
cpmpeatetdrn
computer
welcometogcet
cpdepartment
12002040501027
21
Information and Network Security
102046708
Columnar transposition
#include<bits/stdc++.h>
using namespace std;
string const key = "HACK";
map<int,int> keyMap;
void setPermutationOrder()
{
for(int i=0; i < key.length(); i++)
{
keyMap[key[i]] = i;
}
}
string encryptMessage(string msg)
{
int row,col,j;
string cipher = "";
col = key.length();
row = msg.length()/col;
if (msg.length() % col)
row += 1;
char matrix[row][col];
for (int i=0,k=0; i < row; i++)
{for (int j=0; j<col; ) {
if(msg[k] == '\0') {
matrix[i][j] = '_';
j++;}
if( isalpha(msg[k]) || msg[k]==' ')
{
matrix[i][j] = msg[k];
j++;}
k++;}}
12002040501027
22
Information and Network Security
102046708
for (map<int,int>::iterator ii = keyMap.begin(); ii!=keyMap.end(); ++ii)
{
j=ii->second;
for (int i=0; i<row; i++)
{
if( isalpha(matrix[i][j]) || matrix[i][j]==' ' || matrix[i][j]=='_')
cipher += matrix[i][j];}}
return cipher;}
string decryptMessage(string cipher)
{
int col = key.length();
int row = cipher.length()/col;
char cipherMat[row][col];
for (int j=0,k=0; j<col; j++)
for (int i=0; i<row; i++)
cipherMat[i][j] = cipher[k++];
int index = 0;
for( map<int,int>::iterator ii=keyMap.begin(); ii!=keyMap.end(); ++ii)
ii->second = index++;
char decCipher[row][col];
map<int,int>::iterator ii=keyMap.begin();
int k = 0;
for (int l=0,j; key[l]!='\0'; k++)
{
j = keyMap[key[l++]];
for (int i=0; i<row; i++)
{
decCipher[i][k]=cipherMat[i][j];
}
}
string msg = "";
for (int i=0; i<row; i++)
{
12002040501027
23
Information and Network Security
for(int j=0; j<col; j++)
102046708
{
if(decCipher[i][j] != '_')
msg += decCipher[i][j]; }}
return msg;}
int main(void)
{
string msg = "welcometogcet";
setPermutationOrder();
string cipher = encryptMessage(msg);
cout << "Encrypted Message: " << cipher << endl;
cout << "Decrypted Message: " << decryptMessage(cipher) << endl;
return 0;
}
OUTPUT:
Encrypted Message: emg_lec_wootcte_
Decrypted Message: welcometogcet
12002040501027
24
Information and Network Security
102046708
Practical:7
AIM: To implement Simplified Data Encryption Standard.
#include <iostream>
using namespace std;
void printArray(int arr[],int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl; }
void Permutation(int arr[], int index[], int n){
int temp[n];
for (int i=0; i<n; i++)
temp[i] = arr[index[i]-1];
for (int i=0; i<n; i++)
arr[i] = temp[i];}
void ExPermutation(int arr[], int index[], int arr2[], int n){
for (int i=0; i<n; i++)
arr2[i] = arr[index[i]-1];}
void Split(int arr[], int n, int *l, int *r){
for(int i=0;i<n/2;i++)
l[i] = arr[i];
for(int j=0,i=n/2;i<n;i++,j++)
r[j] = arr[i];}
int bin2dec(int arr[],int size){
int decimal = 0 ;
for(int i = 0 ; i < size ; i++)
decimal = (decimal << 1) + arr[i] ;
return decimal;}
void dec2bin(int opSn, int *ar){
int i=0;
12002040501027
25
Information and Network Security
while(opSn!=0)
102046708
{ ar[i] = opSn%2;
i++;
opSn = opSn/2;} }
void combine(int arr1[], int arr2[], int *arr3, int n){
for (int i=0;i<n/2;i++)
arr3[i]=arr1[i];
for (int i=n/2,j=0;i<n;i++,j++)
arr3[i]=arr2[j];}
void S_box(int a[],int b[],int *opS0S1){
int S0[4][4] = {{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}}, S1[4][4] = {{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
int rowS0bin[2] = {a[0],a[3]}, colS0bin[2] = {a[1],a[2]};
int rowS0dec = bin2dec(rowS0bin,2), colS0dec = bin2dec(colS0bin,2);
int opS0dec = S0[rowS0dec][colS0dec];
int opS0bin[2]={};
dec2bin(opS0dec, opS0bin);
int rowS1bin[2] = {b[0],b[3]}, colS1bin[2] = {b[1],b[2]};
int rowS1dec = bin2dec(rowS1bin,2), colS1dec = bin2dec(colS1bin,2);
int opS1dec = S1[rowS1dec][colS1dec];
int opS1bin[2]={};
dec2bin(opS1dec, opS1bin);
for (int i=0;i<2;i++)
opS0S1[i]=opS0bin[i];
for (int i=2,j=0;i<4;i++,j++)
opS0S1[i]=opS1bin[j];
cout<<"After S-Box: ";
printArray(opS0S1,4);
cout<<endl;}
void Swap(int *left_array, int *right_array, int n){
int temp[n];
12002040501027
26
Information and Network Security
for (int i=0; i<n; i++)
102046708
temp[i] = left_array[i];
for (int i=0; i<n; i++)
left_array[i]= right_array[i];
for (int i=0; i<n; i++)
right_array[i]= temp[i];}
void XOR(int arr1[],int arr2[],int n){
int temp[n];
for(int i=0; i<n; i++)
{
temp[i] = arr1[i] ^ arr2[i];
}
for (int i=0; i<n; i++)
arr2[i] = temp[i];}
void leftRotate(int arr[], int d, int n)
{
int temp[d];
for (int i=0; i<d; i++)
temp[i] = arr[i];
for (int i = 0; i < n-d; i++)
arr[i] = arr[i+d];
for (int i=n-d,j=0; i<n; i++,j++)
arr[i]=temp[j];}
class KeyGeneration {
private:
int P10_rule[10] = {3,5,2,7,4,10,1,9,8,6};
int P8_rule[8] = {6,3,7,4,8,5,10,9};
int temp_left[5]={}, temp_right[5]={};
public:
KeyGeneration(){
12002040501027
27
Information and Network Security
cout<<endl;
102046708
cout<<"KEY GENERATION.."<<endl;
cout<<endl;
}
void key(int master_key[], int *k1, int *k2){
Permutation(master_key,P10_rule,10);
cout<<"After P10 Permutation: ";
printArray(master_key,10);
cout<<endl;
Split(master_key,10,temp_left,temp_right);
cout<<"After split, "<<endl;
cout<<"l = ";
printArray(temp_left,5);
cout<<"r = " ;
printArray(temp_right,5);
cout<<endl;
leftRotate(temp_left,1,5);
leftRotate(temp_right,1,5);
cout<<"After LeftShift-1, "<<endl;
cout<<"l = ";
printArray(temp_left,5);
cout<<"r = ";
printArray(temp_right,5);
cout<<endl;
combine(temp_left,temp_right,master_key,10);
Permutation(master_key,P8_rule,10);
cout<<"After P8, Key-1: ";
for(int i=0;i<8;i++)
k1[i]=master_key[i];
printArray(k1,8);
12002040501027
28
Information and Network Security
cout<<endl;
102046708
leftRotate(temp_left,2,5);
leftRotate(temp_right,2,5);
cout<<"After LeftShift-2, "<<endl;
cout<<"l = ";
printArray(temp_left,5);
cout<<"r = ";
printArray(temp_right,5);
cout<<endl;
combine(temp_left,temp_right,master_key,10);
Permutation(master_key,P8_rule,10);
cout<<"After P8, Key-2: ";
for(int i=0;i<8;i++)
k2[i]=master_key[i];
printArray(k2,8);}};
class Roundfunction{
private:
int Expanrule[8] = {4,1,2,3,2,3,4,1};
int P4_rule[4] = {2,4,3,1};
int r_arr2[8]={},a[4]={},b[4]={};
int opS0S1[4]={};
public:
void roundfun(int *k1,int *l_arr, int *r_arr, int *fk1){
ExPermutation(r_arr, Expanrule, r_arr2,8);
cout<<"After EP: ";
printArray(r_arr2,8);
cout<<endl;
XOR(k1,r_arr2,8);
cout<<"XOR with key"<<endl;
printArray(r_arr2,8);
12002040501027
29
Information and Network Security
cout<<endl;
102046708
Split(r_arr2,8,a,b);
cout<<"After Split"<<endl;
cout<<"l = ";
printArray(a,4);
cout<<"r = ";
printArray(b,4);
cout<<endl;
S_box(a,b,opS0S1);
Permutation(opS0S1,P4_rule,4);
cout<<"After P4"<<endl;
printArray(opS0S1,4);
cout<<endl;
XOR(opS0S1,l_arr,4);
cout<<"XOR with leftarray"<<endl;
printArray(l_arr,4);
cout<<endl;
combine(l_arr,r_arr,fk1,8);
cout<<"After combine"<<endl;
printArray(fk1,8);
cout<<endl;}};
class encrypt : public Roundfunction{
private:
int IP_rule[8] = {2,6,3,1,4,8,5,7};
int IP_inv_rule[8] = {4,1,3,5,7,2,8,6};
int fkk[8]={};
public:
encrypt(){
cout<<endl;
cout<<"ENCRYPTING.."<<endl;
12002040501027
30
Information and Network Security
cout<<endl; }
102046708
int l_arr[4]={},r_arr[4]={};
void enc(int arr[], int *key1, int *key2, int *fk1){
Permutation(arr, IP_rule, 8);
cout<<"After IP: ";
printArray(arr,8);
cout<<endl;
Split(arr,8, l_arr,r_arr);
cout<<"After split, "<<endl;
cout<<"l = ";
printArray(l_arr,4);
cout<<"r = " ;
printArray(r_arr,4);
cout<<endl;
cout<<"Round Function(fk)-1"<<endl;
Roundfunction::roundfun(key1,l_arr,r_arr,fk1);
Swap(l_arr,r_arr,4);
cout<<"After Swap"<<endl;
cout<<"l = ";
printArray(l_arr,4);
cout<<"r = " ;
printArray(r_arr,4);
cout<<endl;
cout<<"Round Function(fk)-2"<<endl;
Roundfunction::roundfun(key2,l_arr,r_arr,fk1);
Permutation(fk1,IP_inv_rule,8);
cout<<"After IP-Inverse, 8-bit Cipher Text is: "<<endl;
printArray(fk1,8);}};
class decrypt : public Roundfunction{
private:
12002040501027
31
Information and Network Security
int IP_rule[8] = {2,6,3,1,4,8,5,7};
102046708
int IP_inv_rule[8] = {4,1,3,5,7,2,8,6};
int fkk[8]={};
public:
int l_arr[4]={},r_arr[4]={};
void decryp(int arr[], int *key1, int *key2, int *fk1){
Permutation(arr, IP_rule, 8);
cout<<"IP"<<endl;
printArray(arr,8);
Split(arr,8, l_arr,r_arr);
cout<<"Split"<<endl;
printArray(l_arr,4);
printArray(r_arr,4);
Roundfunction::roundfun(key2,l_arr,r_arr,fk1);
Swap(l_arr,r_arr,4);
cout<<"swap"<<endl;
printArray(l_arr,4);
printArray(r_arr,4);
Roundfunction::roundfun(key1,l_arr,r_arr,fk1);
Permutation(fk1,IP_inv_rule,8);
cout<<"After IP-Inverse, 8-bit Plain Text is: "<<endl;
printArray(fk1,8);}};
int main()
{
char input;
int arr[8]={};
int master_key[10]={};
int k1[8]={},k2[8]={};
int fk1[8] = {};
cout<<"Enter 10-bit Master Key (using space)"<<endl;
12002040501027
32
Information and Network Security
for(int i=0;i<10;i++){
102046708
cin>>master_key[i];
}
if((sizeof(master_key)/sizeof(master_key[0]))!=10)
throw "Error. Enter 10-bits";
KeyGeneration k;
k.key(master_key,k1,k2);
cout<<"
ndl;
"<<e
cout<<endl;
cout<<"Enter e for Encryption | Enter d for Decryption | Enter b for Both"<<endl;
cin>>input;
if (input == 'b'||input == 'B'){
cout<<"Enter 8-bit Plain Text (using space)"<<endl;
for(int i=0;i<8;i++){
cin>>arr[i];}
if((sizeof(arr)/sizeof(arr[0]))!=8)
throw "Error. Enter 8-bits";
encrypt e;
e.enc(arr,k1,k2,fk1);
for(int i=0;i<8;i++)
arr[i] = fk1[i];
cout<<"
ndl;
"<<e
decrypt d;
d.decryp(arr,k1,k2,fk1);
}
else if (input == 'e'||input == 'E'){
cout<<"Enter 8-bit Plain Text (using space)"<<endl;
for(int i=0;i<8;i++){
12002040501027
33
Information and Network Security
cin>>arr[i];
102046708
}
if((sizeof(arr)/sizeof(arr[0]))!=8)
throw "Error. Enter 8-bits";
encrypt e;
e.enc(arr,k1,k2,fk1);
}
else if (input == 'd'||input == 'D'){
cout<<"Enter 8-bit Cipher Text (using space)"<<endl;
for(int i=0;i<8;i++){
cin>>arr[i];
}
if((sizeof(arr)/sizeof(arr[0]))!=8)
throw "Error. Enter 8-bits";
decrypt d;
d.decryp(arr,k1,k2,fk1);
}
else
throw "Error, Choose correct option";
return 0;
}
OUTPUT:
Enter 10-bit Master Key (using space)
1101011001
KEY GENERATION..
After P10 Permutation: 0 0 1 1 1 1 1 0 0 1
After split,
l=00111
r=11001
12002040501027
34
Information and Network Security
After LeftShift-1,
102046708
l=01110
r=10011
After P8, Key-1: 1 1 0 1 0 0 1 1
After LeftShift-2,
l=11001
r=01110
After P8, Key-2: 0 0 1 0 1 1 0 1
Enter e for Encryption | Enter d for Decryption | Enter b for Both
b
Enter 8-bit Plain Text (using space)
10101111
ENCRYPTING..
After IP: 0 1 1 1 0 1 1 1
After split,
l=0111
r=0111
Round Function(fk)-1
After EP: 1 0 1 1 1 1 1 0
XOR with key
01101101
After Split
l=0110
r=1101
After S-Box: 0 1 0 0
After P4
1000...........
After IP-Inverse, 8-bit Plain Text is:
10101111
12002040501027
35
Information and Network Security
12002040501027
102046708
36
Information and Network Security
102046708
Practical:8
AIM: To implement Diffi-Hellman Key Exchange method.
#include <cmath>
#include <iostream>
using namespace std;
long long int power(long long int a, long long int b,long long int P)
{
if (b == 1)
return a;
else
return (((long long int)pow(a, b)) % P);
}
int main()
{
long long int P, G, x, a, y, b, ka, kb;
P = 23;
cout << "The value of P : " << P << endl;
G = 9;
cout << "The value of G : " << G << endl;
a = 4;
cout << "The private key a for Alice : " << a << endl;
x = power(G, a, P);
b = 3;
cout << "The private key b for Bob : " << b << endl;
y = power(G, b, P);
ka = power(y, a, P);
kb = power(x, b, P);
cout << "Secret key for the Alice is : " << ka << endl;
cout << "Secret key for the Bob is : " << kb << endl;
return 0;
12002040501027
37
}
Information and Network Security
102046708
OUTPUT:
The value of P : 23
The value of G : 9
The private key a for Alice : 4
The private key b for Bob : 3
Secret key for the Alice is : 9
Secret key for the Bob is : 9
12002040501027
38
Information and Network Security
102046708
Practical:9
AIM: To implement RSA encryption-decryption algorithm.
#include<stdio.h>
#include<math.h>
//to find gcd
int gcd(int a, int h)
{
int temp;
while(1)
{
temp = a%h;
if(temp==0)
return h;
a = h;
h = temp;
}
}
int main()
{
//2 random prime numbers
double p = 3;
double q = 7;
double n=p*q;
double count;
double totient = (p-1)*(q-1);
//public key
//e stands for encrypt
double e=2;
12002040501027
39
Information and Network Security
102046708
//for checking co-prime which satisfies e>1
while(e<totient)
{
count = gcd(e,totient);
if(count==1)
break;
else
e++;
}
//private key
//d stands for decrypt
double d;
//k can be any arbitrary value
double k = 2;
//choosing d such that it satisfies d*e = 1 + k * totientd =
(1 + (k*totient))/e;
double msg = 12; double
c = pow(msg,e);double m
= pow(c,d); c=fmod(c,n);
m=fmod(m,n);
printf("Message data = %lf",msg);
printf("\np = %lf",p);
printf("\nq = %lf",q);
printf("\nn = pq = %lf",n);
printf("\ntotient = %lf",totient);
printf("\ne = %lf",e);
printf("\nd = %lf",d);
printf("\nEncrypted data = %lf",c);
printf("\nOriginal Message Sent = %lf",m);
12002040501027
40
Information and Network Security
102046708
return 0;
}
Output:
12002040501027
41
Information and Network Security
12002040501027
102046708
42
Information and Network Security
102046708
Practical:10
AIM: Demonstrate and perform various encryption-decryption techniqueswith
crypt tool.
Encryption and Decryption of Caesar Cipher.
Here, we will implement an encryption and decryption of Caesar Cipher, which is actually a
substitution method of cryptography. The Caesar Cipher involves replacingeach letter of the
alphabet with a letter placed down or up according to the key given.
To start with the process you have to move to the Encrypt/Decrypt tab of the program.There,
you will find Symmetric (Classic) tab - Choose Caesar Cipher. For further information, you
can get guided by the image below.
Figure1: Encrypt/Decrypt of Cryptool
In encryption, we are replacing the plaintext letter with the 3rd letter of the alphabet that is
if “A” is our plaintext character, then the Ciphertext will be “D”
So, if I give “Monarchy” as plaintext in Caesar Cipher, it will show me the encryption, as
shown in the below image.
12002040501027
43
Information and Network Security
102046708
Encryption and Decryption of Playfair:Again, we have to move to Encrypt/Decrypt - Symmetric - Playfair Cipher and performthe
encryption part. We are putting the same plaintext – MONARCHY.
So, when we press the encrypt button, we will get the Ciphertext – “ONARMDYB”.
12002040501027
44
Information and Network Security
102046708
Encryption and Decryption of Hill Cipher:Again, we have to move to Encrypt/Decrypt - Symmetric - Hill Cipher and perform the
encryption part. We are putting the plaintext as – DRGREERROCKS and assuming that the
program gives us the Ciphertext as – FZIFTOTBXGPO.
So, when we press the encrypt button, we will get the Ciphertext –
“FZIFTOTBXGPO”.
Encryption and Decryption of Vigener Cipher:Again, we have to move to Encrypt/Decrypt - Symmetric - Vigener Cipher and perform the
encryption part. We are putting the plaintext as – MICHIGANTECHNOLOGICALUNIVERSITY
and assuming that the program gives usthe Ciphertext as – TWWNPZOAAS…..,with the help
of key as – HOUGHTON.
12002040501027
45
Information and Network Security
102046708
So, when we press the encrypt button, we will get the Ciphertext somewhat like –
“TWWNPZOAASWNUHZBNWWGSNBVCSLYPMM”.
Again, we have to move to Encrypt/Decrypt - Symmetric - Railfence Cipher and perform the
encryption part. We are putting the plaintext as – UNBREAKABLE and assuming that the
program gives us the Ciphertext as – UEBNRAALBKE…..,with thehelp of key as – 3.
12002040501027
46
Information and Network Security
102046708
So, when we press the encrypt button, we will get the Ciphertext like
–“UEBNRAALBKE”.
12002040501027
47
Information and Network Security
12002040501027
102046708
48
Information and Network Security
102046708
Practical:11
AIM: Study and use open-source packet analyser-Wireshark to understand
security mechanism of various network protocols.
What can you do with Wireshark?
Over the past few years, Wireshark has developed a reputation as one of the most reliable
network protocol analysers available on the market. Users across the globe have been using
this open-source application as a complete network analysis tool. Through Wireshark, user
scan troubleshoot network problems, examine networksecurity issues, debug protocols, and
learn network processes.
How to Use Wireshark
As mentioned above, Wireshark is a network protocol analysis tool. At its core, Wireshark
was designed to break down packets of data being transferred across different networks.
The user can search and filter for specific packets of data and analyze how they are
transferred across their network. These packets can be used for analysis on a real-time or
offline basis.
The user can use this information to generate statistics and graphs. Wireshark was originally
known as Ethereal but has since established itself as one of the key networkanalysis tools on
the market. This is the go-to tool for users who want to view data generated by different
networks and protocols.
Wireshark is suitable for novice and expert users alike. The user interface is incrediblysimple
to use once you learn the initial steps to capture packets. More advanced userscan use the
platform’s decryption tools to break down encrypted packets as well.
Wireshark Core Features
Below is a breakdown of Wireshark’s core features:
Capture live packet data
Import packets from text files
View packet data and protocol information
Save captured packet data
Display packets
Filter packets
Search packets
Colorize packets
Generate Statistics
12002040501027
49
Information and Network Security
12002040501027
102046708
50
Information and Network Security
102046708
Practical:12
AIM: Detail Case Study: Real world implementation of Network Security
Algorithm/Concept.
What is Network Security?
Network security allows you to take preventive measures to help protect the networking
infrastructure from malfunction, misuse, destruction, modification, unauthorized access,
etc. While you are uploading your data on the internet and thinking it is safe and secure,
attackers can breach this data and leak confidential information or steal money. This is why
it is necessary to secure your network.
Network security, is an important part of cyber security and, helps in protecting your
network and data stored in it from breaches, software and hardware intrusion, and more.
Network security defines a set of important rules, regulations, and configurations based on
threats, network use, accessibility, and complete threat security.
Types of Network Security:
In the field of network security, there are multiple components working together to ensure
the security of data and networks. Based on this, there are several different types of
network security:
Firewalls
Access Control
Virtual Private Networks (VPNs)
Intrusion Prevention Systems
Wireless Security
Application Security
Behavioural Analytics
Firewalls are services or devices that act as guards responsible for deciding which web page,
pop up, and other services enter and exit a network. These firewalls use apredefined set of
rules that assist in blocking or allowing traffic, depending on the requirements. Firewalls can
be for software, hardware, or both, depending on the needs of the system.
Access Control
Access control allows companies to prevent potential attackers from invading confidential
information and to block unauthorized devices and users from accessing
the given network. This allows only those users to access the network who are permitted to
work with the given resources.
Virtual Private Networks (VPNs)
A VPN generally uses the internet to encrypt the connection between an endpoint device
and a network. Further, VPN allows professionals to authenticate the
12002040501027
51
Information and Network Security
102046708
communication between the network and the device. This results in building an encrypted
and secure tunnel via the internet.
Intrusion Prevention Systems
Intrusion prevention systems find and prevent attacks by scanning network traffic. Thisis done
by using databases of attack techniques that professionals are familiar with and correlating
them with network activities.
Wireless Security
Wired networks are not as secure as wireless networks. It is necessary for you to control the
devices and users that can access your company’s network. Hence, it is important for you to
have wireless security, especially when cybercriminals are rapidly targeting confidential
information for extortion.
Application security involves a set of software, hardware, and processes that track andlock the
weak points of an application to be easily be targeted by attackers to infiltrateyour network.
Behavioural Analytics
If you want to be able to identify anomalies and various network breaches as and whenthey
occur, you need to have a clear idea of the normal behaviour of your network. There are
varied behavioural analytics tools available that automatically spot abnormalactivities.
Further in this blog, you will read about the several tools that can be used by network
security experts to protect networks.
Top Network Security Tools
Some of the security tools, hardware, and software that are necessary to ensure that the
network is, indeed, secure are listed below:
Wireshark
Nessus
Snort
Netcat
Metasploit
Aircrack
BackTrack
Cain and Abel
After gaining insights into the tools that are used to secure networks, let us now understand
what is a network security attack and how it can corrupt the security of anynetwork.
Network Security Attack
Network security attack is malicious attempts that are carried out by cybercriminals to
compromise the security of a network. These attacks are the reasons why there is a
12002040501027
52
Information and Network Security
102046708
great need for network security. Network security is responsible for preventing these
attacks on the methods to prevent them.
Types of Attacks in Network Security
Some of the different types of network security attacks are mentioned below:Virus
It is a malicious file that is downloadable, and once opened by a user, it starts to replace the
codes in the computer with its own set of codes. On spreading, the system files in the
computer will be corrupted, which can result in the corruption of the files ofother computer
systems in the network.
Malware
It is among the severe-most and fastest types of malicious attacks that help gain
unauthorized access to a system or network of systems. Malware is generally selfreplicating, i.e., once a system is corrupted, malware gains entry through the internet and
easily corrupts all computer systems that are connected to the network via the internet. In
the case of malware, even an external device connected to the system willget corrupted.
Worm
It enters a given system without the need of a user. If a user is running an applicationthat is
not too strong, any attacker or hacker using the same internet connection can easily send
malware to that app. Without the knowledge of the user, the application could accept and
execute this malware over the internet, leading to the creation of a worm. Ethical hackers
are in high demand to prevent this type of network security attack.
Packet sniffer
If a user places a passive receiver in the region of a wireless transmitter, then it ends up
seeing a copy of the transmitted packets. Often, these packets consist of confidential
organization data, trade secrets, etc., which can get through to the packet receiver. The
packet
range. Cryptography is the best way to prevent this form of network security attack.
Phishing
This is one of the most common forms of attacks on network security. In this, attackerssend
emails to users pretending to be from a known source, such as investors and bankers, and
building a sense of urgency to catch the users’ attention and/or excite them. These emails
have probable chances of containing malicious attachments or links, which ask usersto share
confidential data.
12002040501027
53
Information and Network Security
102046708
Compromised key
When an attacker gets a network security key, it is known as a compromised key thatacts
as a tool to extract sensitive data. In this case, the attacker uses a compromisedkey and
gets unauthorized access to secure data. This key comprises of a code or number that
assists in interpreting secure data without any notification to the sender or receiver.
Botnet
It is a malicious software that attacks a set of computers connected through a private
network. The attacker gains access and controls all the systems on that network without
the knowledge of the owner. All the computers on that network are referred toas zombies
that spread and corrupt a large number of devices as per the instructionsof the attacker.
DoS
DoS is known as denial of service. This attack is capable of destroying the users’ networks
partially or completely. DoS can also attack even a complete IT infrastructure, making it
unavailable to the actual users. DoS attacks can generally be classified into three
categories, namely, connection flooding, vulnerability attacks, andbandwidth flooding.
12002040501027
54
Download