|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello
I´d like to know if u can help figure out a way to find large prime numbers in other to create the private and public keys on a RSA system. The user types in a number(20 digit) and if it isnt prime, I´m supposed to find the closest prime to the number given. I also dont know how I´m suppose to store these numbers because they´re pretty large and I dont know how work w/them. I´d really appreciate if u could help me, thanks |
|
#2
|
|||
|
|||
|
I think you need use some functions from openssl library - bn.h (big interger objects), try bn.h on google, actually it's will be difficult without any expirience with openssl library.
One of the functions provided by then bn.h (BN package) BN_generate_prime http://www.openssl.org/docs/crypto/...rate_prime.html As it’s name implies, the function generates prime numbers, but more importantly, it generates pseudorandom primes. I guess you can find examples on: http://openssl.org |
|
#3
|
|||
|
|||
|
Here some programs (source) for finding and testing primality:
http://primes.utm.edu/links/programs/seeking_large_primes/ |
|
#4
|
|||
|
|||
|
Quote:
I write example for you: - prerequest: installed openssl library Code:
#include <stdio.h>
/*openssl library includes*/
#include <openssl/bn.h>
#include <openssl/ssl.h>
/*function definitions*/
static void prime_status(int code, int arg, void *cb_arg);
BIGNUM *generate_prime(int bits, int safe);
/*show prime status - callback*/
static void prime_status(int code, int arg, void *cb_arg)
{
if (code==0)
fprintf(stderr, "\n * Found potential prime #%d ... ", (arg + 1));
else if (code == 1 && arg && !(arg % 10))
fprintf(stderr, ".");
else
fprintf(stderr, "\nGot one !\n");
}
/*generate prime - bits, safe*/
BIGNUM *generate_prime(int bits, int safe)
{
char *string;
BIGNUM *prime;
fprintf(stderr, "Searching from a %sprime %d bits in size ...", (safe ? "safe" : ""), bits);
/*generate prime*/
prime=BN_generate_prime(NULL, bits, safe, NULL, NULL, prime_status, NULL);
/*if not generated, return NULL pointer*/
if(!prime)
return NULL;
/*convert big number to decimal*/
string=BN_bn2dec(prime);
/*show result*/
if(string)
{
fprintf(stderr, "Found prime: %s\n", string);
OPENSSL_free(string);
}
return prime;
}
int main()
{
/*pointer on BIGNUM*/
BIGNUM *bn;
/*allocate an initialize a new BIGNUM - dynamicaly*/
bn=BN_new();
/*get big number - prime, 100-bits 10-safe*/
/*CHANGE THIS VALUES*/
if((bn = generate_prime(100, 100)) == NULL)
{
fprintf(stderr, "Can't generate prime !!!\n");
return -3;
}
/*free memory*/
BN_free(bn);
return 1;
}
To compile: gcc -o prime prime.c -lcrypto |
|
#5
|
|||
|
|||
|
Thank you for ur help, but do u know if there's another library that I can use w/ dev-c++(Windows) because I was looking at the openSSL and it loooked really helpful but it only works w/ Linux, right?
|
|
#6
|
|||
|
|||
|
Quote:
Pointer to Openssl library for Windows: Openssl for Windows download and install |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > prime numbers |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|