RSA加密大小并复制到Char Buffer? [英] RSA encryption size and copying to Char Buffer ?

查看:126
本文介绍了RSA加密大小并复制到Char Buffer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经使用C程序完成了RSA ENCRYPTION,并在字符缓冲区中获得了加密的消息.我需要将此加密消息以二进制格式发送到其他系统.
我添加了RSA_size(rsa)命令和free(已加密).但是我在RSA_size(rsa)上获得了coredump.所以我删除了它.
我也想计算加密变量的大小.但是我尝试使用strlen(),执行时得到了不同的长度值. "Bufsize"值始终= 256,但加密字符值始终随不同值而变化.我希望它应该是256 ...请提出建议.
我需要将此加密的值复制到另一个字符缓冲区中,并且必须通过TCP/IP作为数据包发送.复制到字符缓冲区时我很困惑.
在上面需要一些想法.
在此先感谢
帕塔比.

Hi,
I have done RSA ENCRYPTION using C program and got the encrypted message in character buffer. I need to send this encrypted message to a different system in a binary format.
I have added the RSA_size(rsa) command and free( encrypted). But I am getting coredump on RSA_size(rsa). So I removed it.
Also I want to calculate the size of encrypted variable. But I tried with strlen(), I am getting different length value while execution. ''Bufsize'' value always = 256 but the encrypted character value always varies with different values. It should be 256, I hope ...Please suggest.
I need to copy this encrypted value into another character buffer and I have to send as a packet via TCP / IP. I am confused while copying to character buffer.
Need some idea on the above.
Thanks in Advance
Pattabi.

#include <stdio.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
#include <signal.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>

void EncrCardPin(char *,char *);
int main(int argc, char *argv[])
{

    char* pin="2345";
    //char storebuf[256];

    unsigned char* storebuf = (unsigned char *) malloc(500);
    memset(storebuf,'\0',sizeof(storebuf));

    EncrCardPin(pin,storebuf);

    return 0;
}

void EncrCardPin(char *pin,char *encpin)
 {
    int bufSize;
    char *pubname="publkey.pem"; /***** 2048 bit key********/

    FILE *keyfile = NULL;
    RSA* rsa=NULL;
    FILE *test = NULL;

    //int len=RSA_size(rsa) - 11; /********* Giving Core Dump Always *******/

    unsigned char* encrypted = (unsigned char *) malloc(500);

    test=fopen("test.bin","wb");

    printf("Opening the puclic key file: [%s]\n",pubname);
    keyfile = fopen(pubname, "r");

    if (!keyfile)
    { printf("error occured while reading public key file");    }

    rsa =   PEM_read_RSA_PUBKEY(keyfile, NULL, NULL, NULL);

    if (rsa == NULL)
    {       printf("Badness has occured! Did not read  public key file\n"); }
    else
    {       printf("Opened public key file OK!\n"); }

    bufSize = RSA_public_encrypt(strlen(pin), (unsigned char *) pin, encpin, rsa, RSA_PKCS1_PADDING);
    if (bufSize == -1)
    {   printf("Badness has occured! encryption failed\n");
        RSA_free(rsa);
    }
    else
    {
        printf("Encrypted the message OK! \n");
        fprintf(test,"%s",encpin);
        printf("The buffersize is : [%d]\n ================",bufSize);
    }
    free(encrypted);
    RSA_free(rsa);
    fclose(keyfile);
    fclose(test);


 }



[edit]已修改注释,以删除连字符(后接大于号).这样可以使您其余的代码可见! -OriginalGriff [/edit]



[edit]Comment edited to remove hyphen followed by greater than symbol. This allows the rest of your code to be visible! - OriginalGriff [/edit]

推荐答案

问题可能在这里:
The problem is likely to be here:
RSA* rsa=NULL;
FILE *test = NULL;
//int len=RSA_size(rsa) - 11; /********* Giving Core Dump Always *******/

由于RSA_size需要指向RSA的指针,将其传递为null,可能会导致内存访问冲突,从而触发核心转储.

尝试将其传递给实际RSA对象的指针!

[edit]错字字,我错过了"RSA_size"中的"_size"-OriginalGriff [/edit]




我已经这样修改了
RSA * rsa;
int len = RSA_size(rsa);
但是仍然将coredump消息显示为在BN_num_bits()中".不知道它从何而来.有任何线索吗???"

是的,请阅读我说的话.

rsa是一个变量,其中包含指向RSA对象的指针.

您没有为rsa变量分配有效的RSA对象.

当RSA_Size尝试使用您将其作为指针的RSA对象时,它将访问错误的内存.

Since RSA_size requires a pointer to an RSA, handing it a null, will probably give a memory access violation, triggering a core dump.

Try handing it a pointer to an actual RSA object!

[edit]Typo, I missed the "_size" off "RSA_size" - OriginalGriff[/edit]


"Hi ,

I have modified like this
RSA *rsa;
int len = RSA_size(rsa);
But still gives coredump message as "in BN_num_bits()". Don''t know from where its referring this . Any clue ????"

Yes - read what I said.

rsa is a variable, which holds a pointer to an RSA object.

You do not assign a valid RSA object to the rsa variable.

When RSA_Size tries to use the RSA object you hand it the pointer of, it accesses the wrong memory.

Would you expect this to work?

int *i;
*i = 6;

还是应该这样?

int *i;
int j;
i = &j;
*i = 6;


这篇关于RSA加密大小并复制到Char Buffer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆