# Untitled

unknown
plain_text
a month ago
1.5 kB
2
Indexable
Never
```#include <openssl/bn.h>
#include <stdio.h>

void printBN(const 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 given values
BIGNUM *p = BN_new();
BIGNUM *q = BN_new();
BIGNUM *n = BN_new();
BIGNUM *phi_n = BN_new();
BIGNUM *e = BN_new();
BIGNUM *d = BN_new();
BIGNUM *M = BN_new();
BIGNUM *C = BN_new();

// Assigning given values
BN_set_word(p, 37);
BN_set_word(q, 67);
BN_set_word(e, 169);
BN_set_word(M, 1234);

// Calculate n = p * q
BN_mul(n, p, q, ctx);

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

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

// Encrypt: C = M^e mod n
BN_mod_exp(C, M, e, n, ctx);

// Output the results
printBN("Modulus n: ", n);
printBN("Totient phi(n): ", phi_n);
printBN("Private key d: ", d);
printBN("Ciphertext C: ", C);

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

return 0;
}```