0% found this document useful (0 votes)
19 views17 pages

labb

The document contains multiple C code implementations for various cryptographic algorithms, including modular exponentiation, extended Euclidean algorithm, modular inverse, and the Chinese Remainder Theorem. It also covers primality testing using the Miller-Rabin algorithm, as well as the implementation of P-box and S-box for DES, and the RC4 encryption algorithm. Each section provides example test cases and outputs relevant results for the algorithms.

Uploaded by

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

labb

The document contains multiple C code implementations for various cryptographic algorithms, including modular exponentiation, extended Euclidean algorithm, modular inverse, and the Chinese Remainder Theorem. It also covers primality testing using the Miller-Rabin algorithm, as well as the implementation of P-box and S-box for DES, and the RC4 encryption algorithm. Each section provides example test cases and outputs relevant results for the algorithms.

Uploaded by

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

1.

Modular Exponentiation

#include <stdio.h>
long long mod_exp(long long base, long long exp, long long mod) {
long long res = 1;
printf("Base: %lld, Exponent: %lld, Mod: %lld\n", base, exp, mod);
while (exp) {
if (exp % 2) res = (res * base) % mod;
base = (base * base) % mod;
exp /= 2;
printf("Step -> Base: %lld, Exp: %lld, Res: %lld\n", base, exp, res);
}
return res;
}

int main() {
printf("Result: %lld\n", mod_exp(22, 37, 67));
printf("Result: %lld\n", mod_exp(24, 37, 67));
}
2.Extended euclidean alg

#include <stdio.h>

int extended_euclidean(int a, int b, int *s, int *t) {


if (!b) return *s = 1, *t = 0, a;
int s1, t1, gcd = extended_euclidean(b, a % b, &s1, &t1);
*s = t1, *t = s1 - (a / b) * t1;
printf("Step -> a: %d, b: %d, s: %d, t: %d, gcd: %d\n", a, b, *s, *t, gcd);
return gcd;
}
int main() {
int test_cases[][2] = {{428, 23}, {125, 23}};
int num_cases = sizeof(test_cases) / sizeof(test_cases[0]);

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


int a = test_cases[i][0], b = test_cases[i][1], s, t;
int gcd = extended_euclidean(a, b, &s, &t);
printf("Final GCD: %d, s: %d, t: %d\n\n", gcd, s, t);
}
return 0;
}
3. Modular Inverse

#include <stdio.h>

int extended_euclidean(int a, int b, int *s, int *t) {


if (!b) return *s = 1, *t = 0, a;
int s1, t1, gcd = extended_euclidean(b, a % b, &s1, &t1);
*s = t1, *t = s1 - (a / b) * t1;
printf("Step -> a: %d, b: %d, s: %d, t: %d, gcd: %d\n", a, b, *s, *t, gcd);
return gcd;
}

int mod_inverse(int a, int m) {


int s, t, gcd = extended_euclidean(a, m, &s, &t);
if (gcd != 1) return printf("No Modular Inverse\n"), -1;
return (s % m + m) % m;
}

int main() {
int test_cases[][2] = {{23, 428}, {24, 428}};
int num_cases = sizeof(test_cases) / sizeof(test_cases[0]);

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


int a = test_cases[i][0], mod = test_cases[i][1];
int inverse = mod_inverse(a, mod);
if (inverse != -1)
printf("Modular Inverse of %d mod %d = %d\n\n", a, mod, inverse);
}
return 0;
}
4.multiplicative inverse using fermat
#include <stdio.h>

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


int res = 1;
base %= mod;
while (exp) {
if (exp % 2) res = (res * base) % mod;
base = (base * base) % mod;
exp /= 2;
printf("Step -> Base: %d, Exp: %d, Res: %d\n", base, exp, res);
}
return res;
}

int is_prime(int n) {
if (n < 2) return 0;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return 0;
return 1;
}

int mod_inverse_fermat(int a, int p) {


if (!is_prime(p)) return printf("Error: Modulus %d is not prime\n", p), -1;
return mod_exp(a, p - 2, p);
}

int main() {
int test_cases[][2] = {{7, 23}, {10, 29}};
int num_cases = sizeof(test_cases) / sizeof(test_cases[0]);

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


int a = test_cases[i][0], mod = test_cases[i][1];
int inverse = mod_inverse_fermat(a, mod);
if (inverse != -1)
printf("Modular Inverse of %d mod %d = %d\n\n", a, mod, inverse);
}
return 0;
}
5.multiplicative inverse using euler
#include <stdio.h>

// Function to compute (base^exp) % mod using modular exponentiation


int mod_exp(int base, int exp, int mod) {
int res = 1;
base %= mod;
while (exp) {
if (exp % 2) res = (res * base) % mod;
base = (base * base) % mod;
exp /= 2;
printf("Step -> Base: %d, Exp: %d, Res: %d\n", base, exp, res);
}
return res;
}

// Function to compute Euler's Totient Function φ(n)


int euler_totient(int n) {
int res = n;
for (int p = 2; p * p <= n; p++) {
if (n % p) continue;
while (n % p == 0) n /= p;
res -= res / p;
printf("Step -> Prime: %d, φ(n): %d\n", p, res);
}
if (n > 1) res -= res / n;
return res;
}

// Function to compute modular inverse using Euler's Theorem


int mod_inverse_euler(int a, int n) {
if (a % n == 0) return printf("No Inverse Exists\n"), -1;
int phi = euler_totient(n);
printf("Euler’s Totient φ(%d) = %d\n", n, phi);
return mod_exp(a, phi - 1, n);
}

int main() {
int test_cases[][2] = {{23, 428}, {7, 23}, {10, 29}};
int num_cases = sizeof(test_cases) / sizeof(test_cases[0]);

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


int a = test_cases[i][0], mod = test_cases[i][1];
int inverse = mod_inverse_euler(a, mod);
if (inverse != -1)
printf("Modular Inverse of %d mod %d = %d\n\n", a, mod, inverse);
}
return 0;
}
6. Implement crt
#include <stdio.h>

// Extended Euclidean Algorithm to find modular inverse


int extended_euclidean(int a, int b, int *x, int *y) {
if (!b) return *x = 1, *y = 0, a;
int x1, y1, gcd = extended_euclidean(b, a % b, &x1, &y1);
*x = y1, *y = x1 - (a / b) * y1;
return gcd;
}

// Function to compute modular inverse


int mod_inverse(int a, int mod) {
int x, y;
int gcd = extended_euclidean(a, mod, &x, &y);
return (gcd == 1) ? (x % mod + mod) % mod : -1;
}

// Function to solve Chinese Remainder Theorem


int chinese_remainder(int a[], int n[], int size) {
int prod = 1, sum = 0;
for (int i = 0; i < size; i++) prod *= n[i];

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


int mi = prod / n[i]; // Compute partial modulus
int inv = mod_inverse(mi, n[i]); // Compute modular inverse
if (inv == -1) return printf("No solution exists\n"), -1;

sum += a[i] * mi * inv; // Apply CRT formula


printf("Step -> ai: %d, ni: %d, mi: %d, inv: %d, term: %d\n",
a[i], n[i], mi, inv, a[i] * mi * inv);
}

return sum % prod;


}

int main() {
int a[][3] = {{2, 3, 2}, {2, 3, 2}, {3, 5, 7}};
int n[][3] = {{3, 5, 7}, {3, 5, 10}, {13, 10, 23}};
int num_cases = sizeof(a) / sizeof(a[0]);

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


int size = sizeof(a[i]) / sizeof(a[i][0]);
int result = chinese_remainder(a[i], n[i], size);
if (result != -1)
printf("Solution: x ≡ %d (mod %d)\n\n", result, n[i][0] * n[i][1] * n[i][2]);
}
return 0;
}
7.miller rabin prime testing alg
#include <stdio.h>

// Function to compute (base^exp) % mod using modular exponentiation


int mod_exp(int base, int exp, int mod) {
int res = 1;
base %= mod;
while (exp) {
if (exp % 2) res = (res * base) % mod;
base = (base * base) % mod;
exp /= 2;
printf("Step -> Base: %d, Exp: %d, Res: %d\n", base, exp, res);
}
return res;
}

// Miller-Rabin test for a given base 'a'


int check_composite(int n, int d, int r, int a) {
int x = mod_exp(a, d, n);
if (x == 1 || x == n - 1) return 0; // Probably prime
for (int i = 0; i < r - 1; i++) {
x = (x * x) % n;
printf("Step -> Squaring x: %d\n", x);
if (x == n - 1) return 0;
}
return 1; // Composite
}
// Miller-Rabin Primality Test
int miller_rabin(int n, int bases[], int k) {
if (n < 2) return printf("Step -> %d is less than 2. Not Prime.\n", n), 0;
if (n == 2 || n == 3) return printf("Step -> %d is Prime.\n", n), 1;

int d = n - 1, r = 0;
while (d % 2 == 0) d /= 2, r++;
printf("Step -> Expressed n-1 as %d * 2^%d\n", d, r);

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


if (bases[i] < 2 || bases[i] >= n - 1) continue;
printf("Step -> Testing with base: %d\n", bases[i]);
if (check_composite(n, d, r, bases[i])) return printf("No\n"), 0;
}
return printf("Yes\n"), 1;
}

int main() {
int n = 61;
int bases[] = {2, 3, 5}; // Bases to test
int k = sizeof(bases) / sizeof(bases[0]);

miller_rabin(n, bases, k);


return 0;
}
8.Write a function to implement P-box and S-box of DES in which the
permutation/substitution are defined by tables.
Input: Table, Bits for permutation/substitution
Output: permutation/substitution bits
#include <stdio.h>

// P-Box Permutation Function


void p_box(int p[], int bits[]) {
int out[4];
for (int i = 0; i < 4; i++) {
out[i] = bits[p[i] - 1];
printf("P[%d] -> bits[%d] = %d\n", i + 1, p[i], out[i]);
}
printf("P-Box Output: ");
for (int i = 0; i < 4; i++) printf("%d", out[i]);
printf("\n");
}

// S-Box Substitution Function


int s_box(int s[][4], int bits[4]) {
int row = (bits[0] << 1) | bits[3]; // First & last bit → row
int col = (bits[1] << 1) | bits[2]; // Middle 2 bits → column
printf("Row: %d, Col: %d -> S-Box Value: %d\n", row, col, s[row][col]);
return s[row][col];
}

int main() {
// Reduced P-Box Table
int p_table[] = {2, 4, 1, 3}; // Simplified
int bits_p[] = {1, 0, 1, 1}; // Input bits
p_box(p_table, bits_p);

// Reduced S-Box Table (2x4 instead of 4x16)


int s_box_table[4][4] = {
{14, 4, 13, 1}, // Row 0
{2, 15, 11, 8},
{3,10,6,12},
{5,9,0,7}// Row 1
};
int bits_s[] = {1, 0, 1, 1}; // Example 4-bit input
int s_out = s_box(s_box_table, bits_s);
printf("S-Box Output: %02b\n", s_out); // Print as 2-bit binary
}

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

// RC4 Key Scheduling Algorithm (KSA)


void rc4_init(unsigned char *S, unsigned char *key, int key_len) {
int j = 0;
for (int i = 0; i < 256; i++) S[i] = i;
for (int i = 0; i < 256; i++) {
j = (j + S[i] + key[i % key_len]) % 256;
unsigned char temp = S[i];
S[i] = S[j], S[j] = temp;
}
}

// RC4 Pseudo-Random Generation Algorithm (PRGA)


void rc4_crypt(unsigned char *S, unsigned char *data, int data_len) {
int i = 0, j = 0;
for (int k = 0; k < data_len; k++) {
i = (i + 1) % 256;
j = (j + S[i]) % 256;
unsigned char temp = S[i];
S[i] = S[j], S[j] = temp;
int keystream = S[(S[i] + S[j]) % 256];
printf("Step -> i: %d, j: %d, keystream: %d, input: %d, output: %d\n",
i, j, keystream, data[k], data[k] ^ keystream);
data[k] ^= keystream; // XOR with keystream
}
}

int main() {
unsigned char key[] = "Key"; // Example key
unsigned char text[] = "Hello"; // Example plaintext
int key_len = strlen((char *)key);
int text_len = strlen((char *)text);
unsigned char S[256];

printf("Original Text: %s\n", text);


rc4_init(S, key, key_len);
rc4_crypt(S, text, text_len);
printf("Encrypted: %s\n", text);

rc4_init(S, key, key_len); // Reinitialize for decryption


rc4_crypt(S, text, text_len);
printf("Decrypted: %s\n", text);
}
I.
Caesar Cipher
#include <stdio.h>
#include <string.h>

void caesar_cipher(char text[], int key, int decrypt) {


for (int i = 0; text[i]; i++) {
if (text[i] >= 'A' && text[i] <= 'Z')
text[i] = ((text[i] - 'A' + (decrypt ? -key : key) + 26) % 26) + 'A';
else if (text[i] >= 'a' && text[i] <= 'z')
text[i] = ((text[i] - 'a' + (decrypt ? -key : key) + 26) % 26) + 'a';
printf("Step %d -> %c\n", i + 1, text[i]);
}
}

int main() {
char text[] = "HELLO";
int key = 3;
caesar_cipher(text, key, 0);
printf("Encrypted: %s\n", text);
caesar_cipher(text, key, 1);
printf("Decrypted: %s\n", text);
}

II.
Playfair Cipher
#include <stdio.h>
#include <string.h>

char key_square[5][5];

void generate_playfair_key(char key[]) {


int used[26] = {0}, k = 0;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++) {
while (k < strlen(key) && used[key[k] - 'A']) k++;
key_square[i][j] = (k < strlen(key)) ? key[k++] : 'A' + i * 5 + j;
used[key_square[i][j] - 'A'] = 1;
printf("%c ", key_square[i][j]);
}
printf("\n");
}

int main() {
char key[] = "PLAYFAIR";
generate_playfair_key(key);
}

III.
Hill cipher
#include <stdio.h>
#include <string.h>

void hill_cipher(int key[2][2], char text[2]) {


int res[2] = {0};
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
res[i] += key[i][j] * (text[j] - 'A');

printf("Encrypted: %c%c\n", (res[0] % 26) + 'A', (res[1] % 26) + 'A');


}

int main() {
int key[2][2] = {{6, 24}, {1, 13}};
char text[] = "HI";
hill_cipher(key, text);
}
IV.
Vigenère Cipher
#include <stdio.h>
#include <string.h>

void vigenere_cipher(char text[], char key[], int decrypt) {


int len = strlen(text), klen = strlen(key);
for (int i = 0; i < len; i++) {
int shift = key[i % klen] - 'A';
text[i] = ((text[i] - 'A' + (decrypt ? -shift : shift) + 26) % 26) + 'A';
printf("Step %d -> %c\n", i + 1, text[i]);
}
}

int main() {
char text[] = "HELLO", key[] = "KEY";
vigenere_cipher(text, key, 0);
printf("Encrypted: %s\n", text);
vigenere_cipher(text, key, 1);
printf("Decrypted: %s\n", text);
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy