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);
кто сталкивался - может есть какая-то специфика работы с ключем, после восстановления его из массива ?