Cryptography
Cryptography
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;
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;
}
void decrypt() {
char text[MAX_SIZE], ch;
int key, i;
printf("Enter a message to decrypt: ");
scanf("%s", text);
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.
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:
Compiler: DEV-C++
Language: C
Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 30
dicty['j' - 97] = 1;
i = 0;
j = 0;
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() {
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]];
}
}
}
ps = strlen(str);
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
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>
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";
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>
if (isalpha(currentChar)) {
int shift = toupper(keyChar) - 'A';
ciphertext[i] = ((toupper(currentChar) - 'A' + shift) % 26) + 'A';
} else {
ciphertext[i] = currentChar;
}
i++;
}
ciphertext[i] = '\0';
}
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];
return 0;
}
Output: