IS pr-1 To 8
IS pr-1 To 8
IS pr-1 To 8
#include <stdio.h>
int main() {
char text[100];
int shift;
caesarCipher(text, shift);
printf("Encrypted text: %s\n", text);
return 0;
}
Output:
Enter the text: testing project
Enter the shift: 4
Encrypted text: xiwxmrk tvsnigx
Practical-2
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char text[] = "hello world";
char key[] = "bcdefghijklmnopqrstuvwxyz";
monoalphabetic_cipher(text, key);
printf("Encrypted text: %s\n", text);
Output:
Encrypted text: ifmmp xpsme
Decrypted text: hello world
Practical-3
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char text[] = "hello world";
char key[] = "key";
vigenere_cipher(text, key);
printf("Encrypted text: %s\n", text);
// Decryption is the same as encryption with the same key
vigenere_cipher(text, key);
printf("Decrypted text: %s\n", text);
return 0;
}
Output:
Encrypted text: lpkmc qvzy
Decrypted text: hello world
Practical-4
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MATRIX_SIZE 5
if (row_a == row_b) {
// Same row
encrypted_pair[0] = matrix[row_a][(col_a + 1) % MATRIX_SIZE];
encrypted_pair[1] = matrix[row_b][(col_b + 1) % MATRIX_SIZE];
} else if (col_a == col_b) {
// Same column
encrypted_pair[0] = matrix[(row_a + 1) % MATRIX_SIZE][col_a];
encrypted_pair[1] = matrix[(row_b + 1) % MATRIX_SIZE][col_b];
} else {
// Rectangle rule
encrypted_pair[0] = matrix[row_a][col_b];
encrypted_pair[1] = matrix[row_b][col_a];
}
}
int main() {
char key[100], plaintext[100], ciphertext[100];
char matrix[MATRIX_SIZE][MATRIX_SIZE];
prepare_key_matrix(key, matrix);
encrypt_text(plaintext, ciphertext, matrix);
printf("Encrypted text: %s\n", ciphertext);
return 0;
}
Output:
Enter the key: monarchy
Enter the plaintext: hidegold
Encrypted text: BFCKFNTC
Practical-5
#include <stdio.h>
int main() {
// 2x2 key matrix
int keyMatrix[2][2] = {{3, 3},
{2, 5}};
return 0;
}
Output:
Encrypted Text: TC
Practical-6
#include <stdio.h>
#include <string.h>
// Create a grid
char grid[rows][cols];
int main() {
char plaintext[] = "meet me at the park at midnight";
char keyword[] = "secret";
char ciphertext[100];
return 0;
}
Output:
Ciphertext: ea an e e d a g ttpti mehkitmtrmh
Practical-7
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
*n = p * q; // n = p * q
int phi = (p - 1) * (q - 1); // Euler's totient
// Choose e
*e = 3; // Commonly used value for e
while (gcd(*e, phi) != 1) {
(*e) += 2; // Increment to find a suitable e
}
// Compute d
*d = mod_inverse(*e, phi);
}
// Main function
int main() {
int e, d, n;
generate_keypair(&e, &d, &n);
return 0;
}
Output:
Public Key: (e: 7, n: 3233)
Private Key: (d: 1783, n: 3233)
Original Message: 42
Encrypted Message: -2194
Decrypted Message: -2194
Practical-8
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Display keys
printf("Alice's Private Key: %d\n", alice_private);
printf("Bob's Private Key: %d\n", bob_private);
printf("Alice's Public Key: %d\n", alice_public);
printf("Bob's Public Key: %d\n", bob_public);
printf("Shared Secret (Alice): %d\n", alice_shared_secret);
printf("Shared Secret (Bob): %d\n", bob_shared_secret);
return 0;
}
Output:
Alice's Private Key: 4
Bob's Private Key: 8
Alice's Public Key: 4
Bob's Public Key: 16
Shared Secret (Alice): 9
Shared Secret (Bob): 9