Académique Documents
Professionnel Documents
Culture Documents
CertifiCate
This is to certify that Kallaypally Karthik of B.Tech IV year I Semester bearing
the Hall-Ticket number 13011A0513 has fulfilled his Network Security and
Cryptography LAB record for the academic year 2016-2017.
Date of Examination_________________________
Internal Examiner
JNTUHCEH
External Examiner
Page |2
S.No.
Write a program to
implement Caesar cipher
Write a program to
implement playfair
cipher
Write a program to
implement hill cipher
Write a program to
implement vigenere
cipher
Write a program to
implement simple DES
Write a program to
implement the RSA
algorithm
Write a program to
implement the Diffie
Hellman Key exchange
algorithm
Write a program to
implement rail fence
cipher
Write a program to
implement double
transposition cipher
10
WireShark program
JNTUHCEH
Date of
Experiment
Page Number
SIGN
Page |3
Program:
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<string.h>
<ctype.h>
JNTUHCEH
Page |4
getchar();
char *text=malloc(sizeof(char)*100);
printf("Enter plain text: ");
gets(text);
char *cipher=ceaser(text,k);
printf("Encrypted text: ");
puts(cipher);
char *decipher=ceaser(cipher,(-1)*k);
printf("Decrypted text: ");
puts(decipher);
return 0;
}
Output:
JNTUHCEH
Page |5
JNTUHCEH
Page |6
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char matrix[5][5];
int pos[26][2];
// int spaces[100];
void key_matrix(char *key){
int i,j;
int key_itr=0;
int flag[26];
for(i=0;i<26;i++){
flag[i]=0;
pos[i][0]=pos[i][1]=-1;
}
for(i=0;i<5;i++){
for(j=0;j<5;j++){
int set=0;
while(set==0){
if(key_itr<strlen(key)){
char val=key[key_itr];
if(val==' '){
key_itr++;
//spaces[key_itr]=1;
}
else{
if(flag[val-'a']==0 ){
if(val=='i'|| val=='j'){
matrix[i][j]=val;
flag['i'-'a']=1;
flag['j'-'a']=1;
pos['i'-'a'][0]=pos['j''a'][0]=i;
pos['i'-'a'][1]=pos['j''a'][1]=j;
key_itr++;
}
else{
matrix[i][j]=val;
flag[val-'a']=1;
pos[val-'a'][0]=i;
pos[val-'a'][1]=j;
key_itr++;
}
set=1;
}
else{
key_itr++;
}
JNTUHCEH
Page |7
}
}
else if(key[key_itr]=='\0'){
int k;
for(k=0;k<26;k++){
if(flag[k]==0){
matrix[i][j]='a'+k;
flag[k]=1;
pos[k][0]=i;
pos[k][1]=j;
if(k+'a'=='i'){
int j_pos=k+1;
flag[j_pos]=1;
pos[j_pos][0]=i;
pos[j_pos][i]=j;
}
break;
}
}
set=1;
}
}
}
}
}
char* crypt(char *text,int z){
char *temp;
temp=malloc(sizeof(char)*100);
int i=0,j=0;
while(text[i]!='\0'){
if(text[i]==' '){
i++;
continue;
}
else{
temp[j]=text[i];
i++;
j++;
}
}
temp[j]='\0';
if(strlen(temp)%2==1){
temp[j]='x';
j++;
temp[j]='\0';
}
//printf("%s\n",temp );
char *crypt_text;
crypt_text=(char *)malloc(sizeof(char)*100);
JNTUHCEH
Page |8
for(i=0;temp[i]!='\0';i+=2){
char p1=temp[i];
char p2;
if(temp[i]==temp[i+1])
p2='x';
else
p2=temp[i+1];
int p1_i=pos[p1-'a'][0];
int p1_j=pos[p1-'a'][1];
int p2_i=pos[p2-'a'][0];
int p2_j=pos[p2-'a'][1];
int c1_i,c1_j,c2_i,c2_j;
if(p1_i==p2_i && p1_j!=p2_j){
c1_i=c2_i=p1_i;
c1_j=(p1_j+z)%5;
c2_j=(p2_j+z)%5;
}
else if(p1_i!=p2_i && p1_j==p2_j){
c1_j=c2_j=p1_j;
c1_i=(p1_i+z)%5;
c2_i=(p2_i+z)%5;
}
else if(p1_i!=p2_i && p1_j!=p2_j) {
c1_i=p1_i;
c1_j=p2_j;
c2_i=p2_i;
c2_j=p1_j;
}
crypt_text[i]=matrix[c1_i][c1_j];
crypt_text[i+1]=matrix[c2_i][c2_j];
}
crypt_text[i]='\0';
return crypt_text;
}
void print_matrix(){
int i,j;
for(i=0;i<5;i++){
for(j=0;j<5;j++){
printf("%c ",matrix[i][j] );
}
printf("\n");
}
printf("-----------------------------------------------------\n\n");
}
int main(int argc, char const *argv[])
{
char *key=malloc(sizeof(char)*100);
char *text=malloc(sizeof(char)*100);
char *cipher,*decipher;
JNTUHCEH
Page |9
Output:
JNTUHCEH
P a g e | 10
Program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int key[2][2]={{3,3},{2,5}};
int ikey[2][2]={{15,17},{20,9}};
char* crypt(char *text,char type){
char *temp=(char*)malloc(sizeof(char)*100);
int i=0,k=0;
while(text[k]!='\0'){
if(text[k]==' '){
k++;
}
else{
temp[i]=text[k];
i++;
k++;
}
}
if(i%2==1){
temp[i]='x';
i++;
}
temp[i]='\0';
i=0;
char *cipher=(char *)malloc(sizeof(char)*100);
while(temp[i]!='\0'){
JNTUHCEH
P a g e | 11
int j,k;
if(type=='e'){
for(k=0;k<2;k++){
int sum=0;
for(j=0;j<2;j++)
sum+=key[k][j]*(temp[i+j]-'a');
cipher[i+k]=sum%26+'a';
}
}
else if(type=='d'){
for(k=0;k<2;k++){
int sum=0;
for(j=0;j<2;j++)
sum+=ikey[k][j]*(temp[i+j]-'a');
cipher[i+k]=sum%26+'a';
}
}
i+=2;
}
cipher[i]='\0';
return(cipher);
}
int ext_euclid(int a,int m){
int x=1;//ax=1mod(m) or (ax)mod(m)=1
while(1){
if((a*x)%m==1)
return x;
x++;
}
}
void get_key(){
char k[4];
while(1){
printf("Enter key(4 letters): ");
gets(k);
int i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
key[i][j]=(k[i*2+j]-'a')%26;
int det=key[0][0]*key[1][1]-key[0][1]*key[1][0];
det%=26;
if(det==0){
printf("Key cannot be used.Enter a new key\n");
continue;
}
else{
int det_inverse=ext_euclid(det,26);
char adj[2][2];
adj[0][0]=key[1][1];
JNTUHCEH
P a g e | 12
adj[0][1]=(26-key[0][1])%26;
adj[1][0]=(26-key[1][0])%26;
adj[1][1]=key[0][0];
for(i=0;i<2;i++)
for(j=0;j<2;j++)
ikey[i][j]=(det_inverse*adj[i][j])%26;
break;
}
}
}
int main(void) {
get_key();
char *text=(char *)malloc(sizeof(char)*100);
printf("Enter plain text: ");
gets(text);
char *cipher=crypt(text,'e');
printf("Encrypted text: ");
puts(cipher);
char *decipher=crypt(cipher,'d');
printf("Decrypted text: ");
puts(decipher);
return 0;
}
Output:
JNTUHCEH
P a g e | 13
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* encrypt(char *text,char *key){
int keylen=strlen(key);
char *cipher=malloc(sizeof(char)*100);
int i=0,j=0,k=0;//text,key,cipher
while(text[i]!='\0'){
if(text[i]==' '){
i++;
}
else{
cipher[k]=((text[i]-'a')+(key[j]-'a'))%26+'a';
k++;
i++;
j++;
j%=keylen;
}
}
cipher[k]='\0';
return cipher;
}
char* decrypt(char *text,char *key){
int keylen=strlen(key);
char *cipher=malloc(sizeof(char)*100);
int i=0,j=0;//text,key,cipher
while(text[i]!='\0'){
cipher[i]=(text[i]-key[j]+26)%26+'a';
JNTUHCEH
P a g e | 14
i++;
j++;
j%=keylen;
}
cipher[i]='\0';
return cipher;
}
int main(){
char *text,*key;
text=malloc(sizeof(char)*100);
key=malloc(sizeof(char)*100);
printf("Enter plain text: ");
gets(text);
printf("Enter key: ");
gets(key);
char *cipher=encrypt(text,key);
printf("Encrypted text: ");
puts(cipher);
char *decipher=decrypt(cipher,key);
printf("Decrypted text: ");
puts(decipher);
// return 0;
}
Output:
JNTUHCEH
P a g e | 15
The following diagram illustrates the steps taken by the simplified DES (Data
Encryption Standard) in detail.
JNTUHCEH
P a g e | 16
Program:
#include <stdio.h>
#include <stdlib.h>
const
const
const
const
int
int
int
int
initial_permutation[8] = {2,6,3,1,4,8,5,7};
inverse_permutation[8] = {4,1,3,5,7,2,8,6};
expansion_permutation[8] = {4,1,2,3,2,3,4,1};
s1[4][4] = {
{1,0,3,2},
{3,2,1,0},
{0,2,1,3},
{3,1,3,2}
};
const int s2[4][4] = {
{0,1,2,3},
{2,0,1,3},
{3,0,1,0},
{2,1,0,3}
};
const int p10[10] = {3,5,2,7,4,10,1,9,8,6};
const int p8[8] = {6,3,7,4,8,5,10,9};
const int p4[4] = {2,4,3,1};
int k1[8], k2[8];
JNTUHCEH
P a g e | 17
JNTUHCEH
=
=
=
=
0;
0;
1;
1;
s[1]
s[1]
s[1]
s[1]
=
=
=
=
0;
1;
0;
1;
break;
break;
break;
break;
= 0; s[3] = 0; break;
= 0; s[3] = 1; break;
= 1; s[3] = 0; break;
P a g e | 18
JNTUHCEH
P a g e | 19
case
case
case
case
0:
1:
2:
3:
s[0]
s[0]
s[0]
s[0]
}
switch(second)
{
case 0: s[2]
case 1: s[2]
case 2: s[2]
case 3: s[2]
}
=
=
=
=
0;
0;
1;
1;
s[1]
s[1]
s[1]
s[1]
=
=
=
=
0;
1;
0;
1;
break;
break;
break;
break;
=
=
=
=
0;
0;
1;
1;
s[3]
s[3]
s[3]
s[3]
=
=
=
=
0;
1;
0;
1;
break;
break;
break;
break;
JNTUHCEH
P a g e | 20
JNTUHCEH
P a g e | 21
Output:
JNTUHCEH
P a g e | 22
Program:
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<string.h>
<time.h>
P a g e | 23
JNTUHCEH
P a g e | 24
return decipher;
}
int main(){
int p=61,q=53;
int n=p*q;
int t=(p-1)*(q-1);
//printf("p: %d,q: %d,n:%d,t:%d",p,q,n,t);
int e=coprime(t);
int d=ext_euclid(e,t);
//printf(",e: %d,d: %d",e,d);
char *text=malloc(sizeof(char)*100);
printf("Enter plain text: ");
gets(text);
int *cipher=encrypt(text,e,n);
printf("Encrypt text: ");
for(int i=0;i<strlen(text);i++){
printf("%d ",cipher[i] );
}
char *decipher=decrypt(cipher,d,n);
printf("\nDecrypted text: ");
puts(decipher);
return 0;
}
Output:
JNTUHCEH
P a g e | 25
Program:
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<string.h>
<stdbool.h>
JNTUHCEH
P a g e | 26
for(int i=0;i<len;i++){
for(int j=0;j<len;j++){
if(j!=i){
if(arr[i]==arr[j])
return false;
}
}
}
return true;
}
int expWithMod(int a,int b,int n){
//(a^b)%n
a=a%n;
int p=1;
for(int i = 1; i <= b; i++) {
p *= a;
p = p%n;
}
//printf("%d\n",p );
return p;
}
int primitive_root(int p){
for(int i=2;i<p;i++){
if(gcd(i,p)==1){
int a[p-1];
for(int j=0;j<p-1;j++)
a[j]=expWithMod(i,j,p);
if(unique(a,p-1))
return i;
}
}
return -1;
}
int main(){
int p=11;
printf("Enter prime number(p) known to both parties: \n");
scanf("%d",&p);
getchar();
int g=primitive_root(p);
printf("p:%d g:%d\n",p,g );
int xA=6;
int yA=expWithMod(g,xA,p);
int xB=16;
int yB=expWithMod(g,xB,p);
int zA=expWithMod(yB,xA,p);
int zB=expWithMod(yA,xB,p);
printf("private_a: %2d private_b: %2d\n",xA,xB );
printf("public_a: %2d public_b: %2d\n",yA,yB );
JNTUHCEH
P a g e | 27
printf("secret_a:
return 0;
}
Output:
JNTUHCEH
%2d secret_b:
%2d \n",zA,zB );
P a g e | 28
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
char* remove_spaces(char *text){
char *temp=malloc(sizeof(char)*100);
int i=0,j=0;
while(text[i]!='\0'){
if(text[i]!=' '){
temp[j]=text[i];
j++;
}
i++;
}
temp[j]='\0';
return temp;
}
char* encrypt(char *text,int rails){
char *temp=remove_spaces(text);
int itr=0;//cipher itr
char *cipher=(char*)malloc(sizeof(char)*100);
int i,count=0;// count=number of blocks
for(i=0;i<rails;i++){
int block_itr=0;
if(i==0){
while(block_itr<strlen(temp)){
char c=temp[block_itr+i];
cipher[itr]=c;
itr++;
block_itr+=(rails-1)*2;
count++;
}
}
else if(i==(rails-1)){
JNTUHCEH
P a g e | 29
int j;
for(j=0;j<count;j++){
char c=temp[block_itr+i];
if(block_itr+i>=strlen(temp)){
c='@';
}
cipher[itr]=c;
itr++;
block_itr+=(rails-1)*2;
}
}
else{
int j;
for(j=0;j<count;j++){
int jump=(rails-1)*2-(i*2);
int c1=block_itr+i;
int c2=block_itr+i+(jump);
if(c1>=strlen(temp)){
cipher[itr]='@';
cipher[itr+1]='@';
}
else if(c2>=strlen(temp)){
cipher[itr]=temp[c1];
cipher[itr+1]='@';
}
else{
cipher[itr]=temp[c1];
cipher[itr+1]=temp[c2];
}
itr+=2;
block_itr+=(rails-1)*2;
//printf("%c %c ",c1,c2 );
}
}
}
cipher[itr]='\0';
return cipher;
}
char* decrypt(char *text,int rails){
char *cipher=(char*)malloc(sizeof(text));
int text_itr=0,cipher_itr=0;
int i,blank_count=0;
int blocks=strlen(text)/(2*(rails-1));
for(i=0;i<rails;i++){
if(i==0){
cipher_itr=i;
int j;
for(j=0;j<blocks;j++){
char c=text[text_itr];
JNTUHCEH
P a g e | 30
cipher[cipher_itr]=c;
//printf("%c",cipher[cipher_itr]);
cipher_itr+=(rails-1)*2;
text_itr++;
}
}
else if(i==(rails-1)){
cipher_itr=i;
int j=0;
for(j=0;j<blocks;j++){
char c=text[text_itr];
if(c=='@'){
text_itr++;
blank_count++;
}
else{
cipher[cipher_itr]=c;
//printf("%c",cipher[cipher_itr]);
if(j!=rails-1)
cipher_itr+=(rails-1)*2;
text_itr++;
}
}
}
else{
cipher_itr=i;
int j=0;
for(j=0;j<blocks;j++){
char c1=text[text_itr];
char c2=text[text_itr+1];
if(c1=='@'){
text_itr+=2;
blank_count++;
}
else if(c2=='@'){
cipher[cipher_itr]=c1;
//printf("%c",cipher[cipher_itr]);
text_itr+=2;
blank_count++;
}
else{
cipher[cipher_itr]=c1;
//printf("%c",cipher[cipher_itr]);
int jump=(rails-1)*2-i*2;
cipher[cipher_itr+jump]=c2;
if(j!=rails-1)
cipher_itr+=(rails-1)*2;
//printf("%c",cipher[cipher_itr]);
text_itr+=2;
JNTUHCEH
P a g e | 31
}
}
}
}
cipher[cipher_itr]='\0';
return(cipher);
}
void print_array(char *text){
int count=0;
for(int i=0;i<strlen(text);i++){
if(count==5){
printf(" ");
count=0;
}
if(islower(text[i])){
printf("%c",text[i]);
count++;
}
}
printf("\n");
}
int main(){
int rails=0;
printf("Enter rail-count: ");
scanf("%d",&rails);
getchar();
char *text=malloc(sizeof(char)*100);
printf("Enter plain text: ");
gets(text);
char *cipher=encrypt(text,rails);
print_array(cipher);
char *decipher=decrypt(cipher,rails);
print_array(decipher);
return 0;
}
Output:
JNTUHCEH
P a g e | 32
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* remove_spaces(char *text){
char *temp=malloc(sizeof(char)*100);
int i=0,j=0;
while(text[i]!='\0'){
if(text[i]!=' '){
temp[j]=text[i];
j++;
}
i++;
}
temp[j]='\0';
return temp;
}
int* perm(char *text){
int len=strlen(text);
int i,j;
int *order=malloc(sizeof(int)*len);
for(i=0;i<len;i++)
order[i]=i;
for(i=0;i<len-1;i++){
for(j=i+1;j<len;j++){
if(text[i]>text[j]){
char temp=text[i];
text[i]=text[j];
text[j]=temp;
int tempInt=order[i];
order[i]=order[j];
order[j]=tempInt;
}
}
JNTUHCEH
P a g e | 33
}
return order;
}
char *trans(char *text,char *key,char filler){
int text_len=strlen(text);
int key_len=strlen(key);
int no_of_rows;
if(text_len%key_len==0)
no_of_rows=text_len/key_len;
else
no_of_rows=text_len/key_len+1;
char matrix[no_of_rows][key_len];
int i,j;
int itr=0;
//filling matrix
for(i=0;i<no_of_rows;i++){
for(j=0;j<key_len;j++){
if(itr<text_len){
matrix[i][j]=text[itr];
}
else{
matrix[i][j]=filler;
}
itr++;
}
}
char *cipher=malloc(sizeof(char)*100);
int *order=perm(key);
itr=0;
for(j=0;j<key_len;j++){
for(i=0;i<no_of_rows;i++){
cipher[itr]=matrix[i][order[j]];
itr++;
}
}
cipher[itr]='\0';
return cipher;
}
char *untrans(char *text,char* key,char filler){
int text_len=strlen(text);
int key_len=strlen(key);
int no_of_rows;
if(text_len%key_len==0)
no_of_rows=text_len/key_len;
else
no_of_rows=text_len/key_len+1;
char matrix[no_of_rows][key_len];
int i,j;
int itr=0;
JNTUHCEH
P a g e | 34
//filling matrix
int *order=perm(key);
for(j=0;j<key_len;j++){
for(i=0;i<no_of_rows;i++){
matrix[i][order[j]]=text[itr];
itr++;
}
}
char *cipher=malloc(sizeof(char)*100);
itr=0;
for(i=0;i<no_of_rows;i++){
for(j=0;j<key_len;j++){
if(matrix[i][j]!=filler){
cipher[itr]=matrix[i][j];
itr++;
}
}
}
cipher[itr]='\0';
return cipher;
}
char* encrypt(char *text,char* key1,char *key2){
char* cipher=trans(remove_spaces(text),remove_spaces(key1),'X');
cipher=trans(cipher,remove_spaces(key2),'Y');
return cipher;
}
char* decrypt(char *text,char *key1,char *key2){
char* cipher=untrans(text,remove_spaces(key2),'Y');
cipher=untrans(cipher,remove_spaces(key1),'X');
return cipher;
}
int main(){
char *key1=malloc(sizeof(char)*100);
char *key2=malloc(sizeof(char)*100);
char *text=malloc(sizeof(char)*100);
printf("Enter key1: ");
gets(key1);
printf("Enter key2: ");
gets(key2);
printf("Enter plain text: ");
gets(text);
char* cipher=encrypt(text,key1,key2);
printf("Encrypted text: ");
puts(cipher);
char* decipher=decrypt(cipher,key1,key2);
printf("Decrypted text: ");
puts(decipher);
}
JNTUHCEH
P a g e | 35
Output:
JNTUHCEH
P a g e | 36
JNTUHCEH
P a g e | 37
HEX Window
JNTUHCEH
P a g e | 38
Placement of WireShark
JNTUHCEH