0% found this document useful (0 votes)
23 views1 page

Task1 Function

Uploaded by

abdklaib233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views1 page

Task1 Function

Uploaded by

abdklaib233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <stdio.

h>
#include <openssl/bn.h>

// print function
void printBN(char *msg, BIGNUM *a)
{
/* Use BN_bn2hex(a) for hex string
* Use BN_bn2dec(a) for decimal string */
char *number_str = BN_bn2hex(a);
printf("%s %s\n", msg, number_str);
OPENSSL_free(number_str);
}

BIGNUM *Calculate_d(BIGNUM *p, BIGNUM *q, BIGNUM *e)


{
BN_CTX *ctx = BN_CTX_new();
BIGNUM *H = BN_new();
BIGNUM *n = BN_new();
BIGNUM *p_minus = BN_new();
BIGNUM *q_minus = BN_new();
BIGNUM *phi = BN_new();
BIGNUM *d = BN_new();

BN_hex2bn(&H, "1");
BN_sub(p_minus, p, H);
BN_sub(q_minus, q, H);
BN_mul(phi, p_minus, q_minus, ctx);
BN_mod_inverse(d, e, phi, ctx);

BN_CTX_free(ctx);
return d; // Returning the pointer to d
}

int main()
{
BIGNUM *d = BN_new();
// I'm initializing the parameters to be passed to the function here, then
calling the function and storing the return value in d.
BIGNUM *P = BN_new();
BIGNUM *Q = BN_new();
BIGNUM *E = BN_new();

BN_hex2bn(&P, "F7E75FDC469067FFDC4E847C51F452DF");
BN_hex2bn(&Q, "E85CED54AF57E53E092113E62F436F4F");
BN_hex2bn(&E, "0D88C3");

d = Calculate_d(P, Q, E);
printBN(" d = e⁻¹ mod Φ 😈= ", d); // open the unicode keyboard to use emojis 😈

// Free the BIGNUMs after usage


BN_free(P);
BN_free(Q);
BN_free(E);
BN_free(d);

return 0;
}

You might also like