Doc1
Doc1
#include <stdio.h>
#include <ctype.h>
void encrypt(char text[], int key) { char ch;
}
// Encrypt uppercase letters else if
(isupper(ch)) { ch = (ch - 'A' + key) % 26 + 'A';
}
// Encrypt digits else if (isdigit(ch))
{ ch = (ch - '0' + key) % 10 + '0';
}
} else {
printf("Invalid message\n"); return;
}
text[i] = ch; // Store encrypted character back in the string
}
printf("Encrypted message: %s\n", text);
}
void decrypt(char text[], int key) { char ch;
}
// Decrypt digits else if
(isdigit(ch)) {
}
text[i] = ch; // Store decrypted character back in the string
}
printf("Decrypted message: %s\n", text);
}
int main() { char text[500]; int
key;
return 0;
}
Lab 2: Rail Fence Cipher
#include <stdio.h> #include <string.h>
} printf("\n");
}
int main() {
encryptRailFence(text, key);
}
Lab 3: Write a program to implement Playfair Cipher.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define SIZE 5
char keyMatrix[SIZE][SIZE];
for(i=0;i<len;i++){
if(key[i]=='j') key[i]='i';
if(!map[key[i] - 'a']){
newKey[k++] = key[i];
map[key[i] - 'a'] = 1;
}
}
for(i=0; i<26;i++){
if(i+'a'!='j' && !map[i]){
newKey[k++] = i + 'a';
}
}
k=0;
for(i=0;i<SIZE;i++){
for(j=0;j<SIZE;j++){
keyMatrix[i][j]=newKey[k++];
}
}
}
if(row1==row2){
encryptedText[i]=keyMatrix[row1][(col1+1)%SIZE];
encryptedText[i+1]=keyMatrix[row2][(col2+1)%SIZE];
} else if(col1==col2){
encryptedText[i]=keyMatrix[(row1+1)%SIZE][col1];
encryptedText[i+1]=keyMatrix[(row2+1)%SIZE][col2];
} else{
encryptedText[i]=keyMatrix[row1][col2];
encryptedText[i+1]=keyMatrix[row2][col1];
}
}
encryptedText[i]='\0';
}
if(row1==row2){
decryptedText[i] = keyMatrix[row1][(col1-1+SIZE)%SIZE];
decryptedText[i+1] = keyMatrix[row2][(col2-1+SIZE)%SIZE];
} else if(col1==col2){
decryptedText[i]=keyMatrix[(row1-1+SIZE)%SIZE][col1];
decryptedText[i+1]=keyMatrix[(row2-1+SIZE)%SIZE][col2];
} else{
decryptedText[i]=keyMatrix[row1][col2];
decryptedText[i+1]=keyMatrix[row2][col1];
}
}
decryptedText[i]='\0';
}
int main() {
char key[26], text[100], processedText[100], encryptedText[100], decryptedText[100];
prepareKeyMatrix(key);
prepareText(text, processedText);
encryptedPlayfair(processedText, encryptedText);
printf("Encrypted Text: %s\n", encryptedText);
decryptPlayfair(encryptedText, decryptedText);
printf("Decrypted Text: %s\n", decryptedText);
return 0;
}
Lab 4: Write a program to implement Vigenere Cipher.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
if (isalpha(text[i])) {
j++;
} else {
newKey[i] = text[i];
newKey[textLen] = '\0';
if (isalpha(text[i])) {
} else {
cipherText[i] = text[i];
}
cipherText[textLen] = '\0';
if (isalpha(cipherText[i])) {
} else {
plainText[i] = cipherText[i];
plainText[textLen] = '\0';
int main() {
int choice;
scanf("%s", key);
printf("Choose operation:\n1. Encrypt\n2. Decrypt\nEnter choice (1/2): ");
scanf("%d", &choice);
getchar();
if (choice == 1) {
} else if (choice == 2) {
} else {
printf("Invalid choice!\n");
return 0;
}
Lab 5: WAP that computes additive inverse in given modulo n.
#include <stdio.h>
int main(){
int a,n;
printf("Enter a number: ");
scanf("%d",&a);
printf("Enter modulo n: ");
scanf("%d",&n);
if(n<=0){
printf("Modulo n must be greater than zero.\n");
return 1;
}
return 0;
}
Lab6: WAP which takes two numbers and display whether they are relatively prime
or not.
#include<stdio.h>
#include<stdlib.h>
if(are_relatively_prime(num1,num2)){
printf("%d and %d are relatively prime.\n",num1,num2);
} else{
printf("%d and %d are not relatively prime.\n",num1,num2);
}
return 0;
}
Lab 7: Calculation Of GCD:
if (b == 0)
return a;
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
return a;
int main() {
return 0;
}
Lab 8: Write a program to implement Extended Euclidean Algorithm. (Display the results of iterations in
tabular format)
#include <stdio.h>
int q, r;
printf("%-10s %-10s %-10s %-10s %-10s%-10s\n", "q", "r", "x", "y", "gcd");
while (b != 0) {
q = a / b;
r = a % b;
x1 = old_x - q * current_x;
y1 = old_y - q * current_y;
old_x = current_x;
old_y = current_y;
current_x = x1;
current_y = y1;
a = b;
b = r;
*x = old_x;
*y = old_y;
return a;
}
int main() {
int a, b, x, y;
return 0;
}
Lab9: WAP to compute multiplicative inverse in given modulo n using Extended Euclidean Algorithm.
#include <stdio.h>
int q, r;
while (b != 0) {
q = a / b;
r = a % b;
x1 = old_x - q * current_x;
y1 = old_y - q * current_y;
old_x = current_x;
old_y = current_y;
current_x = x1;
current_y = y1;
a = b;
b = r;
*x = old_x;
*y = old_y;
return a;
}
void mod_inverse(int a, int n) {
int x, y;
if (gcd != 1) {
} else {
int inverse = (x % n + n) % n;
int main() {
mod_inverse(num1, num2);
return 0;
}
Lab 10: Write a program to implement Hill Cipher (Key matrix of size 2*2/ Encryption/ Decryption).
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MOD 26
if (detInv == -1) {
printf("Inverse does not exist (Matrix is not invertible modulo 26).\n");
return 0;
}
int main() {
char plaintext[100], ciphertext[100], decrypted[100];
int key[2][2], inverseKey[2][2];
#include<stdio.h>
int s1[4][16]={
};
*col = (input>>1)&0xF;
getRowAndColumn(input,&row,&col);
return s1[row][col];
int main(){
int input,output;
scanf("%d",&input);
return -1;
output=SBoxOutput(input);
printf("The S-Box(s1)output for intput %d is:%d\n",input,output);
return 0;
}
Lab12: Write a program to implement Robin Miller algorithm for primality test.
#include<stdio.h>
int main(){
int n;
printf("Please a postive Integer:\n");
scanf("%d",&n);
return 0;
}
Lab 13: WAP that takes any positive number and display the result after computing Totient value.
#include <stdio.h>
int euler_totient(int n) {
int result = n;
if (n % i == 0) {
while (n % i == 0) {
n /= i;
result -= result / i;
if (n > 1) {
result -= result / n;
return result;
int main() {
int n;
scanf("%d", &n);
if (n <= 0) {
printf("Please enter a positive integer.\n");
} else {
return 0;
}
Lab14: Write a program to compute primitive roots of given number.
#include <stdio.h>
#include <math.h>
int computeTotient(int n) {
int result = n;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
while (n % i == 0) {
n /= i;
}
result -= result / i;
}
}
if (n > 1) {
result -= result / n;
}
return result;
}
void findPrimitiveRoots(int n) {
int phi = computeTotient(n);
int factors[100], size;
findFactors(phi, factors, &size);
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num <= 0) {
printf("Please enter a positive integer.\n");
return 1;
}
findPrimitiveRoots(num);
printf("Sachhyam Sthapit, 20790521");
return 0;
}
Lab 15: WAP to implement Diffie-Hellman Key Exchange Algorithm.
#include <stdio.h>
#include <math.h>
long long power(long long base, long long exp, long long mod) {
long long result = 1;
while (exp > 0) {
if (exp % 2 == 1)
result = (result * base) % mod;
base = (base * base) % mod;
exp /= 2;
}
return result;
}
int main() {
long long p, g, a, b, A, B, key1, key2;
return 0;
}
Lab16: WAP to implement RSA Algorithm (Encryption/Decryption).
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
long long modExp(long long base, long long exp, long long mod) {
long long result = 1;
base = base % mod;
while (exp > 0) {
if (exp % 2 == 1)
result = (result * base) % mod;
exp = exp >> 1;
base = (base * base) % mod;
}
return result;
}
int main() {
int p, q, n, phi, e, d;
long long message, encrypted, decrypted;
n = p * q;
phi = (p - 1) * (q - 1);
printf("Enter public key exponent (e), must be co-prime with %d: ", phi);
scanf("%d", &e);
if (gcd(e, phi) != 1) {
printf("Invalid 'e'. It must be co-prime with %d.\n", phi);
return 1;
}
d = modInverse(e, phi);
if (d == -1) {
printf("No modular inverse found for e. Choose another e.\n");
return 1;
}
if (message >= n) {
printf("Message must be smaller than n (%d).\n", n);
return 1;
}
return 0;
}
Lab 17: WAP to compute discrete logarithm of given number.
#include <stdio.h>
#include <math.h>
int main() {
int base, result, mod, log_value;
if (log_value != -1) {
printf("The discrete logarithm log_%d(%d) mod %d is: %d\n", base, result, mod, log_value);
} else {
printf("No solution found.\n");
}
printf("Sachhyam Sthapit, 20790521");
return 0;
}
Index
S.No Topics Date Signature
1 WAP to implement Shift Cipher. 15 May 2024
2 WAP to implement Rail Fence Cipher. 2 June 2024
3 WAP to implement Playfair Cipher 4 June 2024
4 WAP to implement Vigenere Cipher. 6 June 2024
5 WAP that computes additive inverse in given 10 June 2024
modulo n.