Is Contenthalf
Is Contenthalf
PRACTICAL-1
{
ch = ch - 'Z' + 'A' - 1;
}
str_message[j] = ch;
}
}
printf(" Encrypted message: %s", str_message);
for (j = 0; str_message[j] != '\0'; ++j)
{
ch = str_message[j];
if (ch >= 'a' && ch <= 'z')
{
ch = ch - key;
if (ch<'a')
{
ch = ch + 'z' - 'a' + 1;
}
str_message[j] = ch;
}
else if (ch >= 'A' && ch <= 'Z')
{
ch = ch - key;
if (ch<'A')
{
ch = ch + 'Z' - 'A' + 1;
}
str_message[j] = ch;
}
}
OUTPUT:
PRACTICAL-2
OUTPUT:
PRACTICAL-3
{
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;
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
{ 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]];
}
}
}
// 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);
OUTPUT:
PRACTICAL-4
plen = strlen(p);
klen = strlen(k);
m = 0;
for (i = 0; i < plen; i++)
{
if (p[i] != 32)
{
p1[m] = p[i];
m++;
}
}
plen = strlen(p1);
m = 0;
for (i = klen; i < plen; i++)
{
if (m == klen) m = 0;
k[i] = k[m];
m++;
}
k[i] = '\0';
printf("%s", k);
printf(" %d\n", plen);
printf("Encrypted Text is: ");
for (i = 0; i < plen; i++)
{
e[i] = a[k[i] - 97][p1[i] - 97];
printf("%c", e[i]);
}
printf("\nDecrypted Text is: ");
OUTPUT:
PRACTICAL-5
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 3 // Size of the matrix (3x3 for simplicity)
// Function to find the modular inverse of a number (used in decryption)
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;
}
// Function to calculate the determinant of a 3x3 matrix
int determinant(int matrix[SIZE][SIZE]) {
int det = 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]);
return det;
}
// Function to calculate the adjoint of a 3x3 matrix (used for decryption)
void adjoint(int matrix[SIZE][SIZE], int adj[SIZE][SIZE]) {
adj[0][0] = (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]);
adj[0][1] = -(matrix[0][1] * matrix[2][2] - matrix[0][2] * matrix[2][1]);
if (inverseKey[i][j] < 0)
inverseKey[i][j] += 26;
}
}
// Convert cipher characters to numbers (A=0, B=1, ..., Z=25)
for (int i = 0; i < SIZE; i++) {
cipherVector[i] = cipher[i] - 'A';
}
// Multiply inverse key matrix with cipher vector
multiplyMatrices(inverseKey, cipherVector, messageVector, 26);
// Convert back to characters
printf("Decrypted message: ");
for (int i = 0; i < SIZE; i++) {
printf("%c", messageVector[i] + 'A');
}
printf("\n");
}
int main() {
char message[SIZE + 1], cipher[SIZE + 1];
int keyMatrix[SIZE][SIZE] = {
{6, 24, 1},
{13, 16, 10},
{20, 17, 15}
};
printf("Enter a 3-letter message to encrypt (uppercase only): ");
scanf("%s", message);
// Encrypt the message
encrypt(message, keyMatrix);
printf("Enter a 3-letter cipher to decrypt (uppercase only): ");
scanf("%s", cipher);
// Decrypt the message
decrypt(cipher, keyMatrix);
return 0;
}
OUTPUT: