Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.0 kB
1
Indexable
Never
#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);
    
    // Factorising n = 6887 , p =71, q =97
    // Calculate phi(n) = (p-1)(q-1)
    BN_set_word(phi_n, 6720);

    // 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);

    return 0;
}

Leave a Comment