Cryptography
Cryptography
INSTITUTE OF TECHNOLOGY
(AUTONOMOUS)
(Approved by AICTE New Delhi & Affiliated to JNTU Kakinada)
Practical Record
Name:- ......................................................................................................................
Roll NO:
Certificate
This is to certify that the Bonafide Record of the Laboratory work done by
Mr/Ms………………………………………………………………………………………………………
20 -20
Description :
It's important to note that the AND, OR and XOR operations are bitwise operations, which
means that they operate on the individual bits of a value. However, when you're working with
characters in C, they are usually represented as integers, and performing bitwise operations on
them will give you an integer result which is not in human-readable format.
Here is an example of a program in C that performs the desired operation using bitwise AND,
OR, and XOR operators, then it prints the integer representation of the modified characters.
PROGRAM:
#include <stdio.h>
int main()
{
char str[] = "Hello World";
int i;
for (i = 0; str[i] != '\0'; i++)
{
str[i] = (str[i] & 127) | (str[i] | 127) ^ (str[i] ^ 127);
printf("%d ", str[i]);
}
printf("\n");
return 0;
}
Page 1 of 58
OUTPUT:
RESULT:
Thus the implementation of Implement program that contains a string (char pointer) with a value ‘Hello
World’. The program should AND or and XOR each character in this string with 127 and display the
result had been executed successfully.
Page 2 of 58
IMPLEMENTATION OF CAESAR CIPHER
AIM:
To implement the simple substitution technique named Caesar cipher using C language.
DESCRIPTION:
To encrypt a message with a Caesar cipher, each letter in the message is changed
using a simple rule: shift by three. Each letter is replaced by the letter three letters ahead in
the alphabet. A becomes D, B becomes E, and so on. For the last letters, we can think of the
alphabet as a circle and "wrap around". W becomes Z, X becomes A, Y bec omes B, and Z
becomes C. To change a message back, each letter is replaced by the one three before it.
EXAMPLE:
ALGORITHM:
Page 3 of 58
PROGRAM: (Caesar Cipher)
#include <stdio.h>
#include <string.h>
void encrypt(char *plaintext, int key)
{
int i;
int length = strlen(plaintext);
char ch;
for(i = 0; i < length; i++)
{
ch = plaintext[i];
if(ch >= 'a' && ch <= 'z')
{
ch = ch + key;
if(ch > 'z') {
ch = ch - 'z' + 'a' - 1;
}
plaintext[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z')
{
ch = ch + key;
if(ch > 'Z') {
ch = ch - 'Z' + 'A' - 1;
}
plaintext[i] = ch;
}
}
}
Page 4 of 58
void decrypt(char *ciphertext, int key)
{
int i;
int length = strlen(ciphertext);
char ch;
for(i = 0; i < length; i++) {
ch = ciphertext[i];
if(ch >= 'a' && ch <= 'z')
{
ch = ch - key;
if(ch < 'a') {
ch = ch + 'z' - 'a' + 1;
}
ciphertext[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z')
{
ch = ch - key;
if(ch < 'A')
{
ch = ch + 'Z' - 'A' + 1;
}
ciphertext[i] = ch;
}
}
}
Page 5 of 58
int main()
{
char plaintext[100];
int key;
printf("Enter plaintext: ");
scanf("%s", plaintext);
printf("Enter key: ");
scanf("%d", &key);
encrypt(plaintext, key);
printf("Ciphertext: %s\n", plaintext);
decrypt(plaintext, key);
printf("Decrypted plaintext: %s\n", plaintext);
return 0;
}
Page 6 of 58
OUTPUT:
RESULT:
Page 7 of 58
IMPLEMENTATION OF PLAYFAIR CIPHER
AIM :
To write a C program to implement the Playfair Substitution technique
DESCRIPTION:
The Playfair cipher starts with creating a key table. The key table is a 5×5 grid of
letters that will act as the key for encrypting your plaintext. Each of the 25 letters must be
unique and one letter of the alphabet is omitted from the table (as there are 25 spots and 26
letters in the alphabet).
To encrypt a message, one would break the message into digrams (groups of 2 letters)
such that, for example, "HelloWorld" becomes "HE LL OW OR LD", and map them out on
the key table. The two letters of the diagram are considered as the opposite corners of a
rectangle in the key table. Note the relative position of the corners of this rectangle. Then
apply the following 4 rules, in order, to each pair of letters in the plaintext:
1. If both letters are the same (or only one letter is left), add an "X" after the first letter
2. If the letters appear on the same row of your table, replace them with the letters to
their immediate right respectively
3. If the letters appear on the same column of your table, replace them with the letters
immediately below respectively
4. If the letters are not on the same row or column, replace them with the letters on the
same row respectively but at the other pair of corners of the rectangle defined by the
original pair.
EXAMPLE:
Page 8 of 58
ALGORITHM:
Page 9 of 58
if (w == y)
{
if(choice==1)
{
x = (x + 1) % 5;
z = (z + 1) % 5;
}
else{
x = ((x - 1) % 5+5)%5;
z = ((z - 1) % 5+5)%5;
}
printf("%c%c", key[w][x], key[y][z]);
}
else if (x == z)
{
if(choice==1) {
w = (w + 1) % 5;
y = (y + 1) % 5;
}
else{
w = ((w - 1) % 5+5)%5;
y = ((y - 1) % 5+5)%5;
}
printf("%c%c", key[w][x], key[y][z]);
}
else {
printf("%c%c", key[w][z], key[y][x]);
}
}
Page 10 of 58
void removeDuplicates(char str[])
{
int hash[256] = {0};
int currentIndex = 0;
int lastUniqueIndex = 0;
while(*(str+currentIndex))
{
char temp = *(str+currentIndex);
if(0 == hash[temp]){
hash[temp] = 1;
*(str+lastUniqueIndex) = temp;
lastUniqueIndex++;
}
currentIndex++;
}
*(str+lastUniqueIndex) = '\0';
}
void main()
{
int i, j, k = 0, l, m = 0, n;
char key[MX][MX], keyminus[25], keystr[10], str[25] = {
0
};
char alpa[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'
};
Page 11 of 58
printf("\n1.Encryption\n2.Decryption\n\nChoice(1 or 2):");
scanf("%d",&choice);
if(choice!=1 && choice!=2){ printf("Invalid Choice"); return;}
fflush(stdin);
printf("\nEnter key:");
gets(keystr);
printf("Enter the text:");
gets(str);
removeDuplicates(keystr);
n = strlen(keystr);
//convert the characters to uppertext
for (i = 0; i < n; i++)
{
if (keystr[i] == 'j') keystr[i] = 'i';
else if (keystr[i] == 'J') keystr[i] = 'I';
keystr[i] = toupper(keystr[i]);
}
//convert all the characters of plaintext to uppertext
for (i = 0; i < strlen(str); i++)
{
if (str[i] == 'j') str[i] = 'i';
else if (str[i] == 'J') str[i] = 'I';
str[i] = toupper(str[i]);
}
// store all characters except key
j = 0;
for (i = 0; i < 26; i++)
{
for (k = 0; k < n; k++)
{
if (keystr[k] == alpa[i]) break;
else if (alpa[i] == 'J') break;
}
Page 12 of 58
if (k == n) {
keyminus[j] = alpa[i];
j++;
}
}
//construct key keymatrix
k = 0;
for (i = 0; i < MX; i++)
{
for (j = 0; j < MX; j++)
{
if (k < n)
{
key[i][j] = keystr[k];
k++;
}
else {
key[i][j] = keyminus[m];
m++;
}
printf("%c ", key[i][j]);
}
printf("\n");
}
Page 13 of 58
// construct diagram and convert to cipher text
printf("\nEntered text :%s\nOutput Text :", str);
for (i = 0; i < strlen(str); i++)
{
if (str[i] == 'J') str[i] = 'I';
if (str[i + 1] == '\0') playfair(str[i], 'X', key);
else
{
if (str[i + 1] == 'J') str[i + 1] = 'I';
if (str[i] == str[i + 1]) playfair(str[i], 'X', key);
else
{
playfair(str[i], str[i + 1], key);
i++;
}
}
}
if(choice==2) printf(" (Remove unnecessary X)");
}
Page 14 of 58
OUTPUT:
RESULT:
Thus the Playfair cipher substitution technique had been implemented successfully.
Page 15 of 58
IMPLEMENTATION OF HILL CIPHER
AIM:
To write a C program to implement the hill cipher substitution techniques
DESCRIPTION:
Each letter is represented by a number modulo 26. Often the simple scheme
a A = 0, B= 1... Z
= 25, is used, but this is not an essential feature of the cipher. To encrypt
u a message, each
block of n letters is multiplied by an invertible n × n matrix, against modulus 26. To decrypt
the message, each block is multiplied by the inverse of the matrix used for encryption. The
matrix used for encryption is cipher key and it should be chosen randomly from the set of
invertible n × n matrices (modulo 26).
EXAMPLE :
ALGORITHM:
STEP-1: Read the plain text and key from the user.
STEP-2: Split the plain text into groups of length three.
STEP-3: Arrange the keyword in a 3*3 matrix.
STEP-4: Multiply the two matrices to obtain the cipher text of length three.
STEP-5: Combine all these groups to get the complete cipher text.
Page 16 of 58
Program :
#include<stdio.h>
#include<math.h>
float encrypt[3][1], decrypt[3][1], a[3][3], b[3][3], mes[3][1], c[3][3];
void encryption(); //encrypts the message
void decryption(); //decrypts the message
void getKeyMessage(); //gets key and message from user
void inverse(); //finds inverse of key matrix
void 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];
printf("\nEncrypted string is: ");
for(i = 0; i < 3; i++)
printf("%c", (char)(fmod(encrypt[i][0], 26) + 97));
Page 17 of 58
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];
printf("\nDecrypted string is: ");
for(i = 0; i < 3; i++)
printf("%c", (char)(fmod(decrypt[i][0], 26) + 97));
printf("\n");
}
void getKeyMessage()
{
int i, j;
char msg[3];
Page 18 of 58
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;
}
for(k = 0; k < 3; k++)
{
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];
}
}
}
}
Page 19 of 58
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
b[i][j] = b[i][j] / c[i][i];
printf("\n\nInverse Matrix is:\n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
printf("%d ", b[i][j]);
printf("\n");
}
}
Page 20 of 58
OUTPUT:
RESULT:
Thus the hill cipher substitution technique had been implemented successfully in C.
Page 21 of 58
IMPLEMENTATION OF VIGENERE CIPHER
AIM:
To encrypt, a table of alphabets can be used, termed a tabula recta, Vigenère square, or
Vigenère table. It consists of the
o alphabet written out 26 times in diffe rows,e each alphabet
shifted cyclically to the left compared to the previous alphabet, corresponding to the 26 possible
Caesar ciphers. At different points in the encryption process, the cipher uses a different
alphabet from one of the rows. The alphabet used at each point repeating keyword.
Each row starts with a key letter. The remainder of the row holds the letters A to Z.
Although there are 26 key rows shown, you will only use as many keys as there are unique
letters in the key string, here just 5 keys, {L, E, M, O, N}. For successive letters of the
message, we are going to take successive letters of the key string, and encipher each message
letter using its corresponding key row. Choose the next letter of the key, go al ong that row to
find the column heading that attaches
m the message character; the letter at the intersection of
[key-row, msg-col] is the enciphered letter
EXAMPLE:
Page 22 of 58
ALGORITHM:
include <stdio.h>
#include<conio.h>
#include <ctype.h>
#include <string.h>
void encipher();
void decipher();
void main()
int choice;
clrscr();
while(1)
printf("\t3. Exit");
scanf{"%d",&choice);
if(choice == 3)
exit(0);
Page 23 of 58
else if(choice == 1)
encipher();
else if(choice == 2)
decipher();
else
void encipher()
char input[50],key[10];
scanf("%s" input);
scanf("%s" key);
for(i=0,j=0;i<strlen(input);i++j++)
if(j>=strlen(key))
j=0;
printf("%c",65+(((toupper(input[i])-65)+(toupper(key[j)]- 65))%26));
Page 24 of 58
void decipher()
char input[50],key[10];
int value;
scanf("%s" input);
scanf("%s" key);
for(i=0,j=0;i<strlen(input);i++,j++)
if(j>=strlen(key))
J=0;
value = (toupper(input[i])-64)-(toupper(key[j]))-64);
Page 25 of 58
OUTPUT:
RESULT:
Thus the Vigenere Cipher substitution technique had been implemented successfully.
Page 26 of 58
IMPLEMENTATION OF RAIL FENCE – ROW &
COLUMN TRANSFORMATION TECHNIQUE
AIM:
DESCRIPTION:
In the rail fence cipher, the plain text is written downwards and diagonally on
successive "rails" of an imaginary fence, then moving up when we reach the bottom rail.
When we reach the top rail, the message is written downwards again until the whole plaintext
is written out. The message is then read off in rows.
EXAMPLE:
ALGORITHM:
Page 27 of 58
PROGRAM: (Rail Fence)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
int i,j,len,rails,count,code[100][1000];
char str[1000];
gets(str);
len=strlen(str);
scanf("%d",&rails);
for(i=0;i<rails;i++)
for(j=0;j<len;j++)
code[i][j]=0;
count=0;
j=0;
while(j<len)
if(count%2==0)
Page 28 of 58
for(i=0;i<rails;i++)
//strcpy(code[i][j],str[j]);
code[i][j]=(int)str[j];
j++;
else
for(i=rails-2;i>0;i--)
code[i][j]=(int)str[j];
j++;
count++;
for(i=0;i<rails;i++)
for(j=0;j<len;j++)
if(code[i][j]!=0)
printf("%c",code[i][j]);
printf("\n");
}
Page 29 of 58
OUTPUT:
RESULT:
Page 30 of 58
IMPLEMENTATION OF DES
AIM:
DESCRIPTION:
DES is a symmetric encryption system that uses 64-bit blocks, 8 bits of which are
used for parity checks. The key therefore has a "useful" length of 56 bits, which means that
only 56 bits are actually used in the algorithm. The algorithm involves carrying out
combinations, substitutions and permutations between the text to be encrypted and the key,
while making sure the operations can be performed in both directions. The key is ciphered on
64 bits and made of 16 blocks of 4 bits, generally denoted k 1 to k16. Given that "only" 56 bits
are actually used for encrypting, there can be 256 different keys.
EXAMPLE:
Page 31 of 58
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
int IP[] =
{
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
};
Page 32 of 58
int E[] =
{
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 P[] =
{
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 FP[] =
{
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
};
int S1[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
};
int S2[4][16] =
{
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
};
Page 33 of 58
int S3[4][16] =
{
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
};
int S4[4][16] =
{
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
};
int S5[4][16] =
{
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
};
int S6[4][16] =
{
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
};
int S7[4][16]=
{
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
};
int S8[4][16]=
{
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
};
Page 34 of 58
int PC1[] =
{
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 PC2[] =
{
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
};
int SHIFTS[] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
FILE* out;
int LEFT[17][32], RIGHT[17][32];
int IPtext[64];
int EXPtext[48];
int XORtext[48];
int X[8][6];
int X2[32];
int R[32];
int key56bit[56];
int key48bit[17][48];
int CIPHER[64];
int ENCRYPTED[64];
Page 35 of 58
int initialPermutation(int pos, int text)
{
int i;
for (i = 0; i < 64; i++)
{
if (IP[i] == pos + 1) {
break;
}
}
IPtext[i] = text;
}
int F1(int i)
{
int r, c, b[6];
int j=0;
for ( j = 0; j < 6; j++) {
b[j] = X[i][j];
}
r = b[0] * 2 + b[5];
c = 8 * b[1] + 4 * b[2] + 2 * b[3] + b[4];
if (i == 0) {
return S1[r][c];
}
else if (i == 1) {
return S2[r][c];
}
else if (i == 2) {
return S3[r][c];
}
else if (i == 3) {
return S4[r][c];
}
else if (i == 4) {
return S5[r][c];
}
else if (i == 5) {
return S6[r][c];
}
else if (i == 6) {
return S7[r][c];
}
else if (i == 7) {
return S8[r][c];
}
}
Page 36 of 58
int XOR(int a, int b) {
return (a ^ b);
}
if (i % 32 == 0) {
i = 0;
}
i = i + 4;
}
int value;
i=0;
for ( i = 0; i < 8; i++)
{
value = F1(i);
ToBits(value);
}
}
Page 37 of 58
int PBox(int pos, int text)
{
int i;
for (i = 0; i < 32; i++)
{
if (P[i] == pos + 1) {
break;
}
}
R[i] = text;
}
SBox(XORtext);
i=0;
Page 38 of 58
void convertToBinary(int n)
{
int k, m;
int i=0;
for ( i = 7; i >= 0; i--)
{
m = 1 << i;
k = n & m;
if (k == 0) {
fprintf(out, "0");
}
else {
fprintf(out, "1");
}
}
}
while (i)
{
ch = fgetc(inp);
if (ch == -1) {
break;
}
i--;
convertToBinary(ch);
}
fclose(out);
fclose(inp);
}
Page 39 of 58
for (i = 32; i < 64; i++) {
RIGHT[0][i - 32] = IPtext[i];
}
int k=1;
for (k = 1; k < 17; k++)
{
cipher(k, 0);
int i=0;
for ( i = 0; i < 32; i++)
LEFT[k][i] = RIGHT[k - 1][i];
}
Page 40 of 58
for ( i = 0; i < 64; i++)
{
if (i < 32) {
CIPHER[i] = RIGHT[16][i];
} else {
CIPHER[i] = LEFT[16][i - 32];
}
finalPermutation(i, CIPHER[i]);
}
fclose(out);
}
int bittochar()
{
out = fopen("result.txt", "ab+");
int i=0;
for ( i = 0; i < 64; i = i + 8) {
convertToBits(&ENCRYPTED[i]);
}
fclose(out);
}
Page 41 of 58
int key64to56(int pos, int text)
{
int i;
for (i = 0; i < 56; i++)
{
if (PC1[i] == pos + 1) {
break;
}
}
key56bit[i] = text;
}
k = 0;
for ( i = 28 - shift; i < 28; i++) {
C[x][i] = backup[x - 1][k++];
}
Page 42 of 58
for ( i = 0; i < (28 - shift); i++) {
D[x][i] = D[x - 1][i + shift];
}
k = 0;
for ( i = 28 - shift; i < 28; i++) {
D[x][i] = backup[x - 1][k++];
}
}
int j=0;
for ( j = 0; j < 17; j++)
{
for ( i = 0; i < 28; i++) {
CD[j][i] = C[j][i];
}
while (!feof(in))
{
ch = getc(in);
plain[++i] = ch - 48;
}
fclose(in);
}
Page 43 of 58
void encrypt(long int n)
{
FILE* in = fopen("bits.txt", "rb");
while (!feof(in))
{
ch = getc(in);
plain[++i] = ch - 48;
}
fclose(in);
}
void create16Keys()
{
FILE* pt = fopen("key.txt", "rb");
unsigned int key[64];
int i = 0, ch;
while (!feof(pt))
{
ch = getc(pt);
key[i++] = ch - 48;
}
key64to48(key);
fclose(pt);
}
return size;
}
Page 44 of 58
int main()
{
// destroy contents of these files (from previous runs, if any)
out = fopen("result.txt", "wb+");
fclose(out);
create16Keys();
encrypt(n);
decrypt(n);
return 0;
}
Page 45 of 58
OUTPUT:
RESULT:
Thus the data encryption standard algorithm had been implemented successfully
using C language.
Page 46 of 58
IMPLEMENTATION OF RSA
AIM:
DESCRIPTION:
(me)d = m (mod n)
The public key is represented by the integers n and e; and, the private key, by the
integer d. m represents the message. RSA involves a public key and a private key. The public
key can be known by everyone and is used for encrypting messages. The intention is that
messages encrypted with the public key can only be decrypted in a reasonable amount of
time using the private key.
EXAMPLE:
Page 47 of 58
ALGORITHM:
PROGRAM: (RSA)
#include<stdio.h>
#include<conlo.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
long Int p, q, n, t, flag, [100], e[100], temp[100], j, m[100], en[100], i;
char msg(100];
int prime(long int);
void ce();
long int cd(long int);
vold encrypt();
void decrypt();
vold main()
{
printf("\nENTER FIRST PRIME NUMBER\n");
scanf("%d", &p);
flag = prime(p);
if (flag == 0)
{
printf("\nWRONG INPUT\N");
getch();
exit(1);
}
Page 48 of 58
printf("\nENTER ANOTHER PRIME NUMBER\n");
scanf("%d", &a);
flag = prime(q);
if (lag ==0 || p==q)
{
printf("\nWRONG INPUTAN");
getch();
exit(1);
}
printf("\nENTER MESSAGE\n");
fflush(stdin);
scanf("%s", msg);
for (i = 0; msg[i] != NULL; i++)
m[i] = msglil;
n=p*q;
t=(p-1)*(a-1);
ce();
printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
for(i=0;i<j-1; j++)
printf("\n%!Id\t%!d", e[i], dfil);
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;
}
Page 49 of 58
void ce()
{
int k;
k=0;
for(i=2;i<t; i++)
{
if (t%i==0)
continue;
flag = prime(i);
if (flag =1&&il=p &&il=q)
{
e[k] =i;
flag = cd(e[k]);
if (flag > 0)
{
d[k] = flag;
k++;
}
long int cd(long int x)
{
long int k=1;
while (1)
{
k=k+t;
if (k % x ==0)
return (k / x);
}
}
Page 50 of 58
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 IS5\n");
for (i= 0; en[i] != -1; i++)
printf(*%c”, enfil);
}
Page 51 of 58
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 15\n");
for (i=0; m[i] I=-1; i++)
printf("%c", m(il);
}
Page 52 of 58
OUTPUT:
RESULT:
Thus the C program to implement RSA encryption technique had been implemented
successfully
Page 53 of 58
IMPLEMENTATION OF DIFFIE HELLMAN KEY EXCHANGE
ALGORITHM
AIM:
DESCRIPTION:
Diffie–Hellman Key Exchange establishes a shared secret between two parties that can
be used for secret communication for exchanging data over a public network. It is primarily
used as a method of exchanging cryptography keys for use in symmetric encryption algorithms
like AES. The algorithm in itself is very simple. The process begins by having the two parties,
Alice and Bob. Let's assume that Alice wants to establish a shared secret with Bob.
EXAMPLE:
Page 54 of 58
ALGORITHM:
STEP-1: Both Alice and Bob shares the same public keys g and p.
STEP-2: Alice selects a random public key a.
STEP-3: Alice computes his secret key A as ga mod p.
STEP-5: Similarly Bob also selects a public key b and computes his secret key as B
and sends the same back to Alice.
STEP-6: Now both of them compute their common secret key as the other one’s secretkey
power of a mod p.
Page 55 of 58
PROGRAM:
#include<stdio.h>
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;
return power(a,x,n);
int main()
int n,g,x,a,y,b;
scanf("%d%d",&n,&g);
scanf("%d",&x);
a=power(g,x,n);
Page 56 of 58
// second person will choose the y
scanf("%d",&y);
b=power(g,y,n);
return 0;
Page 57 of 58
OUTPUT:
RESULT:
Thus the Diffie-Hellman key exchange algorithm had been successfully implemented
using C.
Page 58 of 58