0% found this document useful (0 votes)
10 views24 pages

ISExp 2

The document outlines various encryption techniques including substitution and transposition methods, detailing algorithms such as the Caesar cipher, Playfair cipher, and Vigenère cipher. It also includes programming examples for implementing these ciphers in C, demonstrating both encryption and decryption processes. Additionally, it discusses the challenges associated with the one-time pad method and provides insights into transposition techniques like the rail fence and columnar transposition ciphers.

Uploaded by

akaria6996
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views24 pages

ISExp 2

The document outlines various encryption techniques including substitution and transposition methods, detailing algorithms such as the Caesar cipher, Playfair cipher, and Vigenère cipher. It also includes programming examples for implementing these ciphers in C, demonstrating both encryption and decryption processes. Additionally, it discusses the challenges associated with the one-time pad method and provides insights into transposition techniques like the rail fence and columnar transposition ciphers.

Uploaded by

akaria6996
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

EXPERIMENT NO.

02

DATE OF PERFORMANCE: GRADE:

SIGNATURE OF LECTURER/
DATE OF ASSESSMENT:
TTA:

AIM: To study and implement substitution and transposition techniques.

Theory:

A substitution technique is one in which the letters of plaintext are replaced by


other letters or by numbers or symbols. If the plaintext is viewed as a sequence
of bits, then substitution involves replacing plaintext bit patterns with cipher text
bit patterns.

1) Ceaser cipher: The earliest known, and the simplest, use of a substitution
cipher was by Julius Caesar. The Caesar cipher involves replacing each
letter of the alphabet with the letter standing three places further down the
alphabet.
The general Caesar algorithm is:

Decryption:

2) Monoalphabetic Ciphers: With only 25 possible keys, the Caesar cipher


is far from secure. A dramatic increase in the key space can be achieved by
allowing an arbitrary substitution. Before proceeding, we define the term
permutation. A permutation of a finite set of elements S is an ordered
sequence of all the elements of S, with each element appearing exactly
once.
3) Playfair ciphers: The best-known multiple-letter encryption cipher is the
Playfair, which treats diagrams in the plaintext as single units and translates
these units into cipher text diagrams. The Playfair algorithm is based on
the use of a 5 * 5 matrix of letters constructed using a keyword.

1
4) Hill cipher: Another interesting multilateral cipher is the Hill cipher,
developed by the mathematician Lester Hill in 1929. We define the inverse
M-1 of a square matrix M by the equation M (M-1 ) = M-1 M = I, where I
is the identity matrix. I is a square matrix that is all zeros except for ones
along the main diagonal from upper left to lower right. The inverse of a
matrix does not always exist, but when it does, it satisfies the preceding
equation.

5) Polyalphabetic ciphers: Another way to improve on the simple


monoalphabetic technique is to use different monoalphabetic substitutions
as one proceeds through the plaintext message. The general name for this
approach is polyalphabetic substitution cipher. All these techniques have
the following features in common:
1. A set of related monoalphabetic substitution rules is used.
2. A key determines which particular rule is chosen for a given
transformation.
Vigenère Cipher: The best known, and one of the simplest,
polyalphabetic ciphers is the Vigenère cipher. In this scheme, the set of
related monoalphabetic substitution rules consists of the 26 Caesar ciphers
with shifts of 0 through 25. Each cipher is denoted by a key letter, which
is the cipher text letter that substitutes for the plaintext letter a. Thus, a
Caesar cipher with a shift of 3 is denoted by the key value 3.
A general equation of the encryption process is

Decryption:
2
6) One-Time Pad: An Army Signal Corp officer, Joseph Mauborgne,
proposed an improvement to the Vernam cipher that yields the ultimate in
security. Mauborgne suggested using a random key that is as long as the
message, so that the key need not be repeated. In addition, the key is to be
used to encrypt and decrypt a single message, and then is discarded. Each
new message requires a new key of the same length as the new message.
Such a scheme, known as a one-time pad, is unbreakable. It produces
random output that bears no statistical relationship to the plaintext.
Because the cipher text contains no information whatsoever about the
plaintext, there is simply no way to break the code.
The one-time pad offers complete security but, in practice, has two
fundamental difficulties:
1. There is the practical problem of making large quantities of random
keys. Any heavily used system might require millions of random characters
on a regular basis. Supplying truly random characters in this volume is a
significant task.
2. Even more daunting is the problem of key distribution and protection.
For every message to be sent, a key of equal length is needed by both
sender and receiver. Thus, a mammoth key distribution problem exists.

Transposition techniques:

All the techniques examined so far involve the substitution of a ciphertext


symbol for a plaintext symbol. A very different kind of mapping is achieved by
performing some sort of permutation on the plaintext letters. This technique is
referred to as a transposition cipher. The simplest such cipher is the rail fence
technique, in which the plaintext is written down as a sequence of diagonals and
then read off as a sequence of rows.

1) Rail fence: The rail fence cipher (also called a zigzag cipher) is
a classical type of transposition cipher. It derives its name from the manner
in which encryption is performed, in analogy to a fence built with
horizontal rails.

3
2) Row Column technique: In cryptography, transposition ciphers are a type
of encryption method that involves rearranging the order of characters in
the plaintext to produce the cipher text. Columnar transposition cipher is
one such method that involves transposing the plaintext by columns before
encrypting it.

Program 1) write a program for encryption using ceaser cipher.


#include <stdio.h>
#include <string.h>

int main() {
// Declaring variables
int i, key;
char s[1000], c;

// Taking Input
printf("Enter a plaintext to encrypt:\n");
fgets(s, sizeof(s), stdin);
printf("Enter key:\n");
scanf("%d", &key);

int n = strlen(s);

// Encrypting each character according to the given key


for (i = 0; i < n; i++) {
c = s[i];
if (c >= 'a' && c <= 'z') {
c = c + key;
if (c > 'z') {
c = c - 'z' + 'a' - 1;
}
s[i] = c;

4
} else if (c >= 'A' && c <= 'Z') {
c = c + key;
if (c > 'Z') {
c = c - 'Z' + 'A' - 1;
}
s[i] = c;
}
}

// Output the cipher


printf("Encrypted message: %s\n", s);

return 0;
}

Output:

Program 2) write a program for decryption using ceaser cipher.


#include <stdio.h>
#include <string.h>

int main() {
// Declaring variables
int i, key;
char s[1000], c;

// Taking ciphertext and key input


printf("Enter encrypted text:\n");
fgets(s, sizeof(s), stdin);
printf("Enter key:\n");
scanf("%d", &key);

int n = strlen(s);
5
// Decrypting each character according to the given key
for (i = 0; i < n; i++) {
c = s[i];
if (c >= 'a' && c <= 'z') {
c = c - key;
if (c < 'a') {
c = c + 'z' - 'a' + 1;
}
s[i] = c;
} else if (c >= 'A' && c <= 'Z') {
c = c - key;
if (c < 'A') {
c = c + 'Z' - 'A' + 1;
}
s[i] = c;
}
}

// Output the original message


printf("Decrypted message: %s\n", s);

return 0;
}

Output:

Program 3) write a program for encryption using monoalphabetic cipher.


#include<stdio.h>
char monocipher_encr(char);
char alpha[27][3] = { { 'a', 'f' }, { 'b', 'a' }, { 'c', 'g' }, { 'd', 'u' }, {
'e', 'n' }, { 'f', 'i' }, { 'g', 'j' }, { 'h', 'k' }, { 'i', 'l' }, {

6
'j', 'm' }, { 'k', 'o' }, { 'l', 'p' }, { 'm', 'q' }, { 'n', 'r' }, {
'o', 's' }, { 'p', 't' }, { 'q', 'v' }, { 'r', 'w' }, { 's', 'x' }, {
't', 'y' }, { 'u', 'z' }, { 'v', 'b' }, { 'w', 'c' }, { 'x', 'd' }, {
'y', 'e' }, { 'z', 'h' } };
char str[20];
int main() {
char str[20], str2[20];
int i;
printf("\n Enter String..");
gets(str);
for (i = 0; str[i]; i++) {
str2[i] = monocipher_encr(str[i]);
}
str2[i] = '\0';
printf("\n Before Decryption..%s", str);
printf("\n After Decryption..%s\n", str2);
}
char monocipher_encr(char a) {
int i;
for (i = 0; i < 27; i++) {
if (a == alpha[i][0])
break;
}
return alpha[i][1];
}

Output:

Program 4) write a program for encryption using playfair cipher.

#include <stdio.h> //header file


#include <stdlib.h> //header file

7
#include <string.h> //header file

#define SIZE 30
void toLowerCase(char plain[], int ps)
{
int i;
for (i = 0; i < ps; i++) {
if (plain[i] > 64 && plain[i] < 91)
plain[i] += 32;
}
}

// this function will remove all the spaces


int removeSpaces(char* plain, int ps)
{
int i, count = 0;
for (i = 0; i < ps; i++)
if (plain[i] != ' ')
plain[count++] = plain[i];
plain[count] = '\0';
return count;
}
// this function will generate the 5x5 grid square
void generateKeyTable(char key[], int ks, char keyT[5][5])
{
int i, j, k, flag = 0, *dicty;

// character hashmap of 26 character that will


// store count of the alphabet.
dicty = (int*)calloc(26, sizeof(int));
for (i = 0; i < ks; i++) {
if (key[i] != 'j')
dicty[key[i] - 97] = 2;
}

dicty['j' - 97] = 1;
i = 0;

8
j = 0;

for (k = 0; k < ks; k++) {


if (dicty[key[k] - 97] == 2) {
dicty[key[k] - 97] -= 1;
keyT[i][j] = key[k];
j++;
if (j == 5) {
i++;
j = 0;
}
}
}
for (k = 0; k < 26; k++) {
if (dicty[k] == 0) {
keyT[i][j] = (char)(k + 97);
j++;
if (j == 5) {
i++;
j = 0;
}
}
}
}

// this function will search for the characters of a digraph


// in the key and return position of key
void search(char keyT[5][5], char a, char b, int arr[])
{
int i, j;

if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
for (i = 0; i < 5; i++) {

9
for (j = 0; j < 5; j++) {

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); }

// this function will make the plain text length even


int prepare(char str[], int ptrs)
{
if (ptrs % 2 != 0) {
str[ptrs++] = 'z';
str[ptrs] = '\0';
}
return ptrs;
}
void encrypt(char str[], char keyT[5][5], int ps)
{
int i, a[4];

for (i = 0; i < ps; i += 2) {

search(keyT, str[i], str[i + 1], a);

if (a[0] == a[2]) {
str[i] = keyT[a[0]][mod5(a[1] + 1)];
str[i + 1] = keyT[a[0]][mod5(a[3] + 1)];
}

10
else if (a[1] == a[3]) {
str[i] = keyT[mod5(a[0] + 1)][a[1]];
str[i + 1] = keyT[mod5(a[2] + 1)][a[1]];
}
else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
}

// this function will encrypt cipher text using Playfair Cipher algorithm
void encryptByPlayfairCipher(char str[], char key[])
{
char ps, ks, keyT[5][5];

ks = strlen(key);
ks = removeSpaces(key, ks);
toLowerCase(key, ks);

ps = strlen(str);
toLowerCase(str, ps);
ps = removeSpaces(str, ps);

ps = prepare(str, ps);

generateKeyTable(key, ks, keyT);

encrypt(str, keyT, ps);


}

// main code
int main()
{
char str[SIZE], key[SIZE];

11
// key text
strcpy(key, "Algorithm");
printf("Key text: %s\n", key);

// Plaintext
strcpy(str, "Programming");
printf("Plain text: %s\n", str);

// encryption using the "Playfair Cipher" algorithmn


encryptByPlayfairCipher(str, key);

printf("Cipher text: %s\n", str);

return 0;
}

Output:

Program 5) write a program for encryption using polyalphabetic cipher.


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char p[20],c[26],key[20],d[26];
int i=0,j=0,l1=0,l2=0;
clrscr();
printf("\n Enter the plain text: ");
gets(p);
printf("\n Enter the key:------- ");
gets(key);
l1=strlen(p);
l2=strlen(key);

12
for(i=0;i<=l1-1;i++)
{
if(p[i]>=97&&p[i]<=122)
{
c[i]=(((p[i]-97)+(key[j]-97))%26)+97;
c[i+1]='\0';
}
else if(p[i]>=65&&p[i]<=90)
{
c[i]=(((p[i]-65)+(key[j]-65))%26)+65;
c[i+1]='\0';
}
else
{
c[i]=p[i];
c[i+1]='\0';
}
if(j<l2-1)
{
j++;
}
else
{
j=0;
}
}
j=0;
for(i=0;i<=l1-1;i++)
{
if(p[i]>=97&&p[i]<=122)
{
d[i]=((((c[i]-97)-(key[j]-97))+26)%26)+97;
d[i+1]='\0';
}
else if(p[i]>=65&&p[i]<=90)
{
d[i]=((((c[i]-65)+(key[j]-65))+26)%26)+65;

13
d[i+1]='\0';
}
else
{
d[i]=c[i];
d[i+1]='\0';
}
if(j<l2-1)
{
j++;
}
else
{
j=0;
}
}
printf("The encrypted word is :- ");
puts(c);
printf("\n The Decrypted word is :- ");
puts(d);
getch();
}

Output:

Program 6) write a program for encryption using hill cipher.


#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];

14
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 :");
for (i = 0; i < 3; i++)
printf(" %c", d[i] + 65);
for (i = 0; i < 3; i++) {
t = 0;
for (j = 0; j < 3; j++) {
t = t + (b[i][j] * d[j]);
}
c[i] = t % 26;
}
printf("\nDecrypted Cipher Text :");
for (i = 0; i < 3; i++)
printf(" %c", c[i] + 65);
return 0;
}

Output:

15
Program 7) write a program for encryption using one time pad cipher.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
main()
{
//All the text which ever entered is converted to upper and without spaces
int i,j,len1,len2,numstr[100],numkey[100],numcipher[100];
char str[100],key[100],cipher[100];
printf("Enter a string text to encrypt\n");
gets(str);
for(i=0,j=0;i<strlen(str);i++)
{
if(str[i]!=' ')
{
str[j]=toupper(str[i]);
j++;
}
}
str[j]='\0';
//obtaining numerical plain text ex A-0,B-1,C-2
for(i=0;i<strlen(str);i++)
{
numstr[i]=str[i]-'A';
}
printf("Enter key string of random text\n");
gets(key);
for(i=0,j=0;i<strlen(key);i++)
{
if(key[i]!=' ')
{
key[j]=toupper(key[i]);
j++;
}
}
key[j]='\0';
//obtaining numerical one time pad(OTP) or key

16
for(i=0;i<strlen(key);i++)
{
numkey[i]=key[i]-'A';
}

for(i=0;i<strlen(str);i++)
{
numcipher[i]=numstr[i]+numkey[i];
}
for(i=0;i<strlen(str);i++)
{
if(numcipher[i]>25)
{
numcipher[i]=numcipher[i]-26;
}
}
printf("One Time Pad Cipher text is\n");
for(i=0;i<strlen(str);i++)
{
printf("%c",(numcipher[i]+'A'));
}
printf("\n");

Output:

Program 8) write a program for encryption using rail fence cipher.


#include<stdio.h>
#include<string.h>
void main()
{
int i,j,k,l;
char a[20],c[20],d[20];
17
printf("\n\t\t RAIL FENCE TECHNIQUE");
printf("\n\nEnter the input string : ");
gets(a);
l=strlen(a);
/*Ciphering*/
for(i=0,j=0;i<l;i++)
{
if(i%2==0)
c[j++]=a[i];
}
for(i=0;i<l;i++)
{
if(i%2==1)
c[j++]=a[i];
}
c[j]='\0';
printf("\nCipher text after applying rail fence :");
printf("\n%s",c);
/*Deciphering*/
if(l%2==0)
k=l/2;
else
k=(l/2)+1;
for(i=0,j=0;i<k;i++)
{
d[j]=c[i];
j=j+2;
}
for(i=k,j=1;i<l;i++)
{
d[j]=c[i];
j=j+2;
}
d[l]='\0';
printf("\nText after decryption : ");
printf("%s",d);
}

18
Output:

Program 9) write a program for encryption using row column technique.


#include<stdio.h>
#include<string.h>

void cipher(int i, int c);


int findMin();
void makeArray(int, int);

char arr[22][22], darr[22][22], emessage[111], retmessage[111], key[55];


char temp[55], temp2[55];
int k = 0;

int main() {
char *message, *dmessage;

int i, j, klen, emlen, flag = 0;


int r, c, index, min, rows;

printf("Enetr the key\n");


fflush(stdin);
gets(key);

printf("\nEnter message to be ciphered\n");


fflush(stdin);
gets(message);

strcpy(temp, key);
klen = strlen(key);

k = 0;

19
for (i = 0;; i++) {
if (flag == 1)
break;

for (j = 0; key[j] != NULL; j++) {


if (message[k] == NULL) {
flag = 1;
arr[i][j] = '-';
} else {
arr[i][j] = message[k++];
}
}
}
r = i;
c = j;

for (i = 0; i < r; i++) {


for (j = 0; j < c; j++) {
printf("%c ", arr[i][j]);
}
printf("\n");
}

k = 0;

for (i = 0; i < klen; i++) {


index = findMin();
cipher(index, r);
}

emessage[k] = '\0';
printf("\nEncrypted message is\n");
for (i = 0; emessage[i] != NULL; i++)
printf("%c", emessage[i]);

printf("\n\n");
//deciphering

20
emlen = strlen(emessage);
//emlen is length of encrypted message

strcpy(temp, key);

rows = emlen / klen;


//rows is no of row of the array to made from ciphered message
rows;
j = 0;

for (i = 0, k = 1; emessage[i] != NULL; i++, k++) {


//printf("\nEmlen=%d",emlen);
temp2[j++] = emessage[i];
if ((k % rows) == 0) {
temp2[j] = '\0';
index = findMin();
makeArray(index, rows);
j = 0;
}
}

printf("\nArray Retrieved is\n");

k = 0;
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
printf("%c ", darr[i][j]);
//retrieving message
retmessage[k++] = darr[i][j];

}
printf("\n");
}
retmessage[k] = '\0';

printf("\nMessage retrieved is\n");

21
for (i = 0; retmessage[i] != NULL; i++)
printf("%c", retmessage[i]);

getch();
return (0);
}

void cipher(int i, int r) {


int j;
for (j = 0; j < r; j++) {
{
emessage[k++] = arr[j][i];
}
}
// emessage[k]='\0';
}

void makeArray(int col, int row) {


int i, j;

for (i = 0; i < row; i++) {


darr[i][col] = temp2[i];
}
}

int findMin() {
int i, j, min, index;

min = temp[0];
index = 0;
for (j = 0; temp[j] != NULL; j++) {
if (temp[j] < min) {
min = temp[j];
index = j;
}
}

22
temp[index] = 123;
return (index);
}

Output:

23
24

You might also like