LAB 1: WAP to implement Shift Cipher.
(encryption / Decryption / input (key / plain text for
encryption / cipher text for decryption) should be taken form user).
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void shift_cipher(char text[], int key, int encrypt) {
int shift = encrypt ? key : -key;
for (int i = 0; text[i] != '\0'; i++) {
if (isalpha(text[i])) {
char base = isupper(text[i]) ? 'A' : 'a';
text[i] = base + (text[i] - base + shift + 26) % 26;
int main() {
char choice, text[100];
int key;
printf("Choose operation: (E)ncrypt or (D)ecrypt: ");
scanf(" %c", &choice);
printf("Enter shift key: ");
scanf("%d", &key);
printf("Enter text: ");
scanf(" %s", text);
if (choice == 'E' || choice == 'e') {
shift_cipher(text, key, 1);
printf("Encrypted text: %s\n", text);
} else if (choice == 'D' || choice == 'd') {
shift_cipher(text, key, 0);
printf("Decrypted text: %s\n", text);
} else {
printf("Invalid choice! Please select 'E' or 'D'.\n");
printf("swikriti Timalsena,31");
return 0;
}
Lab 2: 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[]);
void findPosition(char ch, int *row, int *col);
void encrypt(char text[]);
void decrypt(char text[]);
void displayKeyMatrix();
int main() {
char key[50], text[100];
int choice;
printf("Enter key: ");
scanf("%s", key);
prepareKeyMatrix(key);
displayKeyMatrix();
printf("1. Encrypt\n2. Decrypt\nEnter choice: ");
scanf("%d", &choice);
getchar();
printf("Enter text: ");
gets(text);
if (choice == 1) encrypt(text);
else decrypt(text);
printf("Result: %s\n", text);
printf("Swikriti Timalsena, 31");
return 0;
void prepareKeyMatrix(char key[]) {
int map[26] = {0}, k = 0, i, j;
for (i = 0; key[i]; i++) key[i] = (key[i] == 'j') ? 'i' : tolower(key[i]);
for (i = 0; i < SIZE; i++)
for (j = 0; j < SIZE; j++) {
while (map[key[k] - 'a'] && key[k]) k++;
keyMatrix[i][j] = key[k] ? key[k++] : (map[i * SIZE + j] ? ' ' : 'a' + i * SIZE + j);
void displayKeyMatrix() {
for (int i = 0; i < SIZE; i++, printf("\n"))
for (int j = 0; j < SIZE; j++)
printf("%c ", keyMatrix[i][j]);
void findPosition(char ch, int *row, int *col) {
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
if (keyMatrix[i][j] == ch) {*row = i; *col = j; return;}
void encrypt(char text[]) {
int i = 0, r1, c1, r2, c2;
while (text[i]) {
findPosition(text[i], &r1, &c1);
findPosition(text[i+1] ? text[i+1] : 'x', &r2, &c2);
text[i] = (r1 == r2) ? keyMatrix[r1][(c1+1)%SIZE] : (c1 == c2) ? keyMatrix[(r1+1)%SIZE]
[c1] : keyMatrix[r1][c2];
text[i+1] = (r1 == r2) ? keyMatrix[r2][(c2+1)%SIZE] : (c1 == c2) ?
keyMatrix[(r2+1)%SIZE][c2] : keyMatrix[r2][c1];
i += 2;
void decrypt(char text[]) {
int i = 0, r1, c1, r2, c2;
while (text[i]) {
findPosition(text[i], &r1, &c1);
findPosition(text[i+1], &r2, &c2);
text[i] = (r1 == r2) ? keyMatrix[r1][(c1+4)%SIZE] : (c1 == c2) ? keyMatrix[(r1+4)%SIZE]
[c1] : keyMatrix[r1][c2];
text[i+1] = (r1 == r2) ? keyMatrix[r2][(c2+4)%SIZE] : (c1 == c2) ?
keyMatrix[(r2+4)%SIZE][c2] : keyMatrix[r2][c1];
i += 2;
}
Lab 3 :WAP to implement Rail Fence Cipher. (encryption / Decryption / input should be
taken from user).
#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<len;i++)
for(j=0;j<len;j++)
rail[key][len]='\n';
for(i=0;i<len;i++){
if(row == 0 || row == key-1)
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: \n");
scanf("%d",&key);
decryptRailFence(cipher,key);
printf("Swikriti Timalsena, 31");
}
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("Syabrona Shahi, 20790534");
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);
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("Swikriti Timalsena, 31");
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) { // Ensure even length by adding 'X'
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; // Exit if matrix is not invertible
encryptHill(plaintext, key, ciphertext);
printf("\nEncrypted text: %s\n", ciphertext);
decryptHill(ciphertext, inverseKey, decrypted);
printf("Decrypted text: %s\n", decrypted);
printf("Swikriti Timalsena, 31");
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("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*1<=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("Please enter a postive Integer:\n");
return 1;
int result = eulerTotient(n);
printf("Euler's Toitent of %d is:%d\n",n,result);
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);
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) // If exp is odd, multiply base with result
result = (result * base) % mod;
exp = exp >> 1; // Divide exp by 2
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 Topic Date Signature
No.
1. LAB 1: WAP to implement Shift Cipher. (encryption / Decryption).
2. Write a program to implement Playfair Cipher.
3. WAP to implement Rail Fence Cipher. (encryption / Decryption )
4. Write a program to implement Vigenere Cipher
5. WAP to implement Euclidean Algorithm to find GCD of given
numbers.
6. WAP that computes additive inverse in given modulo n.
7. WAP which takes two numbers and display whether they are
relatively prime or not.
8. Write a program to implement Extended Euclidean Algorithm.
9. WAP to compute multiplicative inverse in given modulo n using
Extended Euclidean Algorithm.
10. Write a program to implement Hill Cipher (Key matrix of size 2*2/
Encryption/ Decryption/ Input should be taken from user).
11. WAP to demonstrate how output of S-Box (S1) is generated in DES.
12. Write a program to implement Robin Miller algorithm for
primality test.
13. Write a program that takes any positive number and display the
result after computing Totient value.
14. Write a program to compute primitive roots of given number.
15. WAP to implement Diffie-Helman Key Exchange Algorithm.
16. WAP to implement RSA Algorithm (Encryption/Decryption/ Input
Should be taken from user)
Tribhuwan University
Prime College
Khusibun,Nayabazar
Lab Report
Of
Information Security
Submitted by: Submitted to:
Swikriti Timalsena Suman Gupta
Roll no:31