Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

IS pr-1 To 8

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Practical-1

#include <stdio.h>

void caesarCipher(char *text, int shift) {


int i;

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


if (text[i] >= 'A' && text[i] <= 'Z') {
text[i] = (text[i] - 'A' + shift) % 26 + 'A';
} else if (text[i] >= 'a' && text[i] <= 'z') {
text[i] = (text[i] - 'a' + shift) % 26 + 'a';
}
}
}

int main() {
char text[100];
int shift;

printf("Enter the text: ");


fgets(text, 100, stdin);

printf("Enter the shift: ");


scanf("%d", &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>

void monoalphabetic_cipher(char *text, char *key) {


int i, j;
char sub_table[26];

// Create the substitution table


for (i = 0; i < 26; i++) {
sub_table[i] = key[i];
}

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


if (isalpha(text[i])) {
if (isupper(text[i])) {
text[i] = sub_table[text[i] - 'A'];
} else {
text[i] = sub_table[text[i] - 'a'] + 'a';
}
}
}
}

int main() {
char text[] = "hello world";
char key[] = "bcdefghijklmnopqrstuvwxyz";

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

// Decryption is the same as encryption with the same key


monoalphabetic_cipher(text, key);
printf("Decrypted text: %s\n", text);
return 0;
}

Output:
Encrypted text: ifmmp xpsme
Decrypted text: hello world

Practical-3

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void vigenere_cipher(char *text, char *keyword) {


int i, j;
int keyword_len = strlen(keyword);

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


if (isalpha(text[i])) {
int shift = tolower(keyword[i % keyword_len]) - 'a';
if (isupper(text[i])) {
text[i] = (text[i] - 'A' + shift) % 26 + 'A';
} else {
text[i] = (text[i] - 'a' + shift) % 26 + 'a';
}
}
}
}

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

// Function to remove duplicates and prepare the Playfair key matrix


void prepare_key_matrix(const char *key, char
matrix[MATRIX_SIZE][MATRIX_SIZE]) {
int seen[26] = {0};
int i, j = 0, k = 0;
char c;

// Fill the matrix with key and remaining letters


for (i = 0; key[i] != '\0'; i++) {
c = toupper(key[i]);
if (c == 'J') c = 'I';
if (isalpha(c) && !seen[c - 'A']) {
matrix[j / MATRIX_SIZE][j % MATRIX_SIZE] = c;
seen[c - 'A'] = 1;
j++;
}
}
for (i = 0; i < 26; i++) {
c = 'A' + i;
if (c == 'J') continue; // Skip 'J'
if (!seen[i]) {
matrix[j / MATRIX_SIZE][j % MATRIX_SIZE] = c;
j++;
}
}
}

// Function to find the position of a letter in the matrix


void find_position(char matrix[MATRIX_SIZE][MATRIX_SIZE], char c, int *row,
int *col) {
if (c == 'J') c = 'I'; // Treat 'J' as 'I'
for (int i = 0; i < MATRIX_SIZE; i++)
for (int j = 0; j < MATRIX_SIZE; j++)
if (matrix[i][j] == c) {
*row = i;
*col = j;
return;
}
}

// Encrypt a digraph (pair of letters)


void encrypt_pair(char matrix[MATRIX_SIZE][MATRIX_SIZE], char a, char b,
char *encrypted_pair) {
int row_a, col_a, row_b, col_b;
find_position(matrix, a, &row_a, &col_a);
find_position(matrix, b, &row_b, &col_b);

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

// Function to encrypt the entire text


void encrypt_text(const char *plaintext, char *ciphertext, char
matrix[MATRIX_SIZE][MATRIX_SIZE]) {
char digraph[3] = {0};
int len = strlen(plaintext), i, j = 0;

for (i = 0; i < len; i += 2) {


char a = toupper(plaintext[i]);
char b = (i + 1 < len) ? toupper(plaintext[i + 1]) : 'X';
if (a == b) b = 'X'; // Handle repeated letters
encrypt_pair(matrix, a, b, digraph);
ciphertext[j++] = digraph[0];
ciphertext[j++] = digraph[1];
}
ciphertext[j] = '\0';
}

int main() {
char key[100], plaintext[100], ciphertext[100];
char matrix[MATRIX_SIZE][MATRIX_SIZE];

printf("Enter the key: ");


scanf("%s", key);

printf("Enter the plaintext: ");


scanf("%s", plaintext);

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>

// Function to encrypt the message using the Hill Cipher


void encrypt(int keyMatrix[2][2], char message[3], char cipherText[3]) {
int messageVector[2];
int cipherMatrix[2];

// Convert characters to numbers (A=0, B=1, ..., Z=25)


for (int i = 0; i < 2; i++) {
messageVector[i] = message[i] - 'A';
}

// Encrypt using matrix multiplication


for (int i = 0; i < 2; i++) {
cipherMatrix[i] = 0;
for (int j = 0; j < 2; j++) {
cipherMatrix[i] += keyMatrix[i][j] * messageVector[j];
}
cipherMatrix[i] = cipherMatrix[i] % 26; // Mod 26 for letters
}

// Convert back to characters


for (int i = 0; i < 2; i++) {
cipherText[i] = cipherMatrix[i] + 'A';
}
cipherText[2] = '\0'; // Null-terminate the string
}

int main() {
// 2x2 key matrix
int keyMatrix[2][2] = {{3, 3},
{2, 5}};

// Message to be encrypted (2 characters)


char message[3] = "HI"; // Example message
char cipherText[3]; // To store the encrypted message

// Encrypt the message


encrypt(keyMatrix, message, cipherText);

// Display the encrypted message


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

return 0;
}

Output:
Encrypted Text: TC

Practical-6

#include <stdio.h>
#include <string.h>

void columnar_transposition(char *plaintext, char *keyword, char *ciphertext)


{
int rows = strlen(keyword);
int cols = strlen(plaintext) / rows + (strlen(plaintext) % rows > 0 ? 1 : 0);

// Create a grid
char grid[rows][cols];

// Fill the grid with plaintext


int index = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (index < strlen(plaintext)) {
grid[i][j] = plaintext[index++];
} else {
grid[i][j] = ' '; // Pad with spaces if necessary
}
}
}

// Determine column order based on keyword


int order[rows];
for (int i = 0; i < rows; i++) {
order[i] = i;
}
for (int i = 0; i < rows - 1; i++) {
for (int j = i + 1; j < rows; j++) {
if (keyword[order[i]] > keyword[order[j]]) {
int temp = order[i];
order[i] = order[j];
order[j] = temp;
}
}
}

// Read the grid column-wise in the specified order


index = 0;
for (int j = 0; j < cols; j++) {
for (int i = 0; i < rows; i++) {
ciphertext[index++] = grid[i][order[j]];
}
}
ciphertext[index] = '\0';
}

int main() {
char plaintext[] = "meet me at the park at midnight";
char keyword[] = "secret";
char ciphertext[100];

columnar_transposition(plaintext, keyword, ciphertext);

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

return 0;
}

Output:
Ciphertext: ea an e e d a g ttpti mehkitmtrmh

Practical-7

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// Function to compute gcd


int gcd(int a, int b) {
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
return a;
}

// Function to find modular inverse


int mod_inverse(int a, int m) {
for (int x = 1; x < m; x++) {
if ((a * x) % m == 1) {
return x;
}
}
return -1; // Modular inverse doesn't exist
}

// Function to generate keys


void generate_keypair(int *e, int *d, int *n) {
int p = 61; // First prime number
int q = 53; // Second prime number

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

// Function to encrypt plaintext


int encrypt(int plaintext, int e, int n) {
return (int)pow(plaintext, e) % n;
}

// Function to decrypt ciphertext


int decrypt(int ciphertext, int d, int n) {
return (int)pow(ciphertext, d) % n;
}

// Main function
int main() {
int e, d, n;
generate_keypair(&e, &d, &n);

printf("Public Key: (e: %d, n: %d)\n", e, n);


printf("Private Key: (d: %d, n: %d)\n", d, n);

int message = 42; // Example message (should be less than n)


printf("Original Message: %d\n", message);

// Encrypt the message


int encrypted_msg = encrypt(message, e, n);
printf("Encrypted Message: %d\n", encrypted_msg);

// Decrypt the message


int decrypted_msg = decrypt(encrypted_msg, d, n);
printf("Decrypted Message: %d\n", decrypted_msg);

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>

// Function to perform modular exponentiation


int power_mod(int base, int exponent, int modulus) {
int result = 1;
base = base % modulus;
while (exponent > 0) {
if (exponent % 2 == 1) {
result = (result * base) % modulus;
}
exponent = exponent >> 1; // Divide exponent by 2
base = (base * base) % modulus; // Square the base
}
return result;
}

// Function to generate a random private key


int generate_private_key(int prime) {
return rand() % (prime - 2) + 1; // Random number between 1 and prime-2
}

// Main function for Diffie-Hellman Key Exchange


int main() {
srand(time(NULL)); // Seed random number generator

// Example prime number and base


int prime = 23; // A prime number
int base = 5; // A primitive root modulo prime

// Generate private keys


int alice_private = generate_private_key(prime);
int bob_private = generate_private_key(prime);
// Calculate public keys
int alice_public = power_mod(base, alice_private, prime);
int bob_public = power_mod(base, bob_private, prime);

// Calculate shared secrets


int alice_shared_secret = power_mod(bob_public, alice_private, prime);
int bob_shared_secret = power_mod(alice_public, bob_private, prime);

// 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

You might also like