RSA *
DER_decode_RSA_public (unsigned char *buf, long len)
{
RSA *rsa;rsa = d2i_RSAPublicKey (NULL, &buf, len);
return rsa;
}
unsigned char *
DER_encode_RSA_public (RSA * rsa, int *len)
{
unsigned char *buf, *next;*len = i2d_RSAPublicKey (rsa, NULL);
buf = next = (unsigned char *) malloc (*len);
i2d_RSAPublicKey (rsa, &next);return buf;
}main()
{
RSA *rsa,*pub_rsa,*priv_rsa;
unsigned char *priv_key;
unsigned char *pub_key;
int len, len1;
unsigned char *encrypted;
unsigned char *mess = "test message!";rsa = RSA_generate_key(1024, 3, NULL, NULL);
printf("%d\n",RSA_check_key(rsa));
len1 = strlen(mess)*sizeof(char);
encrypted = (unsigned char *) malloc ((size_t) RSA_size(rsa));
RSA_public_encrypt(len1, mess, encrypted, rsa, RSA_PKCS1_OAEP_PADDING);
printf("%s\n",encrypted);
free(encrypted);
RSA_free(rsa);
}
почему этот код ^^^ работает, а
этот нет:RSA *rsa,*pub_rsa,*priv_rsa;
unsigned char *priv_key;
unsigned char *pub_key;
int len, len1;
unsigned char *encrypted;
unsigned char *mess = "test message!";rsa = RSA_generate_key(1024, 3, NULL, NULL);
pub_key = DER_encode_RSA_public(rsa,&len);
printf("%s\n",pub_key);
pub_rsa = DER_decode_RSA_public(pub_key,&len);
//printf("%d\n",RSA_check_key(pub_rsa));
len1 = strlen(mess)*sizeof(char);
encrypted = (unsigned char *) malloc ((size_t) RSA_size(pub_rsa));
RSA_public_encrypt(len1, mess, encrypted, pub_rsa, RSA_PKCS1_OAEP_PADDING);
printf("%s\n",encrypted);
free(encrypted);
RSA_free(pub_rsa);
RSA_free(rsa);кто сталкивался - может есть какая-то специфика работы с ключем, после восстановления его из массива ?
всё - сам разобрался, вот рабочий код - вдруг кому еще придется помучиться...int main()
{
RSA *rsa,*pub_rsa,*priv_rsa;
unsigned char *priv_key;
unsigned char *pub_key;
int len, len1, size;
unsigned char *encrypted;
unsigned char *mess = "test message!";
unsigned char *p, *pp;
unsigned char *decrypt_mess;
FILE *Keyfd = NULL;
FILE *Pubfd = NULL;rsa = RSA_generate_key(1024, RSA_3, NULL, NULL);
size = i2d_RSAPublicKey (rsa, NULL);pub_key = p = (unsigned char *) malloc(size * sizeof(unsigned char));
i2d_RSAPublicKey (rsa, &p);
pub_rsa = d2i_RSAPublicKey(NULL,&pub_key,size);
PEM_write_RSA_PUBKEY(stdout,pub_rsa);size = i2d_RSAPrivateKey(rsa, NULL);
priv_key = pp = (unsigned char *) malloc(size * sizeof(unsigned char));
i2d_RSAPrivateKey (rsa, &pp);
priv_rsa = d2i_RSAPrivateKey(NULL,&priv_key,size);
if( priv_rsa==NULL ) { fprintf(stderr,"priv key error!\n"); return 0; }
PEM_write_RSAPrivateKey(stdout,priv_rsa,NULL, NULL, 0, NULL, NULL);
len1 = (strlen(mess)*sizeof(unsigned char)+1);
encrypted = (unsigned char *) malloc ((size_t) RSA_size(pub_rsa));
len= RSA_public_encrypt(len1, mess, encrypted, pub_rsa, RSA_PKCS1_PADDING);
printf("encrypted: %s len: %d\n",encrypted, len);
if(!(decrypt_mess = (unsigned char *) malloc ((size_t) RSA_size(priv_rsa)))) fprintf(stderr,"can't allocate memory for encrypted text!\n");
printf("decrypting!\n");
len= RSA_private_decrypt(RSA_size(priv_rsa), encrypted, decrypt_mess, priv_rsa, RSA_PKCS1_PADDING);
printf("decrypted: %s len:%d\n",decrypt_mess,len);free(encrypted);
free(decrypt_mess);
RSA_free(pub_rsa);
RSA_free(priv_rsa);
RSA_free(rsa);
}