3,882
社区成员




#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/x509.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/applink.c>
#define PUBFILE "server.cer"
#define PRIVFILE "test.pfx"
EVP_PKEY* ReadPublicKey(const char *certfile);
EVP_PKEY* ReadPrivateKey(const char *keyfile);
int main()
{
SSLeay_add_all_algorithms();
ERR_load_crypto_strings();
ReadPublicKey(PUBFILE);
return 0;
}
EVP_PKEY* ReadPublicKey(const char *certfile)
{
FILE *fp = fopen (certfile, "r");
X509 *x509;
EVP_PKEY *pkey;
if (NULL == fp)
{
return NULL;
}
x509 = PEM_read_X509(fp, NULL, 0, NULL);
fclose (fp);
if (NULL == x509)
{
return NULL;
}
pkey = X509_extract_key(x509);
X509_free(x509);
return pkey;
}
EVP_PKEY* ReadPrivateKey(const char *keyfile)
{
FILE *fp = fopen(keyfile, "r");
EVP_PKEY *pkey;
if (NULL == fp)
{
return NULL;
}
pkey = PEM_read_PrivateKey(fp, NULL, 0, NULL);
fclose (fp);
return pkey;
}
int main(int argc, char **argv)
{
FILE *fp;
EVP_PKEY *pkey;
X509 *cert;
STACK_OF(X509) *ca = NULL;
PKCS12 *p12;
int i;
if (argc != 4) {
fprintf(stderr, "Usage: pkread p12file password opfile\n");
exit (1);
}
SSLeay_add_all_algorithms();
ERR_load_crypto_strings();
if (!(fp = fopen(argv[1], "rb"))) {
fprintf(stderr, "Error opening file %s\n", argv[1]);
exit(1);
}
p12 = d2i_PKCS12_fp(fp, NULL);
fclose (fp);
if (!p12) {
fprintf(stderr, "Error reading PKCS#12 file\n");
ERR_print_errors_fp(stderr);
exit (1);
}
if (!PKCS12_parse(p12, argv[2], &pkey, &cert, &ca)) {
fprintf(stderr, "Error parsing PKCS#12 file\n");
ERR_print_errors_fp(stderr);
exit (1);
}
PKCS12_free(p12);
if (!(fp = fopen(argv[3], "w"))) {
fprintf(stderr, "Error opening file %s\n", argv[1]);
exit(1);
}
if (pkey) {
fprintf(fp, "***Private Key***\n");
PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL);
}
if (cert) {
fprintf(fp, "***User Certificate***\n");
PEM_write_X509_AUX(fp, cert);
}
if (ca && sk_num(ca)) {
fprintf(fp, "***Other Certificates***\n");
for (i = 0; i < sk_X509_num(ca); i++)
PEM_write_X509_AUX(fp, sk_X509_value(ca, i));
}
fclose(fp);
return 0;
}
#include <string.h>
#include <memory.h>
#include <openssl/rsa.h>
#include <openssl/applink.c>
#include <openssl/pem.h>
#define PUBLICKEY "server.cer"
#define PRIVATEKEY "test.pfx"
int main()
{
CRYPTO_malloc_init();
OpenSSL_add_all_algorithms();
char* txt = "hello world!";
char outbuf[1024] = {0};
char what[1024] = {0};
FILE* public_file = fopen(PUBLICKEY, "r");
FILE* private_file = fopen(PRIVATEKEY, "r");
//加密
RSA* pubkey = PEM_read_RSAPublicKey(public_file, NULL, NULL, NULL);
int flen = RSA_size(pubkey);
int len = RSA_public_encrypt(flen, (unsigned char*)txt, (unsigned char*)outbuf, pubkey, RSA_NO_PADDING);
printf("Encrypt:%s\r\n", outbuf);
//解密
RSA* prikey = PEM_read_RSAPrivateKey(private_file, NULL, NULL, NULL);
flen = RSA_size(prikey);
len = RSA_private_decrypt(flen, (unsigned char*)outbuf, (unsigned char*)what, prikey, RSA_NO_PADDING);
printf("Decrypt:%s\r\n", what);
RSA_free(pubkey);
RSA_free(prikey);
return 0;
}