0% found this document useful (0 votes)
1 views32 pages

Doc1

The document contains multiple C programming labs that implement various cryptographic algorithms including Caesar Cipher, Rail Fence Cipher, Playfair Cipher, Vigenere Cipher, and mathematical concepts like GCD, additive inverse, and multiplicative inverse using the Extended Euclidean Algorithm. Each lab provides code for encryption and decryption methods, as well as functions to compute GCD and check for relatively prime numbers. The labs are designed for educational purposes and include user interaction for input and output.

Uploaded by

Sachhyam Sthapit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views32 pages

Doc1

The document contains multiple C programming labs that implement various cryptographic algorithms including Caesar Cipher, Rail Fence Cipher, Playfair Cipher, Vigenere Cipher, and mathematical concepts like GCD, additive inverse, and multiplicative inverse using the Extended Euclidean Algorithm. Each lab provides code for encryption and decryption methods, as well as functions to compute GCD and check for relatively prime numbers. The labs are designed for educational purposes and include user interaction for input and output.

Uploaded by

Sachhyam Sthapit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Lab 1: Caesar Cipher

#include <stdio.h>
#include <ctype.h>
void encrypt(char text[], int key) { char ch;

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

if (isalnum(ch)) { // Encrypt lowercase letters


if (islower(ch)) { ch = (ch - 'a' + key) % 26 + 'a';

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

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

if (isalnum(ch)) { // Decrypt lowercase


letters if (islower(ch)) {

ch = (ch - 'a' - key + 26) % 26 + 'a';


}
// Decrypt uppercase letters else if (isupper(ch)) {
ch = (ch - 'A' - key + 26) % 26 + 'A';

}
// Decrypt digits else if
(isdigit(ch)) {

ch = (ch - '0' - key + 10) % 10 + '0';


} } else {

printf("Invalid message\n"); return;

}
text[i] = ch; // Store decrypted character back in the string
}
printf("Decrypted message: %s\n", text);
}
int main() { char text[500]; int
key;

printf("Enter a message to encrypt: "); scanf("%s",


text); printf("Enter the key: "); scanf("%d", &key);

// Encrypt the message encrypt(text, key);

printf("Enter a message to decrypt: "); scanf("%s",


text); printf("Enter the key: "); scanf("%d", &key);

// Decrypt the message decrypt(text, key);

return 0;
}
Lab 2: Rail Fence Cipher
#include <stdio.h> #include <string.h>

void encryptRailFence(char *text, int key) { int len =


strlen(text); char rail[key][len];

int i, j, dir_down = 0, row = 0, col = 0;

for (i = 0; i < key; i++) for (j = 0; j < len; j++)


rail[i][j] = '\n';

for (i = 0; i < len; i++) { if (row == 0 || row == key -


1) dir_down = !dir_down;

rail[row][col++] = text[i]; row += dir_down ? 1 : 1;

printf("Encrypted text: "); for (i = 0; i < key; i++)


for (j = 0; j < len; j++) if (rail[i][j] != '\n')
printf("%c", rail[i][j]); printf("\n"); }

void decryptRailFence(char *cipher, int key) { int len =


strlen(cipher); char rail[key][len];

int i, j, dir_down = 0, row = 0, col = 0; for (i = 0; i < key; i+


+) for (j = 0; j < len; j++) rail[i][j] = '\n';

for (i = 0; i < len; i++) { if (row == 0 || row ==


key1) dir_down = !dir_down;

rail[row][col++] = '*'; row += dir_down ? 1 :


-1;

} int index = 0; for (i = 0; i < key; i++) for (j = 0; j <


len; j++) if (rail[i][j] == '*' && index < len) rail[i]
[j] = cipher[index++];

row = 0, col = 0; dir_down = 0; printf("Decrypted


Text: "); for (i = 0; i < len; i++) { if (row == 0 || row
== key - 1) dir_down = !dir_down;

printf("%c", rail[row][col++]); row += dir_down ?


1 : -1;

} printf("\n");
}

int main() {

char text[100], cipher[100]; int key;

printf("Enter text to encrypt: "); scanf("%s", text);


printf("Enter a key: "); scanf("%d", &key);

encryptRailFence(text, key);

printf("Enter text to decrypt: "); scanf("%s", cipher);


printf("Enter key: "); scanf("%d", &key);
decryptRailFence(cipher, key); return 0;

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

void prepareKeyMatrix(char key[]){


int map[26] = {0};
int i,j,k=0,len=strlen(key);
char newKey[26] = {0};

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

void findPosition(char ch, int *row, int *col){


int i,j;
if(ch =='j') ch='i';
for(i=0;i<SIZE;i++){
for(j=0;j<SIZE;j++){
if(keyMatrix[i][j] == ch){
*row=i;
*col=j;
return;
}
}
}
}

void prepareText(char text[], char processedText[]){


int len = strlen(text), i, j=0;
for(i=0;i<len;i++){
if(text[i]=='j') text[i]='i';
if(isalpha(text[j])){
processedText[j++] = tolower(text[i]);
if(j>1 && processedText[j-1] == processedText[j-2]){
processedText[j-1] = 'x';
processedText[j++] = tolower(text[j]);
}
}
}
if(j%2!=0) processedText[j++]='x';
processedText[j]='\0';
}

void encryptedPlayfair(char text[], char encryptedText[]){


int i, row1, col1, row2, col2;
for(i=0;text[i]!='\0';i+=2){
findPosition(text[i],&row1, &col1);
findPosition(text[i+1],&row2, &col2);

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

void decryptPlayfair(char text[], char decryptedText[]){


int i, row1, col1, row2, col2;
for(i=0;text[i]!='\0';i+=2){
findPosition(text[i],&row1, &col1);
findPosition(text[i+1],&row2, &col2);

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

printf("Enter the key: ");


scanf("%s", key);

prepareKeyMatrix(key);

printf("Enter the text to encrypt: ");


scanf("%s", text);

prepareText(text, processedText);

encryptedPlayfair(processedText, encryptedText);
printf("Encrypted Text: %s\n", encryptedText);

decryptPlayfair(encryptedText, decryptedText);
printf("Decrypted Text: %s\n", decryptedText);

printf("Sachhyam Sthapit, 20790521");

return 0;
}
Lab 4: Write a program to implement Vigenere Cipher.

#include <stdio.h>

#include <string.h>

#include <ctype.h>

void generateKey(char *text, char *key, char *newKey) {

int textLen = strlen(text);

int keyLen = strlen(key);

for (int i = 0, j = 0; i < textLen; i++) {

if (isalpha(text[i])) {

newKey[i] = key[j % keyLen];

j++;

} else {

newKey[i] = text[i];

newKey[textLen] = '\0';

void encrypt(char *text, char *key, char *cipherText) {

int textLen = strlen(text);

char newKey[textLen + 1];

generateKey(text, key, newKey);

for (int i = 0; i < textLen; i++) {

if (isalpha(text[i])) {

char base = isupper(text[i]) ? 'A' : 'a';

cipherText[i] = ((text[i] - base + (toupper(newKey[i]) - 'A')) % 26) + base;

} else {

cipherText[i] = text[i];
}

cipherText[textLen] = '\0';

void decrypt(char *cipherText, char *key, char *plainText) {

int textLen = strlen(cipherText);

char newKey[textLen + 1];

generateKey(cipherText, key, newKey);

for (int i = 0; i < textLen; i++) {

if (isalpha(cipherText[i])) {

char base = isupper(cipherText[i]) ? 'A' : 'a';

plainText[i] = ((cipherText[i] - base - (toupper(newKey[i]) - 'A') + 26) % 26) + base;

} else {

plainText[i] = cipherText[i];

plainText[textLen] = '\0';

int main() {

char text[100], key[100], result[100];

int choice;

printf("Enter the text: ");

fgets(text, sizeof(text), stdin);

text[strcspn(text, "\n")] = '\0';

printf("Enter the keyword: ");

scanf("%s", key);
printf("Choose operation:\n1. Encrypt\n2. Decrypt\nEnter choice (1/2): ");

scanf("%d", &choice);

getchar();

if (choice == 1) {

encrypt(text, key, result);

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

} else if (choice == 2) {

decrypt(text, key, result);

printf("Decrypted Text: %s\n", result);

} else {

printf("Invalid choice!\n");

printf("Sachhyam Sthapit, 20790521\n");

return 0;

}
Lab 5: WAP that computes additive inverse in given modulo n.

#include <stdio.h>

int additiveInverse(int a, int n){


int inverse=(n-(a%n))%n;
return inverse;

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

int result = additiveInverse(a,n);


printf("The additive inverse of %d modulo %d is: %d.\n",a,n,result);

return 0;
}
Lab6: WAP which takes two numbers and display whether they are relatively prime
or not.

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

int gcd(int a, int b){


while(b!=0){
int temp=b;
b=a%b;
a=temp;
}
return a;
}

int are_relatively_prime(int a, int b){


return gcd(a,b)==1;
}
int main(){
int num1, num2;

printf("\n\nEnter the first number:");


scanf("%d",&num1);
printf("Enter the second number:");
scanf("%d",&num2);

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:

int gcd_recursive(int a, int b) {

if (b == 0)
return a;

return gcd_recursive(b, a % b);

int gcd_iterative(int a, int b) {

while (b != 0) {

int temp = b;

b = a % b;

a = temp;

return a;

int main() {

int num1, num2;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

printf("GCD (Recursive): %d\n", gcd_recursive(num1, num2));

printf("GCD (Iterative): %d\n", gcd_iterative(num1, num2));

printf("Sachhyam Sthapit, 20790521\n");

return 0;

}
Lab 8: Write a program to implement Extended Euclidean Algorithm. (Display the results of iterations in
tabular format)

#include <stdio.h>

int extended_gcd(int a, int b, int *x, int *y) {

int x1, y1;

int q, r;

int old_x = 1, old_y = 0;

int current_x = 0, current_y = 1;

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;

printf("%-10d %-10d %-10d %-10d %-10d\n", q, r, x1, y1, b);

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;

printf("Enter two numbers: ");

scanf("%d %d", &a, &b);

int gcd = extended_gcd(a, b, &x, &y);

printf("GCD: %d\n", gcd);

printf("Coefficients x: %d, y: %d\n", x, y);

printf("Sachhyam Sthapit, 20790521\n");

return 0;

}
Lab9: WAP to compute multiplicative inverse in given modulo n using Extended Euclidean Algorithm.

#include <stdio.h>

int extended_gcd(int a, int b, int *x, int *y) {

int x1, y1;

int q, r;

int old_x = 1, old_y = 0;

int current_x = 0, current_y = 1;

printf("%-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;

printf("%-10d %-10d %-10d %-10d %-10d\n", q, r, current_x, current_y, b);

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;

int gcd = extended_gcd(a, n, &x, &y);

if (gcd != 1) {

printf("\nMultiplicative inverse does not exist (GCD is not 1)\n");

} else {

int inverse = (x % n + n) % n;

printf("\nMultiplicative inverse of %d modulo %d is: %d\n", a, n, inverse);

int main() {

int num1, num2, x, y;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

mod_inverse(num1, num2);

int gcd = extended_gcd(num1, num2, &x, &y);

printf("\nGCD of the given numbers is: %d\n", gcd);

printf("Coefficient: x = %d, y = %d\n", x, y);

printf("Sachhyam Sthapit, 20790521\n");

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

int determinant(int key[2][2]) {


return ((key[0][0] * key[1][1]) - (key[0][1] * key[1][0])) % MOD;
}

int modInverse(int num) {


num = (num % MOD + MOD) % MOD;
for (int i = 1; i < MOD; i++) {
if ((num * i) % MOD == 1)
return i;
}
return -1;
}

int inverseKeyMatrix(int key[2][2], int inverse[2][2]) {


int det = determinant(key);
int detInv = modInverse(det);

if (detInv == -1) {
printf("Inverse does not exist (Matrix is not invertible modulo 26).\n");
return 0;
}

inverse[0][0] = ( key[1][1] * detInv) % MOD;


inverse[0][1] = (-key[0][1] * detInv) % MOD;
inverse[1][0] = (-key[1][0] * detInv) % MOD;
inverse[1][1] = ( key[0][0] * detInv) % MOD;

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


for (int j = 0; j < 2; j++) {
inverse[i][j] = (inverse[i][j] + MOD) % MOD;
}
}
return 1;
}

void encryptHill(char *plaintext, int key[2][2], char *ciphertext) {


int len = strlen(plaintext);
if (len % 2 != 0) {
plaintext[len] = 'X';
plaintext[len + 1] = '\0';
len++;
}

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


int p1 = plaintext[i] - 'A';
int p2 = plaintext[i + 1] - 'A';
int c1 = (key[0][0] * p1 + key[0][1] * p2) % MOD;
int c2 = (key[1][0] * p1 + key[1][1] * p2) % MOD;
ciphertext[i] = c1 + 'A';
ciphertext[i + 1] = c2 + 'A';
}
ciphertext[len] = '\0';
}

void decryptHill(char *ciphertext, int inverseKey[2][2], char *decrypted) {


int len = strlen(ciphertext);
for (int i = 0; i < len; i += 2) {
int c1 = ciphertext[i] - 'A';
int c2 = ciphertext[i + 1] - 'A';
int p1 = (inverseKey[0][0] * c1 + inverseKey[0][1] * c2) % MOD;
int p2 = (inverseKey[1][0] * c1 + inverseKey[1][1] * c2) % MOD;
decrypted[i] = p1 + 'A';
decrypted[i + 1] = p2 + 'A';
}
decrypted[len] = '\0';
}

int main() {
char plaintext[100], ciphertext[100], decrypted[100];
int key[2][2], inverseKey[2][2];

printf("Enter plaintext (Uppercase letters only): ");


scanf("%s", plaintext);

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


if (!isalpha(plaintext[i])) {
printf("Invalid input! Use only alphabetic characters.\n");
return 1;
}
plaintext[i] = toupper(plaintext[i]);
}

printf("Enter 2x2 key matrix (space-separated integers mod 26):\n");


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
scanf("%d", &key[i][j]);
}
}
if (!inverseKeyMatrix(key, inverseKey)) {
return 1;
}

encryptHill(plaintext, key, ciphertext);


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

decryptHill(ciphertext, inverseKey, decrypted);


printf("Decrypted text: %s\n", decrypted);

printf("Sachhyam Sthapit, 20790521");


return 0;
}
Lab11: WAP to demonstrate how output of S-Box (S1) is generated in DES.

#include<stdio.h>

int s1[4][16]={

{14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},

{ 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8},

{ 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0},

{15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}

};

void getRowAndColumn(int input,int*row,int*col){

*row = ((input >> 5)& 0x1)*2 +((input>>0)&0x1);

*col = (input>>1)&0xF;

int SBoxOutput(int input){

int row, col;

getRowAndColumn(input,&row,&col);

return s1[row][col];

int main(){

int input,output;

printf("Sachhyam Sthapit, 20790521");

printf("Enter a 6 bit number(int decimal):");

scanf("%d",&input);

if(input <0 || input>63){

printf("Invalid input: Enter a number between 0 and 63(Inclusive).\n");

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 eulerTotient(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;
}

int main(){

int n;
printf("Please a postive Integer:\n");
scanf("%d",&n);

if(n <= 0){


printf("Invalid input. Please enter a positive integer.\n");
return 1;
}

int result = eulerTotient(n);


printf("Euler's Totient function value for %d is: %d\n", n, result);
printf("Sachhyam Sthapit, 20790521");

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;

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;

int main() {

int n;

printf("Enter a positive integer: ");

scanf("%d", &n);

if (n <= 0) {
printf("Please enter a positive integer.\n");

} else {

int totient = euler_totient(n);

printf("Euler's Totient value of %d is: %d\n", n, totient);

printf("Sachhyam Sthapit, 20790521");

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

int powerMod(int base, int exp, int mod) {


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

void findFactors(int n, int factors[], int *size) {


*size = 0;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
factors[(*size)++] = i;
if (i != n / i) {
factors[(*size)++] = n / i;
}
}
}
factors[(*size)++] = n;
}
int isPrimitiveRoot(int g, int n, int phi, int factors[], int size) {
for (int i = 0; i < size; i++) {
if (powerMod(g, phi / factors[i], n) == 1) {
return 0;
}
}
return 1;
}

void findPrimitiveRoots(int n) {
int phi = computeTotient(n);
int factors[100], size;
findFactors(phi, factors, &size);

printf("Primitive roots of %d: ", n);


int found = 0;
for (int g = 2; g < n; g++) {
if (isPrimitiveRoot(g, n, phi, factors, size)) {
printf("%d ", g);
found = 1;
}
}
if (!found) {
printf("None");
}
printf("\n");
}

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;

printf("Enter a prime number (p): ");


scanf("%lld", &p);
printf("Enter a primitive root (g): ");
scanf("%lld", &g);

printf("Enter private key for Alice: ");


scanf("%lld", &a);
A = power(g, a, p);

printf("Enter private key for Bob: ");


scanf("%lld", &b);
B = power(g, b, p);

key1 = power(B, a, p);


key2 = power(A, b, p);

printf("Shared key (computed by Alice): %lld\n", key1);


printf("Shared key (computed by Bob): %lld\n", key2);
printf("Sachhyam Sthapit, 20790521");

return 0;
}
Lab16: WAP to implement RSA Algorithm (Encryption/Decryption).

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

int gcd(int a, int b) {


while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

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 modInverse(int e, int phi) {


int d = 1;
while ((d * e) % phi != 1) {
d++;
if (d >= phi) return -1;
}
return d;
}

int main() {
int p, q, n, phi, e, d;
long long message, encrypted, decrypted;

printf("Enter two prime numbers (p and q): ");


scanf("%d %d", &p, &q);

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

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


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

printf("\nEnter a numeric message (should be less than %d): ", n);


scanf("%lld", &message);

if (message >= n) {
printf("Message must be smaller than n (%d).\n", n);
return 1;
}

encrypted = modExp(message, e, n);


printf("Encrypted Message: %lld\n", encrypted);

decrypted = modExp(encrypted, d, n);


printf("Decrypted Message: %lld\n", decrypted);

return 0;
}
Lab 17: WAP to compute discrete logarithm of given number.

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

int discrete_log(int base, int result, int mod) {


int x = 0, power = 1;
while (x < mod) {
if (power == result) {
return x;
}
power = (power * base) % mod;
x++;
}
return -1;
}

int main() {
int base, result, mod, log_value;

printf("Enter base (g): ");


scanf("%d", &base);
printf("Enter result (y): ");
scanf("%d", &result);
printf("Enter modulo (p): ");
scanf("%d", &mod);

log_value = discrete_log(base, result, mod);

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.

6 WAP which takes two numbers and display 13 June 2024


whether they are relatively prime or not.
7 Compute GCD (Recursive and Iterative) 18 June 2024

8 WAP to implement Extended Euclidean 21 June 2024


Algorithm.

9 WAP to compute multiplicative inverse in given 26 June 2024


modulo n using Extended Euclidean Algorithm.

10 WAP to implement Hill Cipher. 8 July 2024


11 WAP to demonstrate how output of S-Box (S1) is 9 July 2024
generated in DES.

12 WAP to implement Robin Miller algorithm for 12 July 2024


primality test.
13 WAP that takes any positive number and display 15 July 2024
the result after computing Totient value.

14 WAP to compute primitive roots of given 16 July 2024


number.

15 WAP to implement Diffie-Hellman Key Exchange 17 July 2024


Algorithm.

16 Implement RSA Algorithm 18 July 2024


(Encryption/Decryption)
17 Compute discrete logarithm of a given number 19 July 2024

You might also like