Professional Documents
Culture Documents
Jaypee University of Engineering and Technology Guna, M.P.: Open Source Software Lab
Jaypee University of Engineering and Technology Guna, M.P.: Open Source Software Lab
TECHNOLOGY
GUNA, M.P.
SEMESTER- 5
AIM:
To implement the simple substitution technique named Caesar cipher using
C language.
DESCRIPTION:
EXAMPLE:
ALGORITHM:
STEP-1: Read the plain text from the user.
STEP-2: Read the key value from the user.
STEP-3: If the key is positive then encrypt the text by adding the key with
each
character in the plain text.
STEP-4: Else subtract the key from the plain text.
STEP-5: Display the cipher text obtained above.
Program:
#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;
}
Output:
Enter a message to encrypt: axzd
Enter key: 4
Encrypted message: ebdh
Experiment No. 2
IMPLEMENTATION OF PLAYFAIR CIPHER
AIM:
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 "HELLO
WORLD", 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:
ALGORITHM:
Solution:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 30
void toLowerCase(char plain[], int ps)
int i;
plain[i] += 32;
int i, count = 0;
plain[count++] = plain[i];
plain[count] = '\0';
return count;
// a 26 character hashmap
if (key[i] != 'j')
dicty[key[i] - 97] = 2;
}
dicty['j' - 97] = 1;
i = 0;
j = 0;
if (dicty[key[k] - 97] == 2) {
dicty[key[k] - 97] -= 1;
keyT[i][j] = key[k];
j++;
if (j == 5) {
i++;
j = 0;
if (dicty[k] == 0) {
j++;
if (j == 5) {
i++;
j = 0;
}
}
int i, j;
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
if (keyT[i][j] == a) {
arr[0] = i;
arr[1] = j;
else if (keyT[i][j] == b) {
arr[2] = i;
arr[3] = j;
}
int mod5(int a)
return (a % 5);
if (ptrs % 2 != 0) {
str[ptrs++] = 'z';
str[ptrs] = '\0';
return ptrs;
int i, a[4];
if (a[0] == a[2]) {
else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
// Key
ks = strlen(key);
ks = removeSpaces(key, ks);
toLowerCase(key, ks);
// Plaintext
ps = strlen(str);
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
ps = prepare(str, ps);
}
int main()
strcpy(key, "Monarchy");
strcpy(str, "instruments");
encryptByPlayfairCipher(str, key);
return 0;
Output:
Key text: Monarchy
Plain text: instruments
Cipher text: gatlmzclrqtx
Experiment No. 3
IMPLEMENTATION OF HILL CIPHER
AIM:
To implement the hill cipher substitution techniques using C language.
DESCRIPTION:
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.
Solution:
#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 main() {
getKeyMessage();
encryption();
decryption();
void encryption() {
int i, j, k;
void decryption() {
int i, j, k;
inverse();
printf("\n");
void getKeyMessage() {
int i, j;
char msg[3];
scanf("%f", &a[i][j]);
c[i][j] = a[i][j];
scanf("%s", msg);
}
void inverse() {
int i, j, k;
float p, q;
if(i == j)
b[i][j]=1;
else
b[i][j]=0;
p = c[i][k];
q = c[k][k];
if(i != k) {
printf("\n");
Output:
Experiment No. 4
IMPLEMENTATION OF VIGENERE CIPHER
AIM:
To implement the Vigenere cipher substitution techniques using C
language.
DESCRIPTION:
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 along that
row to find the column heading that matches the message character; the
letter at the [key-row, msg-col] is the enciphered letter.
EXAMPLE:
ALGORITHM:
Solution:
#include<stdio.h>
#include<string.h>
int main(){
if(j == keyLen)
j = 0;
newKey[i] = key[j];
newKey[i] = '\0';
//encryption
encryptedMsg[i] = '\0';
//decryption
decryptedMsg[i] = '\0';
Output:
Original Message: THECRAZYPROGRAMMER
Key: HELLO
New Generated Key: HELLOHELLOHELLOHEL
Encrypted Message: ALPNFHDJAFVKCLATIC
Decrypted Message: THECRAZYPROGRAMMER
Experiment No. 5
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 1:
EXAMPLE 2:
ALGORITHM:
STEP-1: Read the Plain text.
STEP-2: Arrange the plain text in row columnar matrix format.
STEP-3: Now read the keyword depending on the number of columns of
the plain text.
STEP-4: Arrange the characters of the keyword in sorted order and the
corresponding
columns of the plain text.
STEP-5: Read the characters row wise or column wise in the former order
to get the
cipher text.
Solution:
#include<stdio.h>
#include<string.h>
char railMatrix[key][msgLen];
railMatrix[i][j] = '\n';
railMatrix[row][col++] = msg[i];
k= k * (-1);
row = row + k;
if(railMatrix[i][j] != '\n')
printf("%c", railMatrix[i][j]);
char railMatrix[key][msgLen];
railMatrix[i][j] = '\n';
railMatrix[row][col++] = '*';
k= k * (-1);
row = row + k;
}
if(railMatrix[i][j] == '*')
railMatrix[i][j] = enMsg[m++];
row = col = 0;
k = -1;
printf("%c", railMatrix[row][col++]);
k= k * (-1);
row = row + k;
int main(){
int key = 3;
printf("Original Message: %s", msg);
encryptMsg(msg, key);
decryptMsg(enMsg, key);
return 0;
Output:
Original Message: Hello World
Encrypted Message: Horel ollWd
Decrypted Message: Hello World
Experiment No. 6
IMPLEMENTATION OF DATA ENCRYPTION STANDARD (DES)
AIM:
DESCRIPTION:
k . Given that "only" 56 bits are actually used for encrypting, there can be
16
2 different keys.
56
EXAMPLE:
ALGORITHM:
Solution:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
int IP[] =
};
int E[] =
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
};
int P[] =
{
16, 7, 20, 21,
2, 8, 24, 14,
32, 27, 3, 9,
22, 11, 4, 25
};
int FP[] =
};
int S1[4][16] =
};
int S2[4][16] =
};
int S3[4][16] =
};
int S4[4][16] =
};
int S5[4][16] =
};
int S6[4][16] =
};
int S7[4][16]=
};
int S8[4][16]=
};
int PC1[] =
};
int PC2[] =
int SHIFTS[] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
FILE* out;
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];
if (E[i] == pos + 1) {
EXPtext[i] = text;
}
int initialPermutation(int pos, int text)
int i;
if (IP[i] == pos + 1) {
break;
IPtext[i] = text;
int F1(int i)
int r, c, b[6];
b[j] = X[i][j];
r = b[0] * 2 + b[5];
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];
return (a ^ b);
int k, j, m;
static int i;
if (i % 32 == 0) {
i = 0;
m = 1 << j;
k = value & m;
if (k == 0) {
else {
i = i + 4;
int k = 0;
X[i][j] = XORtext[k++];
int value;
value = F1(i);
ToBits(value);
int i;
if (P[i] == pos + 1) {
break;
R[i] = text;
if (mode == 0) {
else {
SBox(XORtext);
PBox(i, X2[i]);
int i;
if (FP[i] == pos + 1) {
break;
ENCRYPTED[i] = text;
void convertToBinary(int n)
int k, m;
m = 1 << i;
k = n & m;
if (k == 0) {
fprintf(out, "0");
else {
fprintf(out, "1");
}
int convertCharToBit(long int n)
char ch;
int i = n * 8;
while (i)
ch = fgetc(inp);
if (ch == -1) {
break;
i--;
convertToBinary(ch);
fclose(out);
fclose(inp);
initialPermutation(i, plain[i]);
}
for (int i = 0; i < 32; i++) {
LEFT[0][i] = IPtext[i];
cipher(k, 0);
if (i < 32) {
CIPHER[i] = RIGHT[16][i];
else {
finalPermutation(i, CIPHER[i]);
}
for (int i = 0; i < 64; i++) {
fclose(out);
initialPermutation(i, plain[i]);
LEFT[0][i] = IPtext[i];
cipher(k, 1);
if (i < 32) {
CIPHER[i] = RIGHT[16][i];
} else {
finalPermutation(i, CIPHER[i]);
fclose(out);
int value = 0;
}
fprintf(out, "%c", value);
int bittochar()
convertToBits(&ENCRYPTED[i]);
fclose(out);
int i;
if (PC2[i] == pos + 1) {
break;
key48bit[round][i] = text;
int i;
for (i = 0; i < 56; i++)
if (PC1[i] == pos + 1) {
break;
key56bit[i] = text;
int k, backup[17][2];
int CD[17][56];
key64to56(i, key[i]);
if (i < 28) {
C[0][i] = key56bit[i];
else {
}
}
k = 0;
}
k = 0;
CD[j][i] = C[j][i];
key56to48(j, i, CD[j][i]);
{
FILE* in = fopen("cipher.txt", "rb");
int i = -1;
char ch;
while (!feof(in))
ch = getc(in);
plain[++i] = ch – 48;
Decryption(plain + i * 64);
bittochar();
fclose(in);
int i = -1;
char ch;
while (!feof(in))
ch = getc(in);
plain[++i] = ch – 48;
Encryption(plain + 64 * i);
fclose(in);
void create16Keys()
int i = 0, ch;
while (!feof(pt))
ch = getc(pt);
key[i++] = ch – 48;
key64to48(key);
fclose(pt);
perror("fseek() failed");
else {
size = ftell(inp);
fclose(inp);
return size;
int main()
fclose(out);
fclose(out);
create16Keys();
convertCharToBit(n);
encrypt(n);
decrypt(n);
return 0;
Output:
result.txt – IT WILL CONTAIN OUR DECRYPTED TEXT.
TEMP FILES: