本文整理汇总了C++中RSA_generate_key函数的典型用法代码示例。如果您正苦于以下问题:C++ RSA_generate_key函数的具体用法?C++ RSA_generate_key怎么用?C++ RSA_generate_key使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RSA_generate_key函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testRSAGen
void testRSAGen(){
RSA *r;
int bits=512,ret;
unsigned long e=RSA_3;
BIGNUM *bne;
r=RSA_generate_key(bits,e,NULL,NULL);
RSA_print_fp(stdout,r,11);
JCG("--------------------------------\n");
RSA_free(r);
bne=BN_new();
ret=BN_set_word(bne,e);
r=RSA_new();
ret=RSA_generate_key_ex(r,bits,bne,NULL);
if(ret!=1)
{
JCG("RSA_generate_key_ex err!\n");
return;
}
/*RSA_print_fp(stdout,r,11); */
RSA_free(r);
}
示例2: main
int main(int argc, char *argv[])
{
RSA *key;
FILE *fp;
int keylen = 0;
if (argc != 2) {
fprintf(stderr, "Error: too many/few arguments.\nUsage: %s <numbits>\n", argv[0]);
return 1;
}
keylen = atoi(argv[1]);
if ((key = RSA_generate_key(keylen, 3, NULL, NULL)) == NULL) {
fprintf(stderr, "%s\n", ERR_error_string(ERR_get_error(), NULL));
return 1;
}
if (RSA_check_key(key) < 1)
{
fprintf(stderr, "Error: Problems while generating RSA Key.\nRetry.\n");
return 1;
}
fp = fopen(SECFILE,"w");
if (PEM_write_RSAPrivateKey(fp, key, NULL, NULL, 0, 0, NULL) == 0)
{
fprintf(stderr, "Error: problems while writing RSA Private Key.\n");
return 1;
}
fclose(fp);
fp = fopen(PUBFILE, "w");
if (PEM_write_RSAPublicKey(fp, key) == 0) {
fprintf(stderr, "Error: problems while writing RSA Public Key.\n");
return 1;
}
fclose(fp);
RSA_free(key);
printf("RSA key generated.\nLenght (bits) = %d\n", keylen);
return 0;
}
示例3: generateKeysRSA
int generateKeysRSA(EVP_PKEY** privKey, EVP_PKEY** pubKey){
RSA* rsa = NULL;
if(privKey == NULL || pubKey == NULL)
return 0;
*privKey = EVP_PKEY_new();
if(*privKey == NULL){
printf("ERR EVP_PKEY_new\n");
return 0;
}
*pubKey = EVP_PKEY_new();
if(*pubKey == NULL){
printf("ERR EVP_PKEY_new\n");
return 0;
}
rsa = RSA_generate_key(2048, RSA_F4, NULL, NULL);
if(rsa == NULL){
printf("ERR RSA_generate_key\n");
return 0;
}
if(1 != EVP_PKEY_assign_RSA(*privKey,
RSAPrivateKey_dup(rsa))){
printf("ERR EVP_PKEY_assign_RSA\n");
return 0;
}
if(1 != EVP_PKEY_assign_RSA(*pubKey,
RSAPublicKey_dup(rsa))){
printf("ERR EVP_PKEY_assign_RSA\n");
return 0;
}
return 1;
}
示例4: GenerateRsaKey
BOOL GenerateRsaKey(INT32 nBit, RSA *pstPublicKey, RSA *pstPrivateKey)
{
RSA* pstRsa = RSA_generate_key(nBit, 65537, NULL, NULL);
if(NULL == pstRsa)
return false;
//Copy Public Key
pstPublicKey->n = NewBigNum();
CopyBigNum(pstPublicKey->n, pstRsa->n);
pstPublicKey->e = NewBigNum();
CopyBigNum(pstPublicKey->e, pstRsa->e);
pstPublicKey->d = NULL;
pstPublicKey->p = NULL;
pstPublicKey->q = NULL;
pstPublicKey->dmp1 = NewBigNum();
CopyBigNum(pstPublicKey->dmp1, pstRsa->dmp1);
pstPublicKey->dmq1 = NewBigNum();
CopyBigNum(pstPublicKey->dmq1, pstRsa->dmq1);
//Copy Private Key
pstPrivateKey->n = NewBigNum();
CopyBigNum(pstPrivateKey->n, pstRsa->n);
pstPrivateKey->e = NewBigNum();
CopyBigNum(pstPrivateKey->e, pstRsa->e);
pstPrivateKey->d = NewBigNum();
CopyBigNum(pstPrivateKey->d, pstRsa->d);
pstPrivateKey->p = NewBigNum();
CopyBigNum(pstPrivateKey->p, pstRsa->p);
pstPrivateKey->q = NewBigNum();
CopyBigNum(pstPrivateKey->q, pstRsa->q);
pstPrivateKey->dmp1 = NewBigNum();
CopyBigNum(pstPrivateKey->dmp1, pstRsa->dmp1);
pstPrivateKey->dmq1 = NewBigNum();
CopyBigNum(pstPrivateKey->dmq1, pstRsa->dmq1);
RSA_free(pstRsa);
return true;
}
示例5:
/*
* genrsa: generates a new rsa key pair and returns the private key in the RSA
* format. If `pub' is not null, it stores in it the pointer to a newly
* allocated dump of the public key that is `*pub_len' bytes. The same is for
* `priv' and `priv_len'.
* On error null is returned.
*/
RSA *genrsa(int key_bits, u_char **pub, u_int *pub_len, u_char **priv, u_int *priv_len)
{
RSA *rsa=0;
int len;
rsa=RSA_generate_key(key_bits, RSA_F4, NULL, NULL);
if (!rsa) {
debug(DBG_SOFT, "RSA key generation failed");
goto error;
}
if(priv) {
*priv=0;
len=i2d_RSAPrivateKey(rsa, priv);
if(priv_len)
*priv_len=len;
if(len <= 0) {
debug(DBG_SOFT, "Cannot dump RSA public key: %s", ssl_strerr());
goto error;
}
}
if(pub) {
*pub=0;
len=i2d_RSAPublicKey(rsa, pub);
if(pub_len)
*pub_len=len;
if(len <= 0) {
debug(DBG_SOFT, "Cannot dump RSA public key: %s", ssl_strerr());
goto error;
}
}
return rsa;
error:
if(rsa)
RSA_free(rsa);
return 0;
}
示例6: generate_key
int
generate_key(int length, PyObject **py_private_key_ndn,
PyObject **py_public_key_ndn, PyObject **py_public_key_digest,
int *public_key_digest_len)
{
RSA *private_key_rsa;
struct ndn_pkey *private_key = NULL;
int r;
seed_prng();
private_key_rsa = RSA_generate_key(length, 65537, NULL, NULL);
private_key = (struct ndn_pkey *)EVP_PKEY_new();
EVP_PKEY_assign_RSA ((EVP_PKEY *)private_key, private_key_rsa);
save_seed ();
if (!private_key_rsa || !private_key) {
unsigned int err;
err = ERR_get_error();
PyErr_Format(g_PyExc_NDNKeyError, "Unable to generate the"
" key: %s", ERR_reason_error_string(err));
return -1;
}
r = ndn_keypair(0, private_key, py_private_key_ndn,
py_public_key_ndn);
if (r < 0)
return -1;
r = create_public_key_digest(private_key, py_public_key_digest,
public_key_digest_len);
if (r < 0)
return -1;
EVP_PKEY_free ((EVP_PKEY*)private_key);
return 0;
}
示例7: malloc
struct login_ctx *login_create(char *username, char *password) {
struct login_ctx *l;
l = malloc(sizeof(struct login_ctx));
if(l == NULL)
return NULL;
l->state = 0;
l->error = SP_LOGIN_ERROR_OK;
l->sock = -1;
l->service_records = NULL;
l->server_ai = NULL;
l->client_parameters = NULL;
l->server_parameters = NULL;
/* Client key pair */
l->rsa = RSA_generate_key(1024, 65537, NULL, NULL);
/* Diffie-Hellman parameters */
l->dh = DH_new ();
l->dh->p = BN_bin2bn(DH_prime, 96, NULL);
l->dh->g = BN_bin2bn(DH_generator, 1, NULL);
DH_generate_key(l->dh);
strncpy(l->username, username, sizeof(l->username) - 1);
l->username[sizeof(l->username) - 1] = 0;
strncpy(l->password, password, sizeof(l->password) - 1);
l->password[sizeof(l->password) - 1] = 0;
DSFYDEBUG("Login context created at %p\n", l);
return l;
}
示例8: kaa_generate_pub_key
static void kaa_generate_pub_key(void)
{
const int kBits = 2048;
const int kExp = 65537;
RSA *rsa = RSA_generate_key(kBits, kExp, 0, 0);
BIO *bio_pem = BIO_new(BIO_s_mem());
i2d_RSA_PUBKEY_bio(bio_pem, rsa);
kaa_public_key_length = BIO_pending(bio_pem);
kaa_public_key = (char *) KAA_MALLOC(kaa_public_key_length);
if (!kaa_public_key) {
kaa_public_key_length = 0;
BIO_free_all(bio_pem);
RSA_free(rsa);
return;
}
BIO_read(bio_pem, kaa_public_key, kaa_public_key_length);
BIO_free_all(bio_pem);
RSA_free(rsa);
}
示例9: sample_job_outputs_stuff
// A job that does work, with output we care about (pub/priv key)
void sample_job_outputs_stuff(lsq_job_args *args)
{
unsigned char * output = (unsigned char *) args->output_buffer.memory;
int encoded_len = 0;
RSA * rsa_key = RSA_generate_key(2048, 65537, NULL, NULL);
memset(args->output_buffer.memory, 0, args->output_buffer.size);
if(rsa_key)
{
// Make sure it'll fit...
encoded_len = i2d_RSAPublicKey(rsa_key, NULL);
//encoded_len += i2d_RSAPrivateKey(rsa_key, NULL);
memcpy(output, &encoded_len, 4);
output += 4;
if(encoded_len <= args->output_buffer.size)
{
i2d_RSAPublicKey(rsa_key, &output);
//i2d_RSAPrivateKey(rsa_key, &output);
}
}
}
示例10: MakeX509CSR
int MakeX509CSR(const char *cn, const char *keyfile, const char *csrfile)
{
InitializeOpenSSL();
RSA *rsa = RSA_generate_key(4096, RSA_F4, NULL, NULL);
BIO *bio = BIO_new(BIO_s_file());
BIO_write_filename(bio, const_cast<char *>(keyfile));
PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, NULL);
BIO_free(bio);
X509_REQ *req = X509_REQ_new();
if (!req)
return 0;
EVP_PKEY *key = EVP_PKEY_new();
EVP_PKEY_assign_RSA(key, rsa);
X509_REQ_set_version(req, 0);
X509_REQ_set_pubkey(req, key);
X509_NAME *name = X509_REQ_get_subject_name(req);
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned char *)cn, -1, -1, 0);
X509_REQ_sign(req, key, EVP_sha1());
EVP_PKEY_free(key);
bio = BIO_new(BIO_s_file());
BIO_write_filename(bio, const_cast<char *>(csrfile));
PEM_write_bio_X509_REQ(bio, req);
BIO_free(bio);
X509_REQ_free(req);
return 1;
}
示例11: key_new
/*
* Create a new key
*/
int key_new(key_t *key)
{
RSA *rsa = NULL;
EVP_PKEY *k = NULL;
/* Create key pair container */
k = EVP_PKEY_new();
if (k == NULL) {
return 0;
}
/* Generate a new RSA key */
rsa = RSA_generate_key(RSA_KEY_BITS, RSA_F4, NULL, NULL);
if (EVP_PKEY_assign_RSA(k, rsa)) {
key->key = k;
return 1;
} else {
printf("Cannot assign RSA key\n");
}
if (k)
EVP_PKEY_free(k);
return 0;
}
示例12: main
main(){
RSA *rsa;
int i,len,en_len;
unsigned char buf[2048],*p,*key_p;
rsa=RSA_generate_key(1024,RSA_F4,NULL,NULL);
p=buf;
len=i2d_RSAPublicKey(rsa,&p);
len+=i2d_RSAPrivateKey(rsa,&p);
RSA_free(rsa);
key_p = (unsigned char*)malloc(len);
memcpy(key_p,buf,len);
p = buf;
base64_encode(key_p,len,p,&en_len);
printf("%s\n",buf);
free(key_p);
}
示例13: makersacert
void makersacert(int days, char* commonname, int bits)
{
RSA *key;
EVP_PKEY *pkey;
X509 *x;
FILE *out;
key = RSA_generate_key(bits, RSA_F4, NULL, NULL);
pkey = EVP_PKEY_new();
EVP_PKEY_set1_RSA(pkey, key);
x = makeselfcert(pkey, days, commonname, EVP_sha256());
out = fopen("rsa_cert.pem", "w");
if (out == NULL)
exit(-1);
PEM_write_X509(out, x);
fclose(out);
out = fopen("rsa_cert_key.pem", "w");
if (out == NULL)
exit(-1);
PEM_write_RSAPrivateKey(out, key, NULL, NULL, 0, NULL, NULL);
fclose(out);
}
示例14: FIPS_rsa_test
/* RSA: generate keys and encrypt and decrypt known plaintext, verify result
* matches the original plaintext
*/
static int FIPS_rsa_test()
{
RSA *key;
unsigned char input_ptext[] = "etaonrishdlc";
unsigned char ctext[256];
unsigned char ptext[256];
int n;
ERR_clear_error();
key = RSA_generate_key(1024,65537,NULL,NULL);
if (!key)
return 0;
n = RSA_size(key);
n = RSA_public_encrypt(sizeof(input_ptext) - 1,input_ptext,ctext,key,RSA_PKCS1_PADDING);
if (n < 0)
return 0;
n = RSA_private_decrypt(n,ctext,ptext,key,RSA_PKCS1_PADDING);
if (n < 0)
return 0;
RSA_free(key);
if (memcmp(input_ptext,ptext,sizeof(input_ptext) - 1))
return 0;
return 1;
}
示例15: mprError
//.........这里部分代码省略.........
mprLog(4, "SSL: %s: Using ciphers %s\n", hostName, ciphers);
SSL_CTX_set_cipher_list(context, ciphers);
//
// Configure the client verification certificate locations
//
if (verifyClient) {
if (caFile == 0 && caPath == 0) {
mprError(MPR_L, MPR_LOG,
"OpenSSL: Must define CA certificates if using client verification");
SSL_CTX_free(context);
context = 0;
return MPR_ERR_BAD_STATE;
}
if (caFile || caPath) {
if ((!SSL_CTX_load_verify_locations(context, caFile, caPath)) ||
(!SSL_CTX_set_default_verify_paths(context))) {
mprError(MPR_L, MPR_LOG,
"OpenSSL: Unable to set certificate locations");
SSL_CTX_free(context);
context = 0;
return MPR_ERR_CANT_ACCESS;
}
if (caFile) {
STACK_OF(X509_NAME) *certNames;
certNames = SSL_load_client_CA_file(caFile);
if (certNames == 0) {
} else {
//
// Define the list of CA certificates to send to the client
// before they send their client certificate for validation
//
SSL_CTX_set_client_CA_list(context, certNames);
}
}
}
mprLog(4, "SSL: %s: is verifying client connections\n", hostName);
if (caFile) {
mprLog(4, "SSL: %s: Using certificates from %s\n", hostName,
caFile);
} else if (caPath) {
mprLog(4, "SSL: %s: Using certificates from directory %s\n",
hostName, caPath);
}
SSL_CTX_set_verify(context, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, verifyX509Certificate);
SSL_CTX_set_verify_depth(context, verifyDepth);
} else {
SSL_CTX_set_verify(context, SSL_VERIFY_NONE, verifyX509Certificate);
}
//
// Define callbacks
//
SSL_CTX_set_tmp_rsa_callback(context, rsaCallback);
SSL_CTX_set_tmp_dh_callback(context, dhCallback);
//
// Enable all buggy client work-arounds
//
SSL_CTX_set_options(context, SSL_OP_ALL);
#ifdef SSL_OP_NO_TICKET
SSL_CTX_set_options(context, SSL_OP_NO_TICKET);
#endif
#ifdef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
SSL_CTX_set_options(context, SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
#endif
SSL_CTX_set_mode(context, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_AUTO_RETRY);
//
// Select the required protocols
//
SSL_CTX_set_options(context, SSL_OP_NO_SSLv2);
if (!(protocols & MPR_HTTP_PROTO_SSLV3)) {
SSL_CTX_set_options(context, SSL_OP_NO_SSLv3);
mprLog(4, "SSL: %s: Disabling SSLv3\n", hostName);
}
if (!(protocols & MPR_HTTP_PROTO_TLSV1)) {
SSL_CTX_set_options(context, SSL_OP_NO_TLSv1);
mprLog(4, "SSL: %s: Disabling TLSv1\n", hostName);
}
//
// Ensure we generate a new private key for each connection
//
SSL_CTX_set_options(context, SSL_OP_SINGLE_DH_USE);
//
// Pre-generate some keys that are slow to compute
//
rsaKey512 = RSA_generate_key(512, RSA_F4, 0, 0);
rsaKey1024 = RSA_generate_key(1024, RSA_F4, 0, 0);
dhKey512 = get_dh512();
dhKey1024 = get_dh1024();
return 0;
}