Enrollment No.
:230843116012
PRACTICAL – 3
AIM:-Implement Ceasar and Hill cipher. Both are substitution cipher.
Analyze the strength of the cipher in terms of brute force attack and
cryptanalysis attack. Suggest one way to improve and strengthen the
cipher and analyze with respect to cryptanalysis attack
Ceasar cipher –
You are given plaintext Hello, Welcome. The key used is 3. How
Ceaser cipher will work?
Test case :
ABC
DEF
Hill Cipher -
Key K =[2 2 19, 21 18 21, 17 17 5]
Plaintext = pay
Ciphertext = RRL
CODE:-
Caesar Cipher
#include<stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_INPUT 100
void caesarEncrypt(char *plaintext, int key, char *ciphertext) {
int i;
for (i = 0; plaintext[i] != '\0'; i++) {
if (isalpha(plaintext[i])) {
char base = isupper(plaintext[i]) ? 'A' : 'a';
ciphertext[i] = ((plaintext[i] - base + key) % 26) + base;
} else {
ciphertext[i] = plaintext[i];
}
}
ciphertext[i] = '\0';
}
void caesarDecrypt(char *ciphertext, int key, char *plaintext) {
caesarEncrypt(ciphertext, 26 - key, plaintext);
9
Enrollment No.:230843116012
void clearInputBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
int main() {
char plaintext[MAX_INPUT];
char ciphertext[MAX_INPUT];
char decrypted[MAX_INPUT];
int key;
printf("Caesar Cipher Implementation\n");
printf("===========================\n\n");
printf("Enter plaintext: ");
fgets(plaintext, MAX_INPUT, stdin);
plaintext[strcspn(plaintext, "\n")] = 0;
printf("Enter key (0-25): ");
scanf("%d", &key);
clearInputBuffer();
key = key % 26;
if (key < 0) key += 26;
caesarEncrypt(plaintext, key, ciphertext);
printf("\nEncryption:\n");
printf("Plaintext: %s\n", plaintext);
printf("Key: %d\n", key);
printf("Ciphertext: %s\n", ciphertext);
caesarDecrypt(ciphertext, key, decrypted);
printf("\nDecryption:\n");
printf("Ciphertext: %s\n", ciphertext);
printf("Decrypted text: %s\n", decrypted);
return 0;
}
10
Enrollment No.:230843116012
OUTPUT
CODE:-
Hill Cipher
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_INPUT 100
#define MATRIX_SIZE 3
void matrixMultiply(int key[MATRIX_SIZE][MATRIX_SIZE], int *vector, int
*result) {
for (int i = 0; i < MATRIX_SIZE; i++) {
result[i] = 0;
for (int j = 0; j < MATRIX_SIZE; j++) {
result[i] += key[i][j] * vector[j];
}
result[i] = result[i] % 26;
}
}
void hillEncrypt(char *plaintext, int key[MATRIX_SIZE][MATRIX_SIZE], char
*ciphertext) {
int len = strlen(plaintext);
int vector[MATRIX_SIZE];
11
Enrollment No.:230843116012
int result[MATRIX_SIZE];
for (int i = 0; i < len; i += MATRIX_SIZE) {
for (int j = 0; j < MATRIX_SIZE; j++) {
if (i + j < len) {
vector[j] = tolower(plaintext[i + j]) - 'a';
} else {
vector[j] = 0;
}
}
matrixMultiply(key, vector, result);
for (int j = 0; j < MATRIX_SIZE; j++) {
ciphertext[i + j] = result[j] + 'A';
}
}
ciphertext[len] = '\0';
}
int determinant(int matrix[MATRIX_SIZE][MATRIX_SIZE]) {
return matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1])
- matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0])
+ matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]);
}
int modInverse(int a, int m) {
a = a % m;
for (int x = 1; x < m; x++) {
if (((a * x) % m) == 1) {
return x;
}
}
return 1;
}
void adjugate(int matrix[MATRIX_SIZE][MATRIX_SIZE], int adj[MATRIX_SIZE]
[MATRIX_SIZE]) {
adj[0][0] = (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) % 26;
adj[0][1] = (-(matrix[0][1] * matrix[2][2] - matrix[0][2] * matrix[2][1])) % 26;
adj[0][2] = (matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1]) % 26;
adj[1][0] = (-(matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0])) % 26;
adj[1][1] = (matrix[0][0] * matrix[2][2] - matrix[0][2] * matrix[2][0]) % 26;
adj[1][2] = (-(matrix[0][0] * matrix[1][2] - matrix[0][2] * matrix[1][0])) % 26;
adj[2][0] = (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]) % 26;
12
Enrollment No.:230843116012
adj[2][1] = (-(matrix[0][0] * matrix[2][1] - matrix[0][1] * matrix[2][0])) % 26;
adj[2][2] = (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]) % 26;
}
void printMatrix(int matrix[MATRIX_SIZE][MATRIX_SIZE]) {
for (int i = 0; i < MATRIX_SIZE; i++) {
for (int j = 0; j < MATRIX_SIZE; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
void clearInputBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
int main() {
char plaintext[MAX_INPUT];
char ciphertext[MAX_INPUT];
char decrypted[MAX_INPUT];
int key[MATRIX_SIZE][MATRIX_SIZE];
printf("Hill Cipher Implementation\n");
printf("=========================\n\n");
printf("Enter the 3x3 key matrix (9 numbers, one per line):\n");
for (int i = 0; i < MATRIX_SIZE; i++) {
for (int j = 0; j < MATRIX_SIZE; j++) {
scanf("%d", &key[i][j]);
key[i][j] = key[i][j] % 26;
if (key[i][j] < 0) key[i][j] += 26;
}
}
clearInputBuffer();
int det = determinant(key);
if (det == 0 || det % 2 == 0 || det % 13 == 0) {
printf("Error: The key matrix is not invertible. Please choose a different key.\n");
return 1;
}
printf("Enter plaintext (3 letters): ");
fgets(plaintext, MAX_INPUT, stdin);
plaintext[strcspn(plaintext, "\n")] = 0;
13
Enrollment No.:230843116012
printf("\nKey Matrix:\n");
printMatrix(key);
for (int i = 0; i < strlen(plaintext); i++) {
plaintext[i] = tolower(plaintext[i]);
}
printf("\nManual calculation for verification:\n");
int p[3] = {plaintext[0] - 'a', plaintext[1] - 'a', plaintext[2] - 'a'};
printf("Plaintext numbers: %d %d %d\n", p[0], p[1], p[2]);
int c[3];
matrixMultiply(key, p, c);
printf("Ciphertext numbers: %d %d %d\n", c[0], c[1], c[2]);
hillEncrypt(plaintext, key, ciphertext);
printf("\nEncryption:\n");
printf("Plaintext: %s\n", plaintext);
printf("Ciphertext: %s\n", ciphertext);
return 0;
}
OUTPUT
14