Untitled
unknown
plain_text
2 years ago
1.1 kB
7
Indexable
#include <stdio.h>
#include <openssl/bn.h>
void initialize_bn(BIGNUM **bn, const char *value) {
*bn = BN_new();
BN_dec2bn(bn, value);
}
void print_bn(const char *label, BIGNUM *bn) {
char *value = BN_bn2dec(bn);
printf("%s: %s\n", label, value);
OPENSSL_free(value);
}
int main() {
BN_CTX *ctx = BN_CTX_new();
BIGNUM *n, *e, *M, *d, *C;
initialize_bn(&n, "6887");
initialize_bn(&e, "143");
initialize_bn(&M, "1234");
// Compute d: The modular inverse of e modulo phi(n)
// For simplicity, and since n = 6887 is a product of two primes (83 and 83),
// phi(n) = (p-1)(q-1) = 82*82 = 6724
BIGNUM *phi_n;
initialize_bn(&phi_n, "6724");
d = BN_mod_inverse(NULL, e, phi_n, ctx);
// Compute C = M^e mod n
C = BN_new();
BN_mod_exp(C, M, e, n, ctx);
print_bn("Decryption key (d)", d);
print_bn("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