Beruflich Dokumente
Kultur Dokumente
Practical:-1
Aim:- Implement Caesar cipher encryption-decryption.
#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="ATTACKATONCE";
int s = 4;
cout << "Text : " << text;
cout << "\nShift: " << s;
cout << "\nCipher: " << encrypt(text, s);
return 0;
}
OUTPUT:
1
INFORMATION AND NETWORK SECURITY 170283116043
Practical:-2
Aim:- Implement Monoalphabetic cipher encryption-decryption.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
char keyl[26] = {'k', 'd', 'j', 'i', 'l', 'w', 'y', 'o', 'r', 't', 'a', 'c', 'e', 'h', 'b', 'u', 'x', 'v', 'p', 'z', 'n', 'q', 'f', 's',
'm', 'g' };
char keyu[26] = {'K', 'D', 'J', 'I', 'L', 'W', 'Y', 'O', 'R', 'T', 'A', 'C', 'E', 'H', 'B', 'U', 'X', 'V', 'P', 'Z', 'N',
'Q', 'F', 'S', 'M', 'G' };
void take_input(char *text)
{
int i=0;
char c;
while(1)
{
c=getch();
if(c==13)
{
text[i]='\0';
break;
}
else if(c==8)
{
putch('\b');
putch(' ');
putch('\b');
i--;
}
else if(c!=32)
{
if((c>=65 && c<=90) || (c>=97 && c<=122))
{
printf("%c",c);
text[i]=c;
i++;
}
}
}
}
int position(char c)
{
int i;
2
INFORMATION AND NETWORK SECURITY 170283116043
for(i=0;i<26;i++)
{
if(c==keyl[i] || c==keyu[i])
{
return i;
}
}
}
void encrypt()
{
int i=0,len;
char ct[200],pt[200],temp;//ct=cipher text,pt=plain text
printf("Enter message to encrypt:\n");
take_input(pt);
len=strlen(pt);
ct[i]='\0';
printf("\nEncrypted Message:\t%s\n",ct);
}
void decrypt()
{
int i=0,len;
char temp,ct[200],pt[200];
printf("Enter message to decrypt:\n");
take_input(ct);
len=strlen(ct);
while(i<len)
{
temp=ct[i];
if(isupper(temp))
pt[i]=position(temp)+65;
else
pt[i]=position(temp)+97;
i++;
}
pt[i]='\0';
printf("\nDecrypted Message:\t%s\n",pt);
}
int main()
{
int choice;
while(1)
{
printf("Enter Your choice:\n1.Encrypt\t2.Decrypt\t3.Exit\n");
3
INFORMATION AND NETWORK SECURITY 170283116043
scanf("%d",&choice);
switch(choice)
{
case 1:
{
encrypt();
break;
}
case 2:
{
decrypt();
break;
}
case 3:
break;
default:
{
printf("Enter a valid choice\n");
}
}
if(choice==3)
break;
}
return 0;
}
OUTPUT:
4
INFORMATION AND NETWORK SECURITY 170283116043
Practical :- 3
Aim :- Implement playfair cipher.
#include<bits/stdc++.h>
using namespace std;
int main()
{
string key, a="";
cout<<"ENTER THE KEY:-";
cin>>key;
for(int i=0;i<key.length();i++)
if(key[i]>=int('a'))
key[i]-=32;
key+="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i_j=0;
for(int i=0;i<key.length();i++) {
if(key[i]=='I'||key[i]=='J'){
if(i_j==0)
a.push_back('I');
i_j++; }
else{
if(a.find(key[i])>key.length())
a.push_back(key[i]); } }
int I=0,J=0;
char A[5][5];
for(int i=0;i<a.length();i++) {
if(I==5){ I=0; J++; }
A[J][I]=a[i];
I++; }
for(int i=0;i<5;i++){
for(int j=0;j<5;j++)
cout<<A[i][j]<<" ";
cout<<endl; }
string m,str="";
cout<<"ENTER THE MESSAGE:-";
cin>>m;
for(int i=0;i<m.length();i++){
if(m[i]>=int('a'))
m[i]-=32;
if(m[i]=='J')
m[i]='I'; }
for(int i=0;i<m.length();i++)
{
if((i+1)<m.length()&&m[i]!=m[i+1])
{
5
INFORMATION AND NETWORK SECURITY 170283116043
str+=m.substr(i,2);
i++;
}
else
{
str.push_back(m[i]);
str.push_back('X');
}
}
for(int i=0;i<str.length();i=i+2)
cout<<str.substr(i,2)<<"\t";
cout<<endl;
for(int i=0;i<str.length();i=i+2)
{
int f=a.find(m[i]),s=a.find(m[i+1]);
int fx=f/5,fy=f%5;
int sx=s/5,sy=s%5;
if(fx==sx)
cout<<A[fx][(fy+1)%5]<<A[sx][(sy+1)%5];
else if(fy==sy)
cout<<A[(fx+1)%5][fy]<<A[(sx+1)%5][sy];
else
cout<<A[fx][sy]<<A[sx][fy];
cout<<"\t";
}
}
OUTPUT:
6
INFORMATION AND NETWORK SECURITY 170283116043
Practical:-4
Aim:- Implement Polyalphabetic cipher encryption-decryption.
#include<stdio.h>
#include<string.h>
int main()
{
char plaintext[20], key_value[20], cipher_text[20];
int i, j = 0;
printf("\nEnter Plain-Text to Encrypt:\t");
scanf("%[^\n]s", plaintext);
fflush(stdin);
printf("\nEnter key value:\t");
scanf("%[^\n]s", key_value);
for(i = 0; i < strlen(plaintext); i++)
{
cipher_text[i] = plaintext[i] + (key_value[j] - 97);
j = j + 1;
if(j == strlen(key_value))
{
j = 0;
}
}
printf("\nPolyalphabetic Cipher Text:\t%s", cipher_text);
return 0;
}
OUTPUT:
7
INFORMATION AND NETWORK SECURITY 170283116043
Practical:-5
Aim:- 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++)
for(k = 0; k < 3; k++)
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:\n";
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
cin>>a[i][j];
c[i][j] = a[i][j];}
8
INFORMATION AND NETWORK SECURITY 170283116043
9
INFORMATION AND NETWORK SECURITY 170283116043
Practical:-6
Aim:- To implement simple AES or DES.
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
using namespace std;
void setB(int &i , int in,int loc){
int hl=0;
hl=1<<31-(loc-1);
if (in)
i=i|hl;
else
i=i&~hl;
}
int getB(int i ,int loc){
i=i>>31-(loc-1);
return i&1;
}
int getV(int in,int b,int e){
int sum=0;
int t=0;
for (int i=e;i>=b;i--){
sum+=getB(in,i)<<t;
t+=1;
}
return sum;
}
void pKEY(int &l,int &r){
int
map[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};
int hl=l;
int hr=r;
for (int t=0;t<56;t++){
if (map[t]>32){
if (t>27){
setB(hr,getB(r,map[t]-32),t-27);
}
else{
setB(hl,getB(r,map[t]-32),t+1);
}
}
else{
if (t>27){
setB(hr,getB(l,map[t]),t-27);
10
INFORMATION AND NETWORK SECURITY 170283116043
}
else{
setB(hl,getB(l,map[t]),t+1);
}
}
}
r=hr;
l=hl;
r=r>>4;
r=r<<4;
l=l>>4;
l=l<<4;
}
void pSUBKEY(int &l,int &r){
int
map[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,4
5,33,48,44,49,39,56,34,53,46,42,50,36,29,32
};
int hl=l;
int hr=r;
for (int t=0;t<48;t++){
if (map[t]>28){
if (t>23){
setB(hr,getB(r,map[t]-28),t-23);
}
else{
setB(hl,getB(r,map[t]-28),t+1);
}
}
else{
if (t>23){
setB(hr,getB(l,map[t]),t-23);
}
else{
setB(hl,getB(l,map[t]),t+1);
}
}
}
r=hr;
l=hl;
r=r>>8;
r=r<<8;
l=l>>8;
l=l<<8;
}
void pIP(int &l,int &r){
11
INFORMATION AND NETWORK SECURITY 170283116043
int
map[64]={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};
int hl=l;
int hr=r;
for (int t=0;t<64;t++){
if (map[t]>32){
if (t>31){
setB(hr,getB(r,map[t]-32),t-31);
}
else{
setB(hl,getB(r,map[t]-32),t+1);
}
}
else{
if (t>31){
setB(hr,getB(l,map[t]),t-31);
}
else{
setB(hl,getB(l,map[t]),t+1);
}
}
}
r=hr;
l=hl;
}
void funcE(int &l,int &r){
int
map[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,2
5,24,25,26,27,28,29,28,29,30,31,32,1};
int hl=l;
int hr=r;
for (int t=0;t<48;t++){
if (t>23){
setB(hr,getB(r,map[t]),t-23);
}
else{
setB(hl,getB(r,map[t]),t+1);
}
}
r=hr;
l=hl;
r=r>>8;
r=r<<8;
l=l>>8;
l=l<<8;
}
12
INFORMATION AND NETWORK SECURITY 170283116043
13
INFORMATION AND NETWORK SECURITY 170283116043
setB(hl,getB(r,map[t]-32),t+1);
}
}
else{
if (t>31){
setB(hr,getB(l,map[t]),t-31);
}
else{
setB(hl,getB(l,map[t]),t+1);
}
}
}
r=hr;
l=hl;
}
int lastP(int s){
int
map[32]={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};
int ss=0;
for (int t=0;t<32;t++){
setB(ss,getB(s,map[t]),t+1);
}
return ss;
}
int funcF(int r,int kl,int kr){
int er=r,el=0;
funcE(el,er);
kl=kl^el;
kr=kr^er;
int b=0;
int s=0;
for (int i=8;i>0;i--){
if (i>4){
b=getV(kr,i*6-24-5,i*6-24);
}else {
b=getV(kl,i*6-5,i*6);
}
int column=getV(b,28,31);
int row=getV(getV(b,32,32)|(getV(b,27,27)<<1),30,32);
s+=funcS(i,row,column)<<(8-i)*4;
}
return lastP(s);
}
int shiftL(int inp,int size,int rep){
for (int i=0;i<rep;i++){
int bit=0;
bit=getB(inp,1);
14
INFORMATION AND NETWORK SECURITY 170283116043
bit=bit<<32-size;
inp=inp<<1;
inp=inp|bit;
}
return inp;
}
string cInt(long long int number)
{
char t[8];
for (int i=0;i<8;i++){
t[i]=((char)(number>>i*8));
}
string s(t);
return s;
}
15
INFORMATION AND NETWORK SECURITY 170283116043
r0=l0;
l0=rt;
finalP(l0,r0);
long long int final=l0;
final=final<<32;
final+=r0;
cout<<"[ "<<"Cipher int:#### "<<hex<<final<<" ####\nChipher Left INT:= "<<l0<<" Right
INT:= "<<r0<<endl;
}
int main (){
long long int keya[8]={ 56,97,111,106,99,126,110,122};
long long int msga[8]={110,111,99,105,91,113,121,49};
long long int msg=0;
long long int key=0;
for (int i=0;i<8;i++){
msg+=(msga[i]<<((7-i)*8));
}
for (int i=0;i<8;i++){
key+=(keya[i]<<((7-i)*8));
}
cout<<"[ Your Msg: "<<msg<<" ]"<<endl;
cout<<"[ Your Key: "<<key<<" ]"<<endl;
Cipher(msg,key);
return 0;
}
OUTPUT:
16
INFORMATION AND NETWORK SECURITY 170283116043
Practical:-7
Aim:- Implement Differ-Hellmen key exchange method.
#include<stdio.h>
#include<math.h>
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 = 13;
printf("The value of P : %lld\n", P);
G = 19;
printf("The value of G : %lld\n\n", G);
a = 6;
printf("The private key a for Alice : %lld\n", a);
x = power(G, a, P);
b = 2;
printf("The private key b for Bob : %lld\n\n", b);
y = power(G, b, P);
ka = power(y, a, P);
kb = power(x, b, P);
printf("Secret key for the Alice is : %lld\n", ka);
printf("Secret Key for the Bob is : %lld\n", kb);
return 0;
}
OUTPUT:
17
INFORMATION AND NETWORK SECURITY 170283116043
Practical:-8
Aim:- Implement RSA Encryption Decrpytion algorithm.
#include<bits/stdc++.h>
using namespace std;
int binary(int num)
{
if(num==1||num==0)
return 1;
else
return (num%2)+binary(num/2)*10;
}
18
INFORMATION AND NETWORK SECURITY 170283116043
CIPHER.push_back(c);
cout<<c<<"\t";
}
cout<<endl<<"DECRPTED MESSAGE"<<endl;
for(int i=0;i<CIPHER.size();i++)
{
int c=CIPHER[i];
int p=SandM(c,D,N);
char data=p;
cout<<data;
}
}
OUTPUT:
20
INFORMATION AND NETWORK SECURITY 170283116043
Practical:-9
Aim:- Write a program to generate SHA-1 hash.
<!DOCTYPE html>
<html>
<body>
<?php
$str = "Friendship never ends";
echo sha1($str);
?>
</body>
</html>
OUTPUT:
21
INFORMATION AND NETWORK SECURITY 170283116043
Practical:-10
Aim:- Implement algorithm to implement digital signature.
<!DOCTYPE html>
<html>
<body>
<?php
$str = "How are you";
$mymd5 = md5($str);
file_put_contents("data.txt",$str);
$encrypt = "done" + $mymd5;
file_put_contents("sign",$encrypt);
$data2 = file_get_contents("sign");
$md = "done"+md5_file("data.txt");
if($md == $data2)
{
echo "signature verified";
}
?>
</body>
</html>
OUTPUT:
22
INFORMATION AND NETWORK SECURITY 170283116043
Practical:-11
Aim:- Perform various encryption decryption techniques with cryptool.
Encryption and Decryption of Caesar Cipher
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.
23
INFORMATION AND NETWORK SECURITY 170283116043
So, when we press the encrypt button, we will get the Ciphertext – “ONARMDYB”.
24
INFORMATION AND NETWORK SECURITY 170283116043
So, when we press the encrypt button, we will get the Ciphertext – “FZIFTOTBXGPO”.
25
INFORMATION AND NETWORK SECURITY 170283116043
So, when we press the encrypt button, we will get the Ciphertext somewhat like –
“TWWNPZOAASWNUHZBNWWGSNBVCSLYPMM”.
So, when we press the encrypt button, we will get the Ciphertext like – “UEBNRAALBKE”.
26
INFORMATION AND NETWORK SECURITY 170283116043
Practical:-12
Aim:- Study and use the wireshark for various network protocol.
27