qq_34422598
qq_34422598
2019-05-09 16:13

C使用openssl RSA base64对数据进行加密解密出错

20
  • c语言
  • php

对数据进行RSA base64加密的时候没有出错,但是在解码base64的时候解码出来的字符串为空。
附代码,求大佬帮忙看一下:

int base64_encode(char *in_str, int in_len, char *out_str)
{
    BIO *b64, *bio;
    BUF_MEM *bptr = NULL;
    size_t size = 0;

    if (in_str == NULL || out_str == NULL)
        return -1;

    b64 = BIO_new(BIO_f_base64());
    bio = BIO_new(BIO_s_mem());
    bio = BIO_push(b64, bio);

    BIO_write(bio, in_str, in_len);
    BIO_flush(bio);

    BIO_get_mem_ptr(bio, &bptr);
    memcpy(out_str, bptr->data, bptr->length);
    out_str[bptr->length] = '\0';
    size = bptr->length;

    BIO_free_all(bio);
    return size;
}
int base64_decode(char *in_str, int in_len, char *out_str)
    {
    BIO *b64, *bio;
    BUF_MEM *bptr = NULL;
    int counts;
    int size = 0;

    if (in_str == NULL || out_str == NULL)
        return -1;

    b64 = BIO_new(BIO_f_base64());
    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);

    bio = BIO_new_mem_buf(in_str, in_len);
    bio = BIO_push(b64, bio);

    size = BIO_read(bio, out_str, in_len);
    out_str[size] = '\0';

    BIO_free_all(bio);
    return size;
}
unsigned char *my_encrypt(char *str,char *path_key){
    char *p_en;
    RSA *p_rsa;
    FILE *file,*p_file;
    int flen,rsa_len;
    if((file=fopen(path_key,"r"))==NULL){
        perror("open key file error");
        return NULL;
    }
    if((p_rsa=PEM_read_RSA_PUBKEY(file,NULL,NULL,NULL))==NULL){//PUBLIC KEY
    // if((p_rsa=PEM_read_RSAPublicKey(file,NULL,NULL,NULL))==NULL){//RSA PUBLIC KEY
    ERR_print_errors_fp(stdout);
        return NULL;
    }
    flen=strlen(str);
    rsa_len=RSA_size(p_rsa);
    p_en=(unsigned 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);
    fclose(file);

    // p_file=fopen("./p_file.txt","w");
    // fputs(p_en, p_file);
    return p_en;
}
char *my_decrypt(unsigned char *str,char *path_key){
    char *p_de;
    RSA *p_rsa;
    FILE *file;
    int rsa_len;
    if((file=fopen(path_key,"r"))==NULL){
        perror("open key file error");
        return NULL;
    }
    if((p_rsa=PEM_read_RSAPrivateKey(file,NULL,NULL,NULL))==NULL){
        ERR_print_errors_fp(stdout);
        return NULL;
    }
    rsa_len=RSA_size(p_rsa);
    p_de=(unsigned 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);
    fclose(file);
    return p_de;
}
main{
        char source[1024]="hello";
    char *ptr_en,*ptr_de, base64_en[1024] = "", base64_de[1024] = "";
    printf("source is    :%s\n",source);

    ptr_en=my_encrypt(source,PUBLICKEY);
    printf("ptr_en strlen = %d\n", strlen(ptr_en));
    printf("after encrypt:%s\n",ptr_en);

    base64_encode(ptr_en,strlen(ptr_en),base64_en);
    printf("after encode len: %d\nmsg:%s\n",strlen(base64_en), base64_en);
    base64_decode(base64_en,strlen(base64_en),base64_de);
    printf("after decode len: %d\nmsg:%s\n",strlen(base64_de), base64_de);//len输出长度为0 数据为空

    ptr_de=my_decrypt(base64_de,OPENSSLKEY);
    printf("ptr_de strlen = %d\n", strlen(ptr_de));
    printf("after decrypt:%s\n",ptr_de);
    if(ptr_en!=NULL){
    free(ptr_en);
    }
    if(ptr_de!=NULL){
    free(ptr_de);
    }
    return 0;
        }

base64_decode解码后长度为0,数据为空。当他们单独使用的加密解密的时候没问题,合起来就会出现这个情况,求大佬看一下 ,非常感谢

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐

换一换