I am new to openssl library and from what I could understand it is possible to generate large primes say 512 bit using the command line argument :openssl prime -generate -bits 512. But I am not sure how can I do this using a C code?

I tried using system() command to generate prime using following code:

int main()
  char c[50];
  strcpy(c, "openssl prime -generate -bits 512");
  long long p = system(c); // IS THIS POSSIBLE??
  return 0;

But I have read there are some security concerns on this as given in this link here

So is it possible to write this large prime generated in a text file or something so that I can use it later in my code.

My final motive is to generate two large prime numbers of order 512 bits.


1 Answer 1


If you want to know how you can do the functionality of openssl prime -generate in C code have a look at the source code of OpenSSL which is open source and easily accessible. Specifically have a look at apps/prime.c which implements openssl prime. There you can see that BN_generate_prime_ex is used to generate the prime:

if (generate) {
    bn = BN_new();
    if (!BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL)) {
        BIO_printf(bio_err, "Failed to generate prime.\n");
        goto end;
  • Thank you for guiding me to right path in openssl repo , but I am getting an error as undefined refernce to bn_NEW(). I have included <openssl/bn.h> header file. What am I missing ?
    – skii
    Jan 5, 2018 at 6:52
  • @AkankshaDixit: This is security.stackexchange.com. This is about concepts and to point you in the right direction. If you have problems with your specific code then 1. ask at stackoverflow.com and 2. provide all the information needed to understand and reproduce your problem. This includes usually providing the actual code in a minimal but complete example and providing the exact error messages. Jan 5, 2018 at 7:00
  • where safe is defined as: int safe = 1
    – CipherX
    Oct 25, 2021 at 8:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .