Untitled

 avatar
unknown
plain_text
a year ago
1.1 kB
7
Indexable
#include <openssl/bn.h>
#include <stdio.h>

void printBN(char *msg, BIGNUM *a) {
    char *number_str = BN_bn2dec(a);
    printf("%s%s\n", msg, number_str);
    OPENSSL_free(number_str);
}

int main() {
    BN_CTX *ctx = BN_CTX_new();
    
    // Initialize variables for Task 1
    BIGNUM *n = BN_new();
    BIGNUM *e = BN_new();
    BIGNUM *M = BN_new();
    BIGNUM *d = BN_new();
    BIGNUM *C = BN_new();
    BIGNUM *phi_n = BN_new();
    
    // Set values for n, e, and M
    BN_set_word(n, 6887);
    BN_set_word(e, 143);
    BN_set_word(M, 1234);

    // Calculate phi(n) = (p-1)(q-1) for p = q = 83
    BN_set_word(phi_n, 6724);

    // Calculate d, the modular multiplicative inverse of e modulo phi(n)
    BN_mod_inverse(d, e, phi_n, ctx);

    // Calculate ciphertext C = M^e mod n
    BN_mod_exp(C, M, e, n, ctx);

    // Output d and C
    printBN("Decryption key (d): ", d);
    printBN("Ciphertext (C): ", C);

    // Cleanup
    BN_free(n);
    BN_free(e);
    BN_free(M);
    BN_free(d);
    BN_free(C);
    BN_free(phi_n);
    BN_CTX_free(ctx);

    return 0;
}
Editor is loading...
Leave a Comment