RSA_private_decrypt 失败

发布于 2025-01-05 11:57:24 字数 4479 浏览 0 评论 0原文

我用的是openssl。首先,我创建了私钥/公钥,然后加密一个字符串并将结果保存在文件中。当我尝试解密该文件时,我的程序失败。更重要的是,每次加密的文件都不一样(我使用md5sum检查)。我错过了什么要处理的事情?

   /*
    gcc -lssl queation.c -o test_ssl
    #openssl genrsa -out test_private.key 1024
    #openssl rsa -in test_private.key -pubout -out test_public.key

*/


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

#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/netdevice.h>
#include <linux/sockios.h>
#include <linux/if.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/if_arp.h>
#include <netinet/if_ether.h>
#include <netinet/ether.h>

#include <fcntl.h>
#include <sys/socket.h>

#define OPENSSLKEY "test_private.key"
#define PUBLICKEY "test_public.key"
#define BUFFSIZE 1024
#define SIZE 1024

#define LIC_FILE  "lic.rn"
#define PRTFL printf("fun = %s, line = %d\n", __FUNCTION__,__LINE__)

static char *ptr_en;
static char *p_en;
static RSA  *p_rsa_public;
static FILE  *fp_public;
static int flen_public, rsa_public_len;


static char *ptr_de;
static char *p_de;
static RSA  *p_rsa_private;
static FILE  *fp_private;
static int flen_private, rsa_private_len;

void usage( unsigned char * prog_name)
{
    printf("usage:  %s\n",
        prog_name);
    exit(1);
}

int main(int argc , char ** argv)
{
    int i, ret , len;
    unsigned char buf_plain[32];
    unsigned char *buf_en;
    unsigned char *raw_buffer;

    FILE * pf_tmp;

    if( argc != 1)
        {
        usage(argv[0]);
        }

    snprintf(buf_plain,sizeof(buf_plain),"this is a test line.\n");

    if((fp_public=fopen(PUBLICKEY,"r"))==NULL)
        {
        perror("open public key file error");
        ret = -1;
        goto error;
        }   

    if((p_rsa_public = PEM_read_RSA_PUBKEY(fp_public,NULL,NULL,NULL))==NULL)
        {
        ERR_print_errors_fp(stdout);
        ret = -1;
        goto error;
        }

    rsa_public_len=RSA_size(p_rsa_public);
    p_en=(unsigned char *)malloc(rsa_public_len+1);
    memset(p_en,0,rsa_public_len+1);
    //printf("%s(%d)p_en = %p,rsa_public_len = %d\n", __FUNCTION__,__LINE__,p_en,rsa_public_len);

    len = RSA_public_encrypt(rsa_public_len,buf_plain,p_en,p_rsa_public,RSA_NO_PADDING);
    if (len !=rsa_public_len)
        {
        fprintf(stderr,"Error: len =%d,  rsa_public_len = %d,ciphertext should match length of key\n", len,rsa_public_len);
        ret = -1;
        goto error;
        }


    pf_tmp = fopen(LIC_FILE,"w");
    if( NULL == pf_tmp )
        {
        printf("open %s failed\n",LIC_FILE);
        ret = -1;
        goto error;
        }

    fwrite(p_en,1,128,pf_tmp);
    fclose(pf_tmp);

    if((fp_private=fopen(OPENSSLKEY,"r"))==NULL)
        {
        perror("open private key file error");
        ret = -1;
        goto error;
        }   

    if((p_rsa_private=PEM_read_RSAPrivateKey(fp_private,NULL,NULL,NULL))==NULL)
        {
        ERR_print_errors_fp(stdout);
        ret = -1;
        goto error;
        }

    rsa_private_len = RSA_size(p_rsa_private);

    pf_tmp = fopen(LIC_FILE,"r");
    if( NULL == pf_tmp )
        {
        printf("open %s failed\n",LIC_FILE);
        ret = -1;
        goto error2;
        }

    raw_buffer = calloc(rsa_private_len,sizeof(char));
    if( NULL == raw_buffer )
        {
        ret = -1;
        goto error;
        }

     len = fread(raw_buffer,  sizeof(char),sizeof(raw_buffer), pf_tmp);
     if( len <=0 )
        {
        ret = -1;
        goto error;
        }

    p_de=(unsigned char *)malloc(rsa_private_len+1);
    memset(p_de,0,rsa_private_len+1);

    //printf("%s(%d)p_en = %p,rsa_public_len = %d\n", __FUNCTION__,__LINE__,p_en,rsa_public_len);
    len =RSA_private_decrypt (rsa_private_len,raw_buffer,p_de,p_rsa_private,RSA_NO_PADDING);
    printf("%s(%d) p_de = %s\n",__FUNCTION__,__LINE__,p_de);
    if ( len != rsa_private_len )
        {
        fprintf(stderr,"Error: ciphertext should match length of key\n");
        exit(1);
        }

error2:
    fclose(pf_tmp);

error:
    free(ptr_en);
    free(ptr_de);
    fclose(fp_public);
    fclose(fp_private);
    RSA_free(p_rsa_public);
    RSA_free(p_rsa_private);

    return ret;
}

I use openssl. First off I created private/public keys, then I encrypt a string and keep the result in a file. When I try to decrypt the file my program fails. What's more, the encrypted file is different each time(I use md5sum checked). What have I missed to deal with?

   /*
    gcc -lssl queation.c -o test_ssl
    #openssl genrsa -out test_private.key 1024
    #openssl rsa -in test_private.key -pubout -out test_public.key

*/


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

#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/netdevice.h>
#include <linux/sockios.h>
#include <linux/if.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/if_arp.h>
#include <netinet/if_ether.h>
#include <netinet/ether.h>

#include <fcntl.h>
#include <sys/socket.h>

#define OPENSSLKEY "test_private.key"
#define PUBLICKEY "test_public.key"
#define BUFFSIZE 1024
#define SIZE 1024

#define LIC_FILE  "lic.rn"
#define PRTFL printf("fun = %s, line = %d\n", __FUNCTION__,__LINE__)

static char *ptr_en;
static char *p_en;
static RSA  *p_rsa_public;
static FILE  *fp_public;
static int flen_public, rsa_public_len;


static char *ptr_de;
static char *p_de;
static RSA  *p_rsa_private;
static FILE  *fp_private;
static int flen_private, rsa_private_len;

void usage( unsigned char * prog_name)
{
    printf("usage:  %s\n",
        prog_name);
    exit(1);
}

int main(int argc , char ** argv)
{
    int i, ret , len;
    unsigned char buf_plain[32];
    unsigned char *buf_en;
    unsigned char *raw_buffer;

    FILE * pf_tmp;

    if( argc != 1)
        {
        usage(argv[0]);
        }

    snprintf(buf_plain,sizeof(buf_plain),"this is a test line.\n");

    if((fp_public=fopen(PUBLICKEY,"r"))==NULL)
        {
        perror("open public key file error");
        ret = -1;
        goto error;
        }   

    if((p_rsa_public = PEM_read_RSA_PUBKEY(fp_public,NULL,NULL,NULL))==NULL)
        {
        ERR_print_errors_fp(stdout);
        ret = -1;
        goto error;
        }

    rsa_public_len=RSA_size(p_rsa_public);
    p_en=(unsigned char *)malloc(rsa_public_len+1);
    memset(p_en,0,rsa_public_len+1);
    //printf("%s(%d)p_en = %p,rsa_public_len = %d\n", __FUNCTION__,__LINE__,p_en,rsa_public_len);

    len = RSA_public_encrypt(rsa_public_len,buf_plain,p_en,p_rsa_public,RSA_NO_PADDING);
    if (len !=rsa_public_len)
        {
        fprintf(stderr,"Error: len =%d,  rsa_public_len = %d,ciphertext should match length of key\n", len,rsa_public_len);
        ret = -1;
        goto error;
        }


    pf_tmp = fopen(LIC_FILE,"w");
    if( NULL == pf_tmp )
        {
        printf("open %s failed\n",LIC_FILE);
        ret = -1;
        goto error;
        }

    fwrite(p_en,1,128,pf_tmp);
    fclose(pf_tmp);

    if((fp_private=fopen(OPENSSLKEY,"r"))==NULL)
        {
        perror("open private key file error");
        ret = -1;
        goto error;
        }   

    if((p_rsa_private=PEM_read_RSAPrivateKey(fp_private,NULL,NULL,NULL))==NULL)
        {
        ERR_print_errors_fp(stdout);
        ret = -1;
        goto error;
        }

    rsa_private_len = RSA_size(p_rsa_private);

    pf_tmp = fopen(LIC_FILE,"r");
    if( NULL == pf_tmp )
        {
        printf("open %s failed\n",LIC_FILE);
        ret = -1;
        goto error2;
        }

    raw_buffer = calloc(rsa_private_len,sizeof(char));
    if( NULL == raw_buffer )
        {
        ret = -1;
        goto error;
        }

     len = fread(raw_buffer,  sizeof(char),sizeof(raw_buffer), pf_tmp);
     if( len <=0 )
        {
        ret = -1;
        goto error;
        }

    p_de=(unsigned char *)malloc(rsa_private_len+1);
    memset(p_de,0,rsa_private_len+1);

    //printf("%s(%d)p_en = %p,rsa_public_len = %d\n", __FUNCTION__,__LINE__,p_en,rsa_public_len);
    len =RSA_private_decrypt (rsa_private_len,raw_buffer,p_de,p_rsa_private,RSA_NO_PADDING);
    printf("%s(%d) p_de = %s\n",__FUNCTION__,__LINE__,p_de);
    if ( len != rsa_private_len )
        {
        fprintf(stderr,"Error: ciphertext should match length of key\n");
        exit(1);
        }

error2:
    fclose(pf_tmp);

error:
    free(ptr_en);
    free(ptr_de);
    fclose(fp_public);
    fclose(fp_private);
    RSA_free(p_rsa_public);
    RSA_free(p_rsa_private);

    return ret;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一片旧的回忆 2025-01-12 11:57:24

看起来您没有读取整个文件:

len = fread(raw_buffer,  sizeof(char),sizeof(raw_buffer), pf_tmp);

请注意,sizeof(raw_buffer) 是指针的大小,但您在文件中写入了 128 个字节(1024 位)。所以你只读回 4 或 8 个字节并尝试解密。

尝试读回 128 字节。

It looks like you are not reading the whole file:

len = fread(raw_buffer,  sizeof(char),sizeof(raw_buffer), pf_tmp);

Note that sizeof(raw_buffer) is the size of a pointer, but you wrote 128 bytes into the file (1024 bits). So you're only reading back 4 or 8 bytes and trying to decrypt that.

Try reading 128 bytes back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文