簡體   English   中英

使用公鑰的 RSA 加密。 基於密鑰的數據大小

[英]RSA encryption using public key. Data size based on key

我正在使用 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 個密鑰編碼 255 個字節。 有什么問題?

你在錯誤的一端填充。 如果您有 255 個字節的數據並且想要將其填充為 256,則應在高位末尾添加一個零字節。 換句話說,您應該在 d[0] 之前插入一個 0。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM