Hello, I'm trying to encrypt a symmetric key using RSA with OpenSSL my code looks somet hing like (for mingw) : in the example the symmetric key is replaced by the word "hello" int main(int argc, char *argv[]) { printf("\nRSA Test\n\n"); RSA *rsa; DWORD dwKeySize = 512; unsigned char *plainkey, *cipherkey; int buffSize;
if (rsa) { rsa = RSA_generate_key(dwKeySize, RSA_F4, NULL, NULL); if (rsa) { buffSize = RSA_public_encrypt(lstrlen(plainkey), plainkey, cipherkey, rsa, RSA_PKCS1_PADDING);
printf(cipherkey); printf("\n");
if (RSA_private_decrypt(buffSize, cipherkey, plainkey, rsa, RSA_PKCS1_PADDING) != -1) printf("Key decoded to : \"%s\"\n", plainkey); else printf("Bad decryption\n"); GlobalFree(cipherkey); GlobalFree(plainkey); /* Try to regenerate same thing, but get different results */ plainkey = (char *)GlobalAlloc(GPTR, 255); cipherkey = (char *)GlobalAlloc(GPTR, 255); lstrcpy(plainkey, "hello"); buffSize = RSA_public_encrypt(lstrlen(plainkey), plainkey, cipherkey, rsa, RSA_PKCS1_PADDING);
printf(cipherkey); printf("\n");
if (RSA_private_decrypt(buffSize, cipherkey, plainkey, rsa, RSA_PKCS1_PADDING) != -1) printf("Key decoded to : \"%s\"\n", plainkey); else printf("Bad decryption\n"); GlobalFree(cipherkey); GlobalFree(plainkey);
} }
/* Free */ if (rsa) RSA_free(rsa); if (plainkey) free(plainkey); if (cipherkey) free(cipherkey); system("PAUSE"); return 0; } the problem is that I get always different ciphertexts for same RSA key and same message, normally it should be same, maybe it concerns the cipher mode, any ideas ? Thanks