`
lobin
  • 浏览: 427290 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多
写道

 

 

OpenSSL

安装

# yum -y install openssl

# yum -y install openssl-devel

 

一些有关PEM(PEM_read)的代码

 

 

typedef void bio_info_cb(struct bio_st *, int, const char *, int, long, long);

 

 

struct crypto_ex_data_st
{
  STACK_OF(void) *sk;
  int dummy; /* gcc is screwing up this data structure :-( */
};
 

 

 

typedef struct crypto_ex_data_st CRYPTO_EX_DATA;
 

 

struct bio_st
{
  BIO_METHOD *method;
  /* bio, mode, argp, argi, argl, ret */
  long (*callback)(struct bio_st *,int,const char *,int, long,long);
  char *cb_arg; /* first argument for the callback */
 
  int init;
  int shutdown;
  int flags;  /* extra storage */
  int retry_reason;
  int num;
  void *ptr;
  struct bio_st *next_bio;  /* used by filter BIOs */
  struct bio_st *prev_bio;  /* used by filter BIOs */
  int references;
  unsigned long num_read;
  unsigned long num_write;
 
  CRYPTO_EX_DATA ex_data;
};

 

 

typedef struct bio_st BIO;

 

 

typedef struct bio_method_st

         {

         int type;

         const char *name;

         int (*bwrite)(BIO *, const char *, int);

         int (*bread)(BIO *, char *, int);

         int (*bputs)(BIO *, const char *);

         int (*bgets)(BIO *, char *, int);

         long (*ctrl)(BIO *, int, long, void *);

         int (*create)(BIO *);

         int (*destroy)(BIO *);

        long (*callback_ctrl)(BIO *, int, bio_info_cb *);

         } BIO_METHOD;

 

static int MS_CALLBACK file_gets(BIO *bp, char *buf, int size)

         {

         int ret=0;

 

         buf[0]='\0';

         if (bp->flags&BIO_FLAGS_UPLINK)

                   {

                   if (!UP_fgets(buf,size,bp->ptr))

                            goto err;

                   }

         else

                   {

                   if (!fgets(buf,size,(FILE *)bp->ptr))

                            goto err;

                   }

         if (buf[0] != '\0')

                   ret=strlen(buf);

         err:

         return(ret);

         }

 

 

static BIO_METHOD methods_filep=

         {

         BIO_TYPE_FILE,

         "FILE pointer",

         file_write,

         file_read,

         file_puts,

         file_gets,

         file_ctrl,

         file_new,

         file_free,

         NULL,

         };

 

BIO_METHOD *BIO_s_file(void)

         {

         return(&methods_filep);

         }

 

BIO *BIO_new(BIO_METHOD *method)

         {

         BIO *ret=NULL;

 

         ret=(BIO *)OPENSSL_malloc(sizeof(BIO));

         if (ret == NULL)

                   {

                   BIOerr(BIO_F_BIO_NEW,ERR_R_MALLOC_FAILURE);

                   return(NULL);

                   }

         if (!BIO_set(ret,method))

                   {

                   OPENSSL_free(ret);

                   ret=NULL;

                   }

         return(ret);

         }

 

long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)

         {

         long ret;

         long (*cb)(BIO *,int,const char *,int,long,long);

 

         if (b == NULL) return(0);

 

         if ((b->method == NULL) || (b->method->ctrl == NULL))

                   {

                   BIOerr(BIO_F_BIO_CTRL,BIO_R_UNSUPPORTED_METHOD);

                   return(-2);

                   }

 

         cb=b->callback;

 

         if ((cb != NULL) &&

                   ((ret=cb(b,BIO_CB_CTRL,parg,cmd,larg,1L)) <= 0))

                   return(ret);

 

         ret=b->method->ctrl(b,cmd,larg,parg);

 

         if (cb != NULL)

                   ret=cb(b,BIO_CB_CTRL|BIO_CB_RETURN,parg,cmd,

                            larg,ret);

         return(ret);

         }

 

 

#define BIO_set_fp(b,fp,c)      BIO_ctrl(b,BIO_C_SET_FILE_PTR,c,(char *)fp)

 

int BIO_gets(BIO *b, char *in, int inl)

         {

         int i;

         long (*cb)(BIO *,int,const char *,int,long,long);

 

         if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL))

                   {

                   BIOerr(BIO_F_BIO_GETS,BIO_R_UNSUPPORTED_METHOD);

                   return(-2);

                   }

 

         cb=b->callback;

 

         if ((cb != NULL) &&

                   ((i=(int)cb(b,BIO_CB_GETS,in,inl,0L,1L)) <= 0))

                            return(i);

 

         if (!b->init)

                   {

                   BIOerr(BIO_F_BIO_GETS,BIO_R_UNINITIALIZED);

                   return(-2);

                   }

 

         i=b->method->bgets(b,in,inl);

 

         if (cb != NULL)

                   i=(int)cb(b,BIO_CB_GETS|BIO_CB_RETURN,in,inl,

                            0L,(long)i);

         return(i);

         }

 

 

int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,

              long *len)

         {

         EVP_ENCODE_CTX ctx;

         int end=0,i,k,bl=0,hl=0,nohead=0;

         char buf[256];

         BUF_MEM *nameB;

         BUF_MEM *headerB;

         BUF_MEM *dataB,*tmpB;

        

         nameB=BUF_MEM_new();

         headerB=BUF_MEM_new();

         dataB=BUF_MEM_new();

         if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL))

                   {

                   BUF_MEM_free(nameB);

                   BUF_MEM_free(headerB);

                   BUF_MEM_free(dataB);

                   PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);

                   return(0);

                   }

 

         buf[254]='\0';

         for (;;)

                   {

                   i=BIO_gets(bp,buf,254);

 

                   if (i <= 0)

                            {

                            PEMerr(PEM_F_PEM_READ_BIO,PEM_R_NO_START_LINE);

                            goto err;

                            }

 

                   while ((i >= 0) && (buf[i] <= ' ')) i--;

                   buf[++i]='\n'; buf[++i]='\0';

 

                   if (strncmp(buf,"-----BEGIN ",11) == 0)

                            {

                            i=strlen(&(buf[11]));

 

                            if (strncmp(&(buf[11+i-6]),"-----\n",6) != 0)

                                     continue;

                            if (!BUF_MEM_grow(nameB,i+9))

                                     {

                                     PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);

                                     goto err;

                                     }

                            memcpy(nameB->data,&(buf[11]),i-6);

                            nameB->data[i-6]='\0';

                            break;

                            }

                   }

         hl=0;

         if (!BUF_MEM_grow(headerB,256))

                   { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; }

         headerB->data[0]='\0';

         for (;;)

                   {

                   i=BIO_gets(bp,buf,254);

                   if (i <= 0) break;

 

                   while ((i >= 0) && (buf[i] <= ' ')) i--;

                   buf[++i]='\n'; buf[++i]='\0';

 

                   if (buf[0] == '\n') break;

                   if (!BUF_MEM_grow(headerB,hl+i+9))

                            { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; }

                   if (strncmp(buf,"-----END ",9) == 0)

                            {

                            nohead=1;

                            break;

                            }

                   memcpy(&(headerB->data[hl]),buf,i);

                   headerB->data[hl+i]='\0';

                   hl+=i;

                   }

 

         bl=0;

         if (!BUF_MEM_grow(dataB,1024))

                   { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; }

         dataB->data[0]='\0';

         if (!nohead)

                   {

                   for (;;)

                            {

                            i=BIO_gets(bp,buf,254);

                            if (i <= 0) break;

 

                            while ((i >= 0) && (buf[i] <= ' ')) i--;

                            buf[++i]='\n'; buf[++i]='\0';

 

                            if (i != 65) end=1;

                            if (strncmp(buf,"-----END ",9) == 0)

                                     break;

                            if (i > 65) break;

                            if (!BUF_MEM_grow_clean(dataB,i+bl+9))

                                     {

                                     PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);

                                     goto err;

                                     }

                            memcpy(&(dataB->data[bl]),buf,i);

                            dataB->data[bl+i]='\0';

                            bl+=i;

                            if (end)

                                     {

                                     buf[0]='\0';

                                     i=BIO_gets(bp,buf,254);

                                     if (i <= 0) break;

 

                                     while ((i >= 0) && (buf[i] <= ' ')) i--;

                                     buf[++i]='\n'; buf[++i]='\0';

 

                                     break;

                                     }

                            }

                   }

         else

                   {

                   tmpB=headerB;

                   headerB=dataB;

                   dataB=tmpB;

                   bl=hl;

                   }

         i=strlen(nameB->data);

         if (     (strncmp(buf,"-----END ",9) != 0) ||

                   (strncmp(nameB->data,&(buf[9]),i) != 0) ||

                   (strncmp(&(buf[9+i]),"-----\n",6) != 0))

                   {

                   PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_END_LINE);

                   goto err;

                   }

 

         EVP_DecodeInit(&ctx);

         i=EVP_DecodeUpdate(&ctx,

                   (unsigned char *)dataB->data,&bl,

                   (unsigned char *)dataB->data,bl);

         if (i < 0)

                   {

                   PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_BASE64_DECODE);

                   goto err;

                   }

         i=EVP_DecodeFinal(&ctx,(unsigned char *)&(dataB->data[bl]),&k);

         if (i < 0)

                   {

                   PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_BASE64_DECODE);

                   goto err;

                   }

         bl+=k;

 

         if (bl == 0) goto err;

         *name=nameB->data;

         *header=headerB->data;

         *data=(unsigned char *)dataB->data;

         *len=bl;

         OPENSSL_free(nameB);

         OPENSSL_free(headerB);

         OPENSSL_free(dataB);

         return(1);

err:

         BUF_MEM_free(nameB);

         BUF_MEM_free(headerB);

         BUF_MEM_free(dataB);

         return(0);

         }

 

 

int PEM_read(FILE *fp, char **name, char **header, unsigned char **data,

              long *len)

        {

        BIO *b;

        int ret;

 

        if ((b=BIO_new(BIO_s_file())) == NULL)

                   {

                   PEMerr(PEM_F_PEM_READ,ERR_R_BUF_LIB);

                return(0);

                   }

        BIO_set_fp(b,fp,BIO_NOCLOSE);

        ret=PEM_read_bio(b, name, header, data,len);

        BIO_free(b);

        return(ret);

        }

 

RSA加密和解密

 

 

// test.cpp
// 
// @author ada
// @version 1.0  2015-01-10

#include "stdafx.h"

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<openssl/rsa.h>
#include<openssl/pem.h>
#include<openssl/err.h>

extern "C"
{
#include "openssl/applink.c"
}

#pragma comment(lib, "libeay32.lib")

#define FILE_KEY "test.key"
#define FILE_PUBLIC_KEY "test_pub.key"
#define BUFFSIZE 1024

RSA* RSA_key();
void RSA_key_write_RSAPrivateKey(const char* fn, RSA* rsa);
RSA* RSA_key_read_RSAPublicKey(RSA* rsa);
void RSA_public_key_write_RSAPublicKey(const char* fn, RSA* rsa);

char * encrypt_with_key(char *str, char *key);

char * encrypt_with_fn(char *str, char *fps);

char * encrypt_with_fp(char *str, FILE* fp);

char * decrypt_with_key(char *s, char *k);

char * decrypt_with_fn(char *str,char *fps);

char * decrypt_with_fp(char *str, FILE* fp);

 
int _tmain(int argc, _TCHAR* argv[])
{
    RSA *rsa = NULL, *rsa_pk = NULL;
    char *source="this is plain text!";
    char *ptr_en, *ptr_de;

    printf("plain text: %s\n", source);


    // RSA KEY TO GENERATE
    rsa = RSA_key();
    // WRITE RSA KEY TO FILE
    RSA_key_write_RSAPrivateKey("www.yhd.com-g.key", rsa);

    // READ PUBLIC KEY FROM RSA KEY
    rsa_pk = RSA_key_read_RSAPublicKey(rsa);

    // WRITE PUBLIC KEY TO FILE
    RSA_public_key_write_RSAPublicKey("www.yhd.com-g.pub.key", rsa_pk);

    char* key = "-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDAiZZSPEZHVnlnx6pRKEZdBP99\nGKaMikoABg2dLsMUk/zOxXLTpduuZNLxBicXLR9z7CCjUPfghFcclrWR5+hyZoAV\n0xOCd6YF9I9I7OL5Fj/GiTTjmuEm9uBxeQz6u2zl843DnDw8vwfZY+FQBDBRYic9\n+F1xPYRCQG6Hd9x9twIDAQAB\n-----END PUBLIC KEY-----\n";
    ptr_en = encrypt_with_key(source, key);
    printf("encrypt text: %s\n", ptr_en);

    ptr_en = encrypt_with_fn(source, FILE_PUBLIC_KEY);
    printf("encrypt text: %s\n", ptr_en);

    FILE *file;
    if((file = fopen(FILE_PUBLIC_KEY, "r")) != NULL) {
        ptr_en = encrypt_with_fp(source, file);
        printf("encrypt text: %s\n", ptr_en);
    }

    char* pk = "-----BEGIN RSA PRIVATE KEY-----\nMIICXgIBAAKBgQDAiZZSPEZHVnlnx6pRKEZdBP99GKaMikoABg2dLsMUk/zOxXLT\npduuZNLxBicXLR9z7CCjUPfghFcclrWR5+hyZoAV0xOCd6YF9I9I7OL5Fj/GiTTj\nmuEm9uBxeQz6u2zl843DnDw8vwfZY+FQBDBRYic9+F1xPYRCQG6Hd9x9twIDAQAB\nAoGBAL9Cbc73DSEkZ6mOEERaa7tPkbRk3l1RqjznxnJT13oDn+iO/4f7hIRyp5DB\n+ebt7gark6hocDPZgudlVqy/4Ii6x6cYtW0hObcLig0+UrQJJG8huLj4Fp0mzcIf\nxAh90Bs0GeVEUp3K0FyMUL0+MT+VBnTHOnmizTIqCIJK3Z+RAkEA5AUqcyjneRsP\nRHq9k1IKK9RjiHKbV1fyF6TZGN+cE9pF8Zlbnv/3PFjBL8j+m6qSAjVD7ftaLprO\na5cQX/aW/QJBANgpy/hQcc4P7dXRbMddILsGkc/1sr078KQkgk8+gXK2aamdi1z8\nt6bCGQ7d6te1wPxnLvBgv+vpSJRJWdbt18MCQQCLG5OwOe4tdhK0ZmUtcUl/P82R\nOsoGWUL6pblEcXbYrbsqD7NTpzj/+e2x519dPIxjCHfuwf9cm1j/TmPRB7FJAkAR\n4+OtuXqxwz+74olONrbuw6xtubgBUWSpMRxX0qSECfTUxOUz3PaWJppMULGPCMvg\nF+oamudHbxdCro9w6l5hAkEAmzgmluE0SmFMvJ1jujflVGvIUKtwqQvyNg/qcCSj\nqhu+VTIyp//q/fwW5+U2JoVkx+IaIqzS8g0aDQHS0WPd+Q==\n-----END RSA PRIVATE KEY-----";
    ptr_de = decrypt_with_key(ptr_en, pk);
    printf("decrypt text(plain text): %s\n", ptr_de);

    ptr_de = decrypt_with_fn(ptr_en, FILE_KEY);
    printf("decrypt text(plain text): %s\n", ptr_de);

    if((file = fopen(FILE_KEY, "r")) != NULL) {
        ptr_de = decrypt_with_fp(ptr_en, file);
        printf("decrypt text(plain text): %s\n", ptr_de);
    }

    if(ptr_en != NULL) {
        free(ptr_en);
    }
    if(ptr_de != NULL) {
        free(ptr_de);
    }
    return 0;
}

void RSA_key_write_RSAPrivateKey(const char* fn, RSA* rsa) {
    FILE *fp = NULL;
    fp = fopen(fn, "w");
    if (fp == NULL) {
        fprintf(stderr,"%s open error", fn);
    }
    printf("file %s opened...\n", fn);
    PEM_write_RSAPrivateKey(fp, rsa, NULL, NULL, 512, NULL, NULL);
    fclose(fp);
}

RSA* RSA_key_read_RSAPublicKey(RSA* rsa) {
    
    BIO *bio = NULL;
    RSA* pk = NULL;

    bio = BIO_new(BIO_s_mem());
    PEM_write_bio_RSAPublicKey(bio, rsa);
    if((pk = PEM_read_bio_RSAPublicKey(bio, NULL, NULL, NULL))==NULL) {
        ERR_print_errors_fp(stdout);
        return NULL;
    }
    return pk;
}

void RSA_public_key_write_RSAPublicKey(const char* fn, RSA* rsa) {
    FILE* fp = NULL;
    fp = fopen(fn, "w");
    if (fp == NULL) {
        fprintf(stderr,"%s open error", fn);
        return;
    }
    printf("file %s opened...\n", fn);
    PEM_write_RSAPublicKey(fp, rsa);
    fclose(fp);
}

RSA* RSA_key() {
    RSA* rsa = NULL; // RSA_new();
    // BIGNUM * e = BN_new();
    unsigned long ue = RSA_3;

    printf("RSA_generate_key\n");
    rsa = RSA_generate_key(1024, ue, NULL, NULL);
    printf("RSA_generate_key return\n");

    //int rt = RSA_generate_key_ex(rsa, 1024, e, NULL);
    //unsigned long error = ERR_get_error();
    //printf("error: %ld\n", error);
    return rsa;
}

char *encrypt_with_key(char *str, char *key) {
    // read public key from the specific string -- with char * from memory
#undef OPENSSL_NO_BIO 

    BIO *bio = NULL;
    RSA *p_rsa = NULL;
    int flen, rsa_len;
    char *p_en = NULL;

    if ((bio = BIO_new_mem_buf(key, -1)) == NULL) // read public key from string
    {
        perror("BIO_new_mem_buf failed!");
    }
    if((p_rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL))==NULL) {
        ERR_print_errors_fp(stdout);
        return NULL;
    }







    flen = strlen(str);
    rsa_len = RSA_size(p_rsa);
    p_en = (char *) malloc(rsa_len + 1);
    memset(p_en, 0, rsa_len + 1);
    if(RSA_public_encrypt(rsa_len, (unsigned char *) str, (unsigned char *) p_en, p_rsa, RSA_NO_PADDING) < 0) {
        return NULL;
    }
    BIO_free(bio);
    RSA_free(p_rsa);
    return p_en;
}

// encript string with the specific key
// #str  the string to be encript
// #fps  the file name of key
char *encrypt_with_fn(char *str, char *fps) {
    char *p_en = NULL;
    FILE *file;
    if((file = fopen(fps, "r")) == NULL) {
        perror("open key file error");
        return NULL;    
    }
    p_en = encrypt_with_fp(str, file);
    fclose(file);
    return p_en;
}

char *encrypt_with_fp(char *str, FILE* fp) {
    RSA *p_rsa = NULL;
    int flen, rsa_len;
    char *p_en = NULL;

    // PEM_read_RSA_PUBKEY: 
    // DECLARE_PEM_rw(RSA_PUBKEY, RSA)
    // 
    // #define DECLARE_PEM_rw(name, type) DECLARE_PEM_read(name, type) DECLARE_PEM_write(name, type)
    // 
    // #define DECLARE_PEM_read(name, type) DECLARE_PEM_read_bio(name, type) DECLARE_PEM_read_fp(name, type)
    // #define DECLARE_PEM_write(name, type) DECLARE_PEM_write_bio(name, type) DECLARE_PEM_write_fp(name, type)
    // 
    // // #define DECLARE_PEM_read_bio(name, type) type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u);
    // #define DECLARE_PEM_read_bio(name, type) /**/
    // // #define DECLARE_PEM_read_fp(name, type) /**/
    // #define DECLARE_PEM_read_fp(name, type) type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u);
    // 
    // // #define DECLARE_PEM_write_bio(name, type) int PEM_write_bio_##name(BIO *bp, type *x);
    // #define DECLARE_PEM_write_bio(name, type) /**/
    // // #define DECLARE_PEM_write_fp(name, type) int PEM_write_##name(FILE *fp, type *x);
    // #define DECLARE_PEM_write_fp(name, type) /**/
    //
    //
    // DECLARE_PEM_rw(RSA_PUBKEY, RSA)
    // 
    // DECLARE_PEM_read(RSA_PUBKEY, RSA) DECLARE_PEM_write(RSA_PUBKEY, RSA)
    // 
    // DECLARE_PEM_read_bio(RSA_PUBKEY, RSA) DECLARE_PEM_read_fp(RSA_PUBKEY, RSA) DECLARE_PEM_write_bio(RSA_PUBKEY, RSA) DECLARE_PEM_write_fp(RSA_PUBKEY, RSA)
    // 
    // /**/
    // RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x, pem_password_cb *cb, void *u);
    // /**/
    // /**/
    //
    //
    // RSA *PEM_read_bio_RSA_PUBKEY(BIO *bp, RSA **x, pem_password_cb *cb, void *u);
    // /**/
    // /**/
    // /**/

    // read public key from the specific string -- with char * from memory
    if((p_rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
    //if((p_rsa=PEM_read_RSAPublicKey(file,NULL,NULL,NULL))==NULL){
        ERR_print_errors_fp(stdout);
        return NULL;
    }

    flen = strlen(str);
    rsa_len = RSA_size(p_rsa);
    p_en = (char *) malloc(rsa_len + 1);
    memset(p_en, 0, rsa_len + 1);
    if(RSA_public_encrypt(rsa_len, (unsigned char *) str, (unsigned char *) p_en, p_rsa, RSA_NO_PADDING) < 0) {
        return NULL;
    }
    RSA_free(p_rsa);
    return p_en;
}

char * decrypt_with_key(char *s, char *k) {
    char *p_de = NULL;
    RSA *p_rsa = NULL;
    int rsa_len;
    // read public key from the specific string -- with char * from memory
#undef OPENSSL_NO_BIO 

    BIO *bio;
    if ((bio = BIO_new_mem_buf(k, -1)) == NULL) // read key from string
    {     
        perror("BIO_new_mem_buf failed!");
        return NULL;
    }
    if((p_rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)) == NULL) {
        ERR_print_errors_fp(stdout);
        return NULL;
    }


    rsa_len=RSA_size(p_rsa);
    p_de = (char *) malloc(rsa_len + 1);
    memset(p_de,0,rsa_len+1);
    if(RSA_private_decrypt(rsa_len, (unsigned char *) s, (unsigned char*) p_de, p_rsa, RSA_NO_PADDING) < 0) {
        return NULL;
    }
    RSA_free(p_rsa);
    return p_de;
}

// decript string with the specific key
// #str  the string to be decript
// #fps  the file name of key
char * decrypt_with_fn(char *str, char *fps) {
    char *p_de;
    FILE *file;
    if((file=fopen(fps,"r"))==NULL){
        perror("error to open key from file");
        return NULL;
    }

    p_de = decrypt_with_fp(str, file);
    fclose(file);
    return p_de;
}

// decript string with the specific key
// #str  the string to be decript
// #fps  the file name of key
char * decrypt_with_fp(char *str, FILE* fp) {
    RSA *p_rsa = NULL;
    char *p_de = NULL;
    int rsa_len;

    if((p_rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL)) == NULL){
        ERR_print_errors_fp(stdout);
        return NULL;
    }

    rsa_len = RSA_size(p_rsa);
    p_de = (char *) malloc(rsa_len + 1);
    memset(p_de, 0, rsa_len + 1);
    if(RSA_private_decrypt(rsa_len, (unsigned char *) str, (unsigned char *) p_de, p_rsa, RSA_NO_PADDING) < 0) {
        return NULL;
    }
    RSA_free(p_rsa);
    return p_de;
}

 

TLS

 

0
1
分享到:
评论

相关推荐

    openssl和openssl-devel离线rpm安装包.zip_OPENSSL 库_openssl_openssl-deve

    `openssl-devel` 是OpenSSL的开发库,包含了头文件和静态链接库,用于编译和链接到OpenSSL功能的应用程序。开发人员在编写需要使用OpenSSL的软件时,需要安装这个包,以便在编译时可以正确地引用OpenSSL的函数和结构...

    OpenSSL-for-iPhone-master.zip_OpenSSL-for-iPhone_openssl_openssl

    OpenSSL 是一个强大的安全套接层(SSL)和传输层安全(TLS)协议实现库,同时也包含各种加密算法、证书处理和其他安全功能。在iOS开发中,如果你的应用需要进行安全的网络通信,如HTTPS,或者需要对数据进行加密解密...

    openssl-1.0.2u + openssl-1.0.2k

    标题中的"openssl-1.0.2u + openssl-1.0.2k"指的是OpenSSL库的两个不同版本,1.0.2u和1.0.2k。OpenSSL是一个开源项目,提供安全套接层(SSL)和传输层安全(TLS)协议的实现,用于加密通信,确保网络数据传输的安全...

    openssl-1.0.0.tar.gz~~openssl-3.3.1.tar.gz.zip

    不同版本的OpenSSL压缩包: openssl-1.0.0.tar.gz openssl-1.0.1.tar.gz openssl-1.0.2.tar.gz openssl-1.1.0.tar.gz openssl-1.1.1.tar.gz openssl-3.0.14.tar.gz openssl-3.1.6.tar.gz openssl-3.2.2.tar.gz ...

    openssl-0.9.8k_WIN32_openssl-0.9.8k_WIN32_OpenSSL-Win32_openssl_

    这个压缩包“openssl-0.9.8k_WIN32_openssl-0.9.8k_WIN32_OpenSSL-Win32_openssl_”显然包含了适用于 Windows 32位系统的 OpenSSL 版本0.9.8k。这个版本可能已经过时,因为最新的稳定版本通常会提供更多的安全修复和...

    openssl-win32工具 win32OpenSSL

    用openSSL工具生成密钥使用说明: 安装后进入OpenSSL工具,输入如下命令: • Linux用户(以Ubuntu为例): $ openssl 进入OpenSSL程序 OpenSSL&gt; genrsa -out rsa_private_key.pem 1024 生成私钥 OpenSSL&gt; pkcs8 -...

    Openssl.rar_C++_VC6_openssl vc6_openssl 静态库

    基于VC6的openssl库, (1) lib\libeay32.lib及lib\ssleay32.lib是已经编译好的VC6可用的静态库, 只要把这两个文件拷到VC6的lib目录中即可。 (2) include\openssl是openssl相关的头文件, 只要把openssl这个文件夹...

    centosopenssl-1.1.1u+openssl-1.1.1vopenssl1.1.1u+openssl1.1.1v

    centos7 最新版本的openssl rpm安装包 openssl-1.1.1u-1.el7.x86_64.rpm openssl-debuginfo-1.1.1u-1.el7.x86_64.rpm openssl-devel-1.1.1u-1.el7.x86_64.rpm openssl-1.1.1v-1.el7.x86_64.rpm openssl-debuginfo-...

    openssl win x64

    **OpenSSL 在 Windows x64 平台上的编译与应用** OpenSSL 是一个开源的加密库,包含了大量的加密算法,如对称加密、非对称加密、哈希函数等,广泛应用于网络通信的安全保障,如 SSL/TLS 协议。在 Windows x64 环境...

    OpenSSL3.2动态库

    OpenSSL 是一个强大的安全套接层(SSL)和传输层安全(TLS)协议实现库,同时也包含各种加密算法、证书工具和其他相关功能。OpenSSL 3.2 是该库的一个新版本,它提供了最新的安全更新、性能改进以及对现有特性的优化...

    openssl-OpenSSL_1_1_1d.tar.gz

    "openssl-OpenSSL_1_1_1d.tar.gz" 是一个包含 OpenSSL 1.1.1d 版本的压缩文件,它遵循了通常的命名规范,其中 "openssl" 表示这是一个与 OpenSSL 相关的软件,"OpenSSL_1_1_1d" 指的是 OpenSSL 的特定版本号,即 ...

    安装openssl和openssl-develd 的依赖包

    这里我们关注的是“openssl”和“openssl-devel”这两个关键组件,它们是实现安全套接字层(SSL)和传输层安全(TLS)协议的基础。SSL/TLS协议用于加密网络通信,确保数据在传输过程中不被窃取或篡改。 **openssl**...

    openssl已编译,3.0.3

    OpenSSL 是一个强大的安全套接层 (SSL) 和传输层安全 (TLS) 库,用于加密通信,确保网络数据传输的安全性。它包含了各种加密算法、常用的密钥和证书操作,以及SSL/TLS协议实现。标题提到的是 "openssl已编译,3.0.3...

    Hmailserver配置openSSL生成密钥公钥Win64OpenSSL+DKIM

    Hmailserver配置openSSL生成密钥公钥Win64OpenSSL+DKIM,文件内容有Win64OpenSSL_Light-1_1_1w,GenDKIM.bat,GenDKIMtxt.exe,makeHMScert.bat,先安装Win64OpenSSL_Light-1_1_1w,然后进入安装目录的bin目录下,将...

    openssl-windows64

    OpenSSL 是一个强大的安全套接层 (SSL) 和 Transport Layer Security (TLS) 库,用于在互联网上实现安全通信。这个“openssl-windows64”压缩包显然是为64位Windows操作系统设计的OpenSSL版本,主要用途是进行数据...

    OpenSSL学习资料(比较齐全的OpenssL相关资料汇总)

    OpenSSL 是一个强大的安全套接层(SSL/TLS)库,同时也包含各种加密算法、证书操作和其他相关工具。这个压缩包文件提供了丰富的OpenSSL学习资料,涵盖了从基础概念到实践应用的不同方面。以下是对这些知识点的详细...

    已经编译的openssl,openssl-0.9.8q.zip版本

    **OpenSSL 概述** OpenSSL 是一个强大的安全套接字层密码库,包含各种主要的密码算法、常用的密钥和证书封装管理功能以及 SSL 协议,并提供丰富的应用程序供测试或其他目的使用。这个库被广泛应用于网络服务器,...

    OpenSSL3.0.0静态编译

    OpenSSL 3.0.0 是该库的一个重要版本,引入了多项改进和新特性。在Windows环境下,使用Microsoft Visual Studio 2019 (msvc2019) 进行OpenSSL的静态编译,可以确保编译出的库文件与目标应用程序紧密集成,减少运行时...

    openssl1.1.0.rar

    OpenSSL 是一个强大的安全套接层 (SSL) 和 Transport Layer Security (TLS) 库,它包含各种加密算法、常用的密钥和证书管理功能,以及 SSL/TLS 协议的实现。OpenSSL 1.1.0 版本是该项目的一个重要里程碑,引入了许多...

    openssl编程帮助文档

    ### OpenSSL编程帮助文档知识点梳理 #### 第一章:基础知识 **1.1 对称算法** - **概念**:对称算法是一种加密技术,在该技术中,加密和解密使用相同的密钥。 - **常见对称算法**:包括DES、3DES、AES、RC4等。 -...

Global site tag (gtag.js) - Google Analytics