Professional Documents
Culture Documents
IS Practical - 200120107533
IS Practical - 200120107533
IS Practical - 200120107533
Practical – 1
CEASER CIPHER
AIM: Understand CEASER CIPHER.
Exercises:
1) Write a C program to implement Caesar cipher encryption and decryption.
Encryption Code:
#include<stdio.h>
int main()
{
char message[100], ch;
int i, key;
printf("Enter a message to encrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}
}
printf("Encrypted message: %s", message);
return 0;
}
Decryption Code:
#include<stdio.h>
int main()
{
char message[100], ch;
int i, key;
printf("Enter a message to decrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch < 'A'){
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;
}
}
printf("Decrypted message: %s", message);
return 0;
}
OUTPUT:
Practical – 2
MONOALPHABETIC CIPHER
AIM: Introduction to MONOALPHABETIC CIPHER.
Exercises:
Write a C program to implement Monoalphabetic cipher encryption-decryption.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char
pt[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
'P','Q','R','S','T','U','V','W','X','Y','Z'};
char
ct[26]={'Z','Y','X','W','V','U','T','S','R','Q','P','O','N','M','L',
'K','J','I','H','G','F','E','D','C','B','A'};
char p[20]={'\0'},c[20]={'\0'},r[20]={'\0'};
int i,j;
Output:
Practical – 3
PLAYFAIR CIPHER
AIM: Introduction to PLAYFAIR CIPHER
Exercises:
Write a C program to implement Playfair cipher encryption-decryption.
Code:
#include<stdio.h>
printf("**********Playfair Cipher************\n\n");
if (key[i] == 'j')
key[i] = 'i';
}
int flag;
int count = 0;
// inserting the key into the table
for (i = 0; i < 5; ++i) {
for (j = 0; j < 5; ++j) {
flag = 0;
while (flag != 1) {
if (count > key_len)
goto l1;
count = 0;
for (i = -1; i < l; ++i) {
char bogus;
if ((l + count) % 2 != 0) {
if (p1[length - 1] == 'x')
p1[length] = 'z';
else
p1[length] = 'x';
}
char cipher_text[length];
int r1, r2, c1, c2;
int k1;
if (r1 == r2) {
cipher_text[k1] = table[r1][(c1 + 1) % 5];
cipher_text[k1 + 1] = table[r1][(c2 + 1) % 5];
}
k1 = k1 + 1;
}//end of for with k1
printf("\n\nThe Cipher text is:\n ");
for (i = 1; i <= length; ++i)
printf("%c ", cipher_text[i]);
}
Output:
Practical – 4
POLYALPHABETIC
AIM: Introduction to POLYALPHABETIC.
Exercises:
Write a C program to implement Polyalphabetic cipher encryption-decryption.
Answer:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char pt[20]={'\0'},ct[20]={'\0'},key[20]={'\0'},rt[20]={'\0'};
int i,j;
// clrscr();
printf("\n enter the plain text:");
scanf("%s",pt);
printf("\n enter the key:");
scanf("%s",key);
Output
Practical – 5
HILL CIPHER
AIM: Introduction to HILL CIPHER.
Exercises:
Write a C program to implement Hill cipher encryption-decryption
Answer:
#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 text\n ");
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 :");
Output:
Practical – 6
DES Algorithm
AIM: Introduction to simple DES algorithm.
Exercises:
Write a C program to implement simple DES
Answer:
#include<stdio.h>
int main()
{
int i, cnt=0, p8[8]={6,7,8,9,1,2,3,4};
int p10[10]={6,7,8,9,10,1,2,3,4,5};
//Applying p10...
for(i=0; i<10; i++)
{
cnt = p10[i];
temp[i] = input[cnt-1];
}
temp[i]='\0';
printf("\nYour p10 key is :");
for(i=0; i<10; i++)
{ printf("%d,",p10[i]); }
//Applying p8...
for(i=0; i<8; i++)
{
cnt = p8[i];
k1[i] = temp[cnt-1];
}
printf("\nYour key k1 is :");
puts(k1);
//This program can be extended to generate k2 as per DES algorithm.
}
Output
Practical – 7
Diffie-Hellman Key Exchange
AIM: Introduction to Diffie-Hellman Key Exchange.
Exercises:
Write a C program to implement Diffie-Hellmen Key exchange Method.
Answer:
#include<stdio.h>
long 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);
a=power(g,x,n);
Output:
Practical – 8
RSA
AIM: Introduction to RSA encryption decryption algorithm.
Exercises:
Write a C program to implement RSA encryption-decryption algorithm.
Answer:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
long int p, q, n, t, flag, e[100], d[100], temp[100], j, m[100],
en[100], i;
char msg[100];
int prime(long int);
void ce();
long int cd(long int);
void encrypt();
void decrypt();
void main()
{
printf("\nENTER FIRST PRIME NUMBER\n");
scanf("%d", &p);
flag = prime(p);
if (flag == 0)
{
printf("\nWRONG INPUT\n");
getch();
exit(1);
}
printf("\nENTER ANOTHER PRIME NUMBER\n");
scanf("%d", &q);
flag = prime(q);
if (flag == 0 || p == q)
{
printf("\nWRONG INPUT\n");
getch();
exit(1);
}
printf("\nENTER MESSAGE\n");
fflush(stdin);
scanf("%s", msg);
for (i = 0; msg[i] != NULL; i++)
m[i] = msg[i];
n = p * q;
t = (p - 1) * (q - 1);
ce();
printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
for (i = 0; i < j - 1; i++)
printf("\n%ld\t%ld", e[i], d[i]);
encrypt();
decrypt();
}
int prime(long int pr)
{
int i;
j = sqrt(pr);
for (i = 2; i <= j; i++)
{
if (pr % i == 0)
return 0;
}
return 1;
}
void ce()
{
int k;
k = 0;
for (i = 2; i < t; i++)
{
if (t % i == 0)
continue;
flag = prime(i);
if (flag == 1 && i != p && i != q)
{
e[k] = i;
flag = cd(e[k]);
if (flag > 0)
{
d[k] = flag;
k++;
}
if (k == 99)
break;
}
}
}
long int cd(long int x)
{
long int k = 1;
while (1)
{
k = k + t;
if (k % x == 0)
return (k / x);
}
}
void encrypt()
{
long int pt, ct, key = e[0], k, len;
i = 0;
len = strlen(msg);
while (i != len)
{
pt = m[i];
pt = pt - 96;
k = 1;
for (j = 0; j < key; j++)
{
k = k * pt;
k = k % n;
}
temp[i] = k;
ct = k + 96;
en[i] = ct;
i++;
}
en[i] = -1;
printf("\nTHE ENCRYPTED MESSAGE IS\n");
for (i = 0; en[i] != -1; i++)
printf("%c", en[i]);
}
void decrypt()
{
long int pt, ct, key = d[0], k;
i = 0;
while (en[i] != -1)
{
ct = temp[i];
k = 1;
for (j = 0; j < key; j++)
{
k = k * ct;
k = k % n;
}
pt = k + 96;
m[i] = pt;
i++;
}
m[i] = -1;
printf("\nTHE DECRYPTED MESSAGE IS\n");
for (i = 0; m[i] != -1; i++)
printf("%c", m[i]);
}
Output:
Practical – 9
Digital signature algorithm
AIM: Introduction to digital signature algorithm.
Exercises:
Write a C program to implement digital signature algorithm.
Answer:
#include <stdio.h> #include <stdlib.h> #include <time.h>
// 64-bit integer type
typedef long long int dlong;
// rational ec point typedef struct { dlong x, y;
} epnt;
// elliptic curve parameters
typedef struct { long a, b; dlong N;
epnt G; dlong r;
} curve;
// signature pair typedef struct { long a, b;
} pair;
// dlong for holding intermediate results,
// long variables in exgcd() for efficiency,
// maximum parameter size 2 * p.y (line 129)
// limits the modulus size to 30 bits.
// maximum modulus
const long mxN = 1073741789;
// max order G = mxN + 65536
const long mxr = 1073807325;
// symbolic infinity
const long inf = -2147483647;
// single global curve
curve e;
// point at infinity zerO
epnt zerO;
// impossible inverse mod N
int inverr;
// return mod(v^-1, u)
long exgcd (long v, long u)
{
register long q, t; long r = 0, s = 1; if (v < 0) v += u;
while (v) {
q = u / v;
t = u - q * v; u = v; v = t; t = r - q * s; r = s; s = t;
}
if (u != 1) {
printf (" impossible inverse mod N, gcd = %d\n", u); inverr = 1;
}
return r;
}
// return mod(a, N)
static inline dlong modn (dlong a)
{
a %= e.N;
if (a < 0) a += e.N;
return a;
}
// return mod(a, r)
dlong modr (dlong a)
{
a %= e.r;
if (a < 0) a += e.r;
return a;
}
// return the discriminant of E
long disc (void)
{
{
long a = i[0], b = i[1];
e.N = i[2]; inverr = 0;
if ((e.N < 5) || (e.N > mxN)) return 0;
e.a = modn(a);
e.b = modn(b);
e.G.x = modn(i[3]);
e.G.y = modn(i[4]);
e.r = i[5];
if ((e.r < 5) || (e.r > mxr)) return 0;
printf ("\nE: y^2 = x^3 + %dx + %d", a, b);
printf (" (mod %lld)\n", e.N); pprint ("base point G", e.G);
printf ("order(G, E) = %lld\n", e.r);
return 1;
}
// pseudorandom number [0..1)
double rnd(void)
{
return rand() / ((double)RAND_MAX + 1);
}
// signature primitive
pair signature (dlong s, long f)
{
long c, d, u, u1; pair sg;
epnt V;
printf ("\nsignature computation\n");
do {
do {
u = 1 + (long)(rnd() * (e.r - 1)); pmul (&V, e.G, u);
c = modr(V.x);
while (c == 0);
u1 = exgcd (u, e.r);
d = modr(u1 * (f + modr(s * c)));
}
while (d == 0);
printf ("one-time u = %d\n", u); pprint ("V = uG", V);
sg.a = c; sg.b = d;
return sg;
}
// verification primitive
int verify (epnt W, long f, pair sg)
{
long c = sg.a, d = sg.b; long t, c1, h1, h2; dlong h;
epnt V, V2;
// domain check
t = (c > 0) && (c < e.r);
t &= (d > 0) && (d < e.r);
if (! t) return 0;
printf ("\nsignature verification\n"); h = exgcd (d, e.r);
h1 = modr(f * h); h2 = modr(c * h);
printf ("h1,h2 = %d, %d\n", h1,h2); pmul (&V, e.G, h1);
pmul (&V2, W, h2);
pprint ("h1G", V);
pprint ("h2W", V2);
padd (&V, V, V2);
pprint ("+ =", V);
if (isO (V)) return 0; c1 = modr(V.x);
printf ("c' = %d\n", c1);
return (c1 == c);
}
// digital signature on message hash f, error bit d
else
break;
}
}
Output:
(tcc, srand(1); first set only)
E: y^2 = x^3 + 355x + 671 (mod 1073741789)
base point G (13693, 10088) order(G, E) = 1073807281
key generation
private key s = 1343570
public key W = sG (817515107, -192163292)
signature verification
h1,h2 = 871754294, 34741072
h1G (708182134, 29830217)
h2W (270156466, -328492261)
+ = (464115167, -267961770)
c' = 464115167
Valid
Practical – 10
CRYPTOOL
AIM: Introduction to cryptool.
Exercises:
Perform any one of the above encryption technique with cryptool..
Answer:
Encryption and Decryption of Hill Cipher
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
Practical – 11
Wireshark
AIM: Study and use the Wireshark for the various network protocols.
Exercises:
Perform Below Exercises using Wireshark
1. Is your browser running HTTP version 1.0 or 1.1? What version of HTTP is the
server Running?
Answer:
2. What languages (if any) does your browser indicate that it can accept to the server?
Answer: en-US (US English) as shown in prior screenshot
4. What is the status code returned from the server to your browser?
Answer: 200 OK (see prior screen- shot)
5.When was the HTML file that you are retrieving last modified at the server?
We can filter messages by http.last_modified and we see that the HTTP response I received
for the html file doesn’t show this field. We do have a http.last_modified field in the favicon
response however, as shown in the screenshot below. This says the favicon was last modified
on Feb 25, 2010.
7.By inspecting the raw data in the packet content window, do you see any headers within the
data that are not displayed in the packet-listing window? If so, name one.
Answer:No. The raw data appears to match up exactly with what is shown in the packet-
listing window.
8.Inspect the contents of the first HTTP GET request from your browser to the server. Do you
see an ―IF-MODIFIED-SINCEǁ line in the HTTP GET?
Answer: No
9.Inspect the contents of the server response. Did the server explicitly return the contents of
the file? How can you tell?
Answer: See screenshot of response with Alice’s Adventures…
10.Now inspect the contents of the second HTTP GET request from your browser to the
server.
Do you see an ―IF-MODIFIED-SINCE:ǁ line in the HTTP GET? If so, what information
follows the ―IF-MODIFIED-SINCE:ǁ header?
Answer: Yes
11.What is the HTTP status code and phrase returned from the server in response to this
second HTTP GET? Did the server explicitly return the contents of the file? Explain.
Answer:
As seen in the previous screenshot, we get a HTTP/1.1 304 Not Modified Response. This is
much shorter than the full response packet seen previously which contained all of Alice in
Wonderland.