Untitled

 avatar
unknown
plain_text
a year ago
1.1 kB
2
Indexable
#include <stdlib.h>

#include <stdio.h>

// Function to compute modular exponentiation (base^exponent % modulus)
int modular_exponentiation(int base, int exponent, int modulus) {
    int result = 1;
    base = base % modulus;
    while (exponent > 0) {
        if (exponent % 2 == 1)
            result = (result * base) % modulus;
        exponent = exponent >> 1;
        base = (base * base) % modulus;
    }
    return result;
}

// Diffie-Hellman key exchange function
int diffie_hellman(int p, int g, int xa, int xb) {
    // Calculate public keys
    int ya = modular_exponentiation(g, xa, p);
    int yb = modular_exponentiation(g, xb, p);

    // Calculate shared secret key
    int Kab = modular_exponentiation(yb, xa, p);

    return Kab;
}

int main() {
    // Parameters
    int p = 773;  // Prime number
    int g = 2;    // Primitive root
    int xa = 333; // Alice's secret key
    int xb = 603; // Bob's secret key

    // Perform Diffie-Hellman key exchange
    int shared_secret = diffie_hellman(p, g, xa, xb);

    // Output shared secret key
    printf("Shared secret key (Kab): %d\n", shared_secret);

    return 0;
}
Leave a Comment