Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.1 kB
2
Indexable
Never
#include <stdio.h>
#include <openssl/bn.h>

int main() {
    BN_CTX *ctx = BN_CTX_new();
    BIGNUM *p = BN_new();
    BIGNUM *q = BN_new();
    BIGNUM *n = BN_new();
    BIGNUM *e = BN_new();
    BIGNUM *d = BN_new();
    BIGNUM *M = BN_new();
    BIGNUM *C = BN_new();

    // Given values
    BN_hex2bn(&n, "6887");
    BN_dec2bn(&e, "143");
    BN_dec2bn(&M, "1234");

    // Calculate p and q (prime factors of n)
    BN_generate_prime_ex(p, 1024, NULL, NULL, NULL);
    BN_div(q, NULL, n, p, ctx);

    // Calculate phi(n)
    BIGNUM *phi_n = BN_new();
    BN_sub(p, p, BN_value_one());
    BN_sub(q, q, BN_value_one());
    BN_mul(phi_n, p, q, ctx);

    // Calculate d (private key)
    BN_mod_inverse(d, e, phi_n, ctx);

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

    // Print the calculated values
    printf("Calculated d: %s\n", BN_bn2hex(d));
    printf("Calculated C: %s\n", BN_bn2hex(C));

    // Free memory
    BN_free(p);
    BN_free(q);
    BN_free(n);
    BN_free(e);
    BN_free(d);
    BN_free(M);
    BN_free(C);
    BN_free(phi_n);
    BN_CTX_free(ctx);

    return 0;
}
Leave a Comment