c++ - 使用公钥进行 RSA 加密。基于键的数据大小

标签 c++ openssl size rsa

我正在使用 OpenSSL RSA API 通过服务器的公钥来加密数据。

uint32_t rsasize = RSA_size(keypair); // -- RSA key size == 256

uint32_t dl = 255
uint8_t *d; // points to 255 bytes of data

unsigned char *encrypt = (unsigned char*)malloc(rsasize);

auto num = RSA_public_encrypt(dl, d, encrypt, keypair, RSA_NO_PADDING);
if ( num == -1 )
{
    auto err = malloc(130);
    ERR_load_crypto_strings();
    ERR_error_string(ERR_get_error(), (char*)err);
    printf("Error encrypting message: %s\n", err);
    return nullptr;
}

我正在使用RSA_NO_PADDING,因此 RSA 应该可以轻松地使用 256 字节公钥加密 255 字节。但我收到了这个:

Error encrypting message: error:0406B07A:rsa routines:RSA_padding_add_none:data too small for key size

我将 dl(data_lenght) 更改为 256(仅 +1),得到了以下结果:

Error encrypting message: error:04068084:rsa routines:RSA_EAY_PUBLIC_ENCRYPT:data too large for modulus

我知道 RSA 可以使用 256 个 key 对 255 个字节进行编码。有什么问题吗?

最佳答案

您在错误的一端填充。如果您有 255 字节的数据并且想要将其填充到 256,则应该在高位端添加一个零字节。换句话说,您应该在 d[0] 之前插入 0。

关于c++ - 使用公钥进行 RSA 加密。基于键的数据大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17746263/

相关文章:

c++ - C++ 中的数组和函数

windows - 无法在 Windows 上构建 Iron hello world

Android 应用程序下载模式涉及不同设备/屏幕上的相同图像

C++ Windows字体大小问题

size - 哈希表的最大大小应该是多少?

c++ - 数组不从 C++ 中的文件读取数据

c++ - 错误运算符 new[] : function does not take 1 arguments

macos - 弃用的 OpenSSL 功能

SSL证书: How to display country and state information?

c++ - 素数查找器找不到素数,在 7 后停止