labb
labb
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>
#include <stdio.h>
int main() {
int test_cases[][2] = {{23, 428}, {24, 428}};
int num_cases = sizeof(test_cases) / sizeof(test_cases[0]);
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 main() {
int test_cases[][2] = {{7, 23}, {10, 29}};
int num_cases = sizeof(test_cases) / sizeof(test_cases[0]);
int main() {
int test_cases[][2] = {{23, 428}, {7, 23}, {10, 29}};
int num_cases = sizeof(test_cases) / sizeof(test_cases[0]);
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]);
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);
int main() {
int n = 61;
int bases[] = {2, 3, 5}; // Bases to test
int k = sizeof(bases) / sizeof(bases[0]);
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);
9. RC4
#include <stdio.h>
#include <string.h>
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];
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];
int main() {
char key[] = "PLAYFAIR";
generate_playfair_key(key);
}
III.
Hill cipher
#include <stdio.h>
#include <string.h>
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>
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);
}