0% found this document useful (0 votes)
22 views

Cryptography

Uploaded by

Subraz Thapa
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)
22 views

Cryptography

Uploaded by

Subraz Thapa
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/ 21

Tribhuvan University

Institute of Science and Technology


Madan Bhandari Memorial College

Subject: Submitted by:


Cryptography
Name: ……………………………
Roll No: ……...
Faculty: Bsc.CSIT (5th semester)
Practical Index

S.N Title Signature

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.
Lab no: 1
Title: Write a program for the implementation of Ceaser Cipher

The Caesar cipher is one of the simplest and most widely known encryption techniques in
cryptography. It involves shifting each letter in the plaintext by a fixed number of positions down
or up the alphabet. For example, with a shift of 3, 'A' would be replaced by 'D', 'B' would become
'E', and so on. The Caesar cipher is named after Julius Caesar, who is reputed to have used it to
communicate with his generals. Despite its simplicity, it can be effective for encrypting messages,
especially when used with larger keys or combined with other techniques. However, it is
vulnerable to brute-force attacks due to its limited number of possible keys.

Compiler: DEV-C++
Language: C

Source Code:
#include <stdio.h>
#include <ctype.h>
#define MAX_SIZE 500
void encrypt() {
char text[MAX_SIZE], ch;
int key, i;

printf("Enter a message to encrypt: ");


scanf("%s", text);

printf("Enter the key: ");


scanf("%d", &key);

for (i = 0; text[i] != '\0'; ++i) {


ch = text[i];

if (isalnum(ch)) {
if (islower(ch)) {
ch = (ch - 'a' + key) % 26 + 'a';
} else if (isupper(ch)) {
ch = (ch - 'A' + key) % 26 + 'A';
} else if (isdigit(ch)) {
ch = (ch - '0' + key) % 10 + '0';
}
} else {
printf("Invalid Message");
return;
}
text[i] = ch;
}

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


}

void decrypt() {
char text[MAX_SIZE], ch;
int key, i;
printf("Enter a message to decrypt: ");
scanf("%s", text);

printf("Enter the key: ");


scanf("%d", &key);
for (i = 0; text[i] != '\0'; ++i) {
ch = text[i];

if (isalnum(ch)) {
if (islower(ch)) {
ch = (ch - 'a' - key + 26) % 26 + 'a';
} else if (isupper(ch)) {
ch = (ch - 'A' - key + 26) % 26 + 'A';
} else if (isdigit(ch)) {
ch = (ch - '0' - key + 10) % 10 + '0';
}
} else {
printf("Invalid Message");
return;
}
text[i] = ch;
}
printf("Decrypted message: %s\n", text);

}
int main() {
encrypt();
decrypt();

return 0;
}
Output:
Lab no: 2
Title: Write a program to implement Hill cipher.

Hill Cipher:
The Hill cipher is a polygraphic substitution cipher based on linear algebra. It operates on blocks
of text, typically pairs or triples of letters, rather than individual letters. Invented by Lester S. Hill
in 1929, it's one of the earliest known practical implementations of a block cipher.

Here's how it works:


Key Matrix: The encryption key is represented by a square matrix (often 2x2 or 3x3), which must
be invertible.
Encryption: To encrypt, the plaintext is split into blocks of letters corresponding to the size of the
key matrix. Each block is then converted into a numerical vector (e.g., A=0, B=1, ..., Z=25), and
multiplied by the key matrix modulo the alphabet size. The resulting vector is converted back to
letters to form the ciphertext.
Decryption: Decryption involves multiplying the ciphertext vector by the inverse of the key
matrix modulo the alphabet size. This operation retrieves the original plaintext vector, which is
then converted back to letters.

Compiler: DEV C++


Language: C
Source Code:
#include<stdio.h>
#include<math.h>
#include<ctype.h>

float encrypt[3][1] = {0}, decrypt[3][1] = {0}, a[3][3], b[3][3], mes[3][1], c[3][3];

void encryption();
void decryption();
void getKeyMessage();
void inverse();

int main() {
getKeyMessage();
encryption();
decryption();
return 0;
}

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

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[4];
printf("Enter 3x3 matrix for key (It should be invertible):\n");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
scanf("%f", &a[i][j]);
c[i][j] = a[i][j];
}
printf("\nEnter a 3 letter string: ");
scanf("%s", msg);
for(i = 0; i < 3; i++)
mes[i][0] = tolower(msg[i]) - 'a';
}

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];
}
}
}
}
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("%f ", b[i][j]);
printf("\n");
}
}
Output:
Lab no: 3
Title: Write a program to illustrate Playfair cipher.

The Playfair cipher is a classical substitution cipher invented in 1854 by Charles Wheatstone, but
it was popularized by Lyon Playfair, hence the name. It is a digraphic cipher, meaning it operates
on pairs of letters, making it more secure than simple substitution ciphers.

How it Works:
1. Key Table Creation: A key phrase or word is used to create a 5x5 grid (key table), filling
it with unique letters of the alphabet (usually omitting 'J' and combining 'I' and 'J' into one
letter).
2. Encryption:

• The plaintext is divided into pairs of letters.


• For each pair:
• If the letters are in the same row, they are replaced by the letters to their immediate right
(circularly wrapping around if needed).
• If the letters are in the same column, they are replaced by the letters immediately below
(circularly wrapping around if needed).
• If the letters form a rectangle, they are replaced by the letters on the same row but at the
other pair of corners of the rectangle.
• If the letters are the same, an 'X' is inserted between them, and the process is repeated.
3. Decryption:
In decryption, the reverse process is applied: pairs of letters are replaced based on their positions
in the key table

Compiler: DEV-C++
Language: C
Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}
}

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

void generateKeyTable(char key[], int ks, char keyT[5][5]) {

int i, j, k, flag = 0, *dicty;

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;
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;
}
}
}
}
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++) {
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); }
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)];
} 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]];
}
}
}
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);
}
int encryption() {

char str[SIZE], key[SIZE];


strcpy(key, "Arsenal");
printf("Key text: %s\n", key);
strcpy(str, "London");
printf("Plain text: %s\n", str);

encryptByPlayfairCipher(str, key);
printf("Cipher text: %s\n", str);
return 0;
}
void decrypt(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)];
} 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]];
}
}
}

void decryptByPlayfairCipher(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);

generateKeyTable(key, ks, keyT);


decrypt(str, keyT, ps);
}
int decryption() {
char str[SIZE], key[SIZE];
strcpy(key, "Arsenal");
printf("Key text: %s\n", key);

strcpy(str, "London");
printf("Plain text: %s\n", str);

decryptByPlayfairCipher(str, key);
printf("Deciphered text: %s\n", str);

return 0;
}
int main(){
encryption();
decryption();

}
Output:
Lab no: 4
Title: Write a program to illustrate Vignere cipher.

The Vigenère cipher is a method of encrypting alphabetic text that uses a simple form of
polyalphabetic substitution. It was invented by the French diplomat and cryptographer Blaise de
Vigenère in the 16th century. The Vigenère cipher is significantly more secure than the simpler
Caesar cipher.

How it Works:
Key: The Vigenère cipher uses a keyword or phrase as the encryption key. This key is repeated to
match the length of the plaintext message.
Encryption: Each letter in the plaintext is shifted according to the corresponding letter in the key.
For example, if the letter 'A' in the key corresponds to a shift of 1 (Caesar cipher), and the first
letter in the plaintext is 'B', it would be encrypted as 'C'. The next letter in the plaintext would be
shifted by the value of the next letter in the key, and so on.
Decryption: To decrypt the message, the process is reversed. Each letter in the ciphertext is shifted
backward according to the corresponding letter in the key.

Compiler: DEV-C++
Language: C
Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void upper_case(char *src) {


while (*src != '\0') {
if (islower(*src))
*src &= ~0x20;
src++;
}
}
char* encipher(const char *src, char *key, int is_encode) {
int i, klen, slen;
char *dest;
dest = strdup(src);
upper_case(dest);
upper_case(key);

for (i = 0, slen = 0; dest[slen] != '\0'; slen++)


if (isupper(dest[slen]))
dest[i++] = dest[slen];

dest[slen = i] = '\0'; /* null pad it, make it safe to use */

klen = strlen(key);
for (i = 0; i < slen; i++) {
if (!isupper(dest[i]))
continue;
dest[i] = 'A' + (is_encode ? dest[i] - 'A' + key[i % klen] - 'A'
: dest[i] - key[i % klen] + 26) % 26;
}

return dest;
}

int main() {
const char *str = "Weather le challange garyo ta";
const char *cod, *dec;
char key[] = "VIGENERECIPHER";

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


printf("key: %s\n", key);

cod = encipher(str, key, 1);


printf("Cipher Text: %s\n", cod);
dec = encipher(cod, key, 0);
printf("Plain Text: %s\n", dec);

return 0;
}
Output:
Lab no: 5
Title: Write a program to illustrate Autokey cipher.

The autokey cipher is a cryptographic algorithm that extends the Vigenère cipher by allowing the
key to be as long as the plaintext message. It was designed to address the weakness of the repeating
key in the original Vigenère cipher. Instead of repeating the key, the autokey cipher appends the
plaintext message itself to the key to create a longer key sequence.

How it Works:
Key Creation: Like the Vigenère cipher, the autokey cipher uses a keyword or phrase as the initial
part of the key.
Key Extension: The key is then extended by appending the plaintext message to it.
Encryption: Each letter in the plaintext is shifted according to the corresponding letter in the
extended key.
Decryption: To decrypt the message, the recipient uses the same extended key. The ciphertext is
decrypted using the extended key in reverse.
Compiler: DEV-C++
Language: C

Source Code:
#include <stdio.h>
#include <ctype.h>

#define MAX_SIZE 100

void encryptAutokey(char *plaintext, char *key, char *ciphertext) {


int i = 0;
while (plaintext[i] != '\0') {
char currentChar = plaintext[i];
char keyChar = key[i % strlen(key)];

if (isalpha(currentChar)) {
int shift = toupper(keyChar) - 'A';
ciphertext[i] = ((toupper(currentChar) - 'A' + shift) % 26) + 'A';
} else {
ciphertext[i] = currentChar;
}
i++;
}
ciphertext[i] = '\0';
}

void decryptAutokey(char *ciphertext, char *key, char *decryptedtext) {


int i = 0;
while (ciphertext[i] != '\0') {
char currentChar = ciphertext[i];
char keyChar = key[i % strlen(key)];

if (isalpha(currentChar)) {
int shift = toupper(keyChar) - 'A';
decryptedtext[i] = ((toupper(currentChar) - 'A' - shift + 26) % 26) + 'A';
} else {
decryptedtext[i] = currentChar;
}
i++;
}
decryptedtext[i] = '\0';
}

int main() {
char plaintext[MAX_SIZE];
char key[MAX_SIZE];
char ciphertext[MAX_SIZE];
char decryptedtext[MAX_SIZE];

printf("Enter the plaintext: ");


fgets(plaintext, MAX_SIZE, stdin);
printf("Enter the key: ");
fgets(key, MAX_SIZE, stdin);

encryptAutokey(plaintext, key, ciphertext);


decryptAutokey(ciphertext, key, decryptedtext);

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


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

return 0;
}
Output:

You might also like