本文介绍了如何加密/解密与RSA长的输入消息? [Openssl的,C]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的加密/解密的消息一个简单的测试程序。

我有一个 keylength

INT keylength = 1024; //它也可以是2048,4096

和最大输入长度:

INT MAXLEN =(keylength / 8)-11;

和我知道,我的输入大小应该是<比MAXLEN,是这样的:

 如果(INSIZE> = MAXLEN)
        的printf(不能加密/解密\\ n!);

我的问题很简单 - 它是可能的(如果是这样,我怎么能做到这一点)来加密/与RSA信息比 MAXLEN 长解密?

主要code也很简单,但仅当INSIZE< MAXLEN:

  IF((encBytes = RSA_public_encrypt(strlen的(BUFF1)+1,BUFF1,buff2,密钥对,RSA_PKCS1_PADDING))== -1)
    {
        的printf(错误。\\ n);
    }    如果((decBytes = RSA_private_decrypt(encBytes,buff2,buff3,密钥对,RSA_PKCS1_PADDING))== -1)
    {
        的printf(错误。\\ n);
    }


解决方案

加密长消息,需要综合方案 - RSA算法加密会话密钥(即AES密钥),以及数据本身使用该密钥加密。
我建议没有发明另一自行车和使用完善的方案,即PKCS#7 / CMS或OpenPGP的,根据您的需要。

I wrote a simple test program that encrypts/decrypts a message.

I have a keylength:

int keylength = 1024; // it can also be 2048, 4096

and max input length:

int maxlen = (keylength/8)-11;

and I know that my input size should be < than maxlen, something like this:

if(insize >= maxlen)
        printf("cannot encrypt/decrypt!\n");

My question is simple - is it possible (if so, how can I do this) to encrypt/decrypt with RSA messages LONGER than maxlen?

Main code is also, very simple but works only when insize < maxlen:

   if((encBytes=RSA_public_encrypt(strlen(buff1)+1, buff1, buff2, keypair, RSA_PKCS1_PADDING)) == -1)
    {
        printf("error\n");
    }

    if((decBytes=RSA_private_decrypt(encBytes, buff2, buff3, keypair, RSA_PKCS1_PADDING)) == -1)
    {
        printf("error\n");
    }
解决方案

Encrypting long messages requires combined scheme - RSA algorithm encrypts session key (i.e. AES key), and data itself is encrypted with that key.I would recommend to not invent another bicycle and use well established scheme, i.e. PKCS#7/CMS or OpenPGP, depending on your needs.

这篇关于如何加密/解密与RSA长的输入消息? [Openssl的,C]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 08:49