gpt4 book ai didi

c++ - RSA可变加密长度

转载 作者:行者123 更新时间:2023-11-30 03:34:48 25 4
gpt4 key购买 nike

我有创建 RSA 加密的示例程序(取自在线示例)。

#include <cstring>
#include <iostream>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/bio.h>

RSA * createPublicRSA(unsigned char * key)
{
RSA * rsa= NULL;
BIO * keybio = BIO_new_mem_buf(key, -1);
rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa, NULL, NULL);
return rsa;
}

int public_encrypt(unsigned char * data, int data_len, unsigned char * key, unsigned char * encrypted)
{
RSA * rsa = createPublicRSA(key);
int result = RSA_public_encrypt(data_len, data, encrypted, rsa, RSA_PKCS1_PADDING);
return result;
}

int main(int argc, char* argv[]) {
unsigned char publicKey[]="-----BEGIN PUBLIC KEY-----\n"\
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy8Dbv8prpJ/0kKhlGeJY\n"\
"ozo2t60EG8L0561g13R29LvMR5hyvGZlGJpmn65+A4xHXInJYiPuKzrKUnApeLZ+\n"\
"vw1HocOAZtWK0z3r26uA8kQYOKX9Qt/DbCdvsF9wF8gRK0ptx9M6R13NvBxvVQAp\n"\
"fc9jB9nTzphOgM4JiEYvlV8FLhg9yZovMYd6Wwf3aoXK891VQxTr/kQYoq1Yp+68\n"\
"i6T4nNq7NWC+UNVjQHxNQMQMzU6lWCX8zyg3yH88OAQkUXIXKfQ+NkvYQ1cxaMoV\n"\
"PpY72+eVthKzpMeyHkBn7ciumk5qgLTEJAfWZpe4f4eFZj/Rc8Y8Jj2IS5kVPjUy\n"\
"wQIDAQAB\n"\
"-----END PUBLIC KEY-----\n";

unsigned char plainText[2048/8] = "plain text"; //key length : 2048
size_t length = strlen(reinterpret_cast<const char*>(plainText));
unsigned char encrypted[4098]= {};
int encrypted_length = public_encrypt(plainText, length, publicKey, encrypted);
if (argc > 1) {
printf("Encrypted length: %d (Actual length: %lu)\n", encrypted_length, strlen(const_cast<const char*>(reinterpret_cast<char*>(encrypted))));
} else {
std::cout << encrypted;
}
}

私钥是:

-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAy8Dbv8prpJ/0kKhlGeJYozo2t60EG8L0561g13R29LvMR5hy
vGZlGJpmn65+A4xHXInJYiPuKzrKUnApeLZ+vw1HocOAZtWK0z3r26uA8kQYOKX9
Qt/DbCdvsF9wF8gRK0ptx9M6R13NvBxvVQApfc9jB9nTzphOgM4JiEYvlV8FLhg9
yZovMYd6Wwf3aoXK891VQxTr/kQYoq1Yp+68i6T4nNq7NWC+UNVjQHxNQMQMzU6l
WCX8zyg3yH88OAQkUXIXKfQ+NkvYQ1cxaMoVPpY72+eVthKzpMeyHkBn7ciumk5q
gLTEJAfWZpe4f4eFZj/Rc8Y8Jj2IS5kVPjUywQIDAQABAoIBADhg1u1Mv1hAAlX8
omz1Gn2f4AAW2aos2cM5UDCNw1SYmj+9SRIkaxjRsE/C4o9sw1oxrg1/z6kajV0e
N/t008FdlVKHXAIYWF93JMoVvIpMmT8jft6AN/y3NMpivgt2inmmEJZYNioFJKZG
X+/vKYvsVISZm2fw8NfnKvAQK55yu+GRWBZGOeS9K+LbYvOwcrjKhHz66m4bedKd
gVAix6NE5iwmjNXktSQlJMCjbtdNXg/xo1/G4kG2p/MO1HLcKfe1N5FgBiXj3Qjl
vgvjJZkh1as2KTgaPOBqZaP03738VnYg23ISyvfT/teArVGtxrmFP7939EvJFKpF
1wTxuDkCgYEA7t0DR37zt+dEJy+5vm7zSmN97VenwQJFWMiulkHGa0yU3lLasxxu
m0oUtndIjenIvSx6t3Y+agK2F3EPbb0AZ5wZ1p1IXs4vktgeQwSSBdqcM8LZFDvZ
uPboQnJoRdIkd62XnP5ekIEIBAfOp8v2wFpSfE7nNH2u4CpAXNSF9HsCgYEA2l8D
JrDE5m9Kkn+J4l+AdGfeBL1igPF3DnuPoV67BpgiaAgI4h25UJzXiDKKoa706S0D
4XB74zOLX11MaGPMIdhlG+SgeQfNoC5lE4ZWXNyESJH1SVgRGT9nBC2vtL6bxCVV
WBkTeC5D6c/QXcai6yw6OYyNNdp0uznKURe1xvMCgYBVYYcEjWqMuAvyferFGV+5
nWqr5gM+yJMFM2bEqupD/HHSLoeiMm2O8KIKvwSeRYzNohKTdZ7FwgZYxr8fGMoG
PxQ1VK9DxCvZL4tRpVaU5Rmknud9hg9DQG6xIbgIDR+f79sb8QjYWmcFGc1SyWOA
SkjlykZ2yt4xnqi3BfiD9QKBgGqLgRYXmXp1QoVIBRaWUi55nzHg1XbkWZqPXvz1
I3uMLv1jLjJlHk3euKqTPmC05HoApKwSHeA0/gOBmg404xyAYJTDcCidTg6hlF96
ZBja3xApZuxqM62F6dV4FQqzFX0WWhWp5n301N33r0qR6FumMKJzmVJ1TA8tmzEF
yINRAoGBAJqioYs8rK6eXzA8ywYLjqTLu/yQSLBn/4ta36K8DyCoLNlNxSuox+A5
w6z2vEfRVQDq4Hm4vBzjdi3QfYLNkTiTqLcvgWZ+eX44ogXtdTDO7c+GeMKWz4XX
uJSUVL5+CVjKLjZEJ6Qc2WZLl94xSwL71E41H4YciVnSCQxVc4Jw
-----END RSA PRIVATE KEY-----

当我构建它时

g++ encr.cpp -o encr -lcrypto -I/opt/local/include/

并运行几次,我得到变量“实际长度”

$ ./encr show-length
Encrypted length: 256 (Actual length: 256) <--- Correct
$ ./encr show-length
Encrypted length: 256 (Actual length: 79)
$ ./encr show-length
Encrypted length: 256 (Actual length: 215)
$ ./encr show-length
Encrypted length: 256 (Actual length: 52)
$ ./encr show-length
Encrypted length: 256 (Actual length: 256) <--- Correct
$ ./encr show-length
Encrypted length: 256 (Actual length: 48)

你也可以做 ./encr |厕所 | awk '{print $3;}' 获取尺寸

无论我在哪里提到“正确”,它都会很好地解密,否则我会得到错误(这里是示例)

$ ./encr | openssl rsautl -decrypt -inkey private.pem
RSA operation error
140735156518992:error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error:rsa_pk1.c:273:
140735156518992:error:04065072:rsa routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed:rsa_eay.c:602:
$ ./encr | openssl rsautl -decrypt -inkey private.pem <---------------------------------- CORRECTLY PADDED
plain text$ ./encr | openssl rsautl -decrypt -inkey private.pem
RSA operation error
140735156518992:error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error:rsa_pk1.c:273:
140735156518992:error:04065072:rsa routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed:rsa_eay.c:602:
$ ./encr | openssl rsautl -decrypt -inkey private.pem <---------------------------------- CORRECTLY PADDED
plain text$ ./encr | openssl rsautl -decrypt -inkey private.pem <---------------------------------- CORRECTLY PADDED
plain text$ ./encr | openssl rsautl -decrypt -inkey private.pem
RSA operation error
140735156518992:error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error:rsa_pk1.c:273:
140735156518992:error:04065072:rsa routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed:rsa_eay.c:602:

我在使用 RSA_public_encrypt 时做错了什么?

最佳答案

在 C/C++ 中,char*unsigned char* 可以表示“一个字符串”或“一些字节”,由开发人员决定哪个是其中(尽管 unsigned char* 通常表示“一些字节”,“unsigned”是线索)。

RSA 与几乎所有计算机设计的密码例程一样,对字节进行操作。有时输入字节是文本,但输出字节几乎总是不是。

如果您有一个 40 位 RSA key ,它可以生成一个答案 21 74 65 78 74。指向该位置的指针上的 strlen 将返回……某个至少为 5 的数字。如果它恰好返回 5,则意味着我们很幸运,下一段内存已分配为 0x00。在这种幸运的情况下,我们可以 printf (%s) 指针,它会发出 !text

它可能产生了 25 00 F2 1B D5,它的 strlen1 一样,因为第二个字节是 0x00strlen 解释“字符串结尾”的方式。

如果您需要输出文本,则需要将其转换为文本形式。十六进制是短值的常用方式,但 RSA-2048 生成 256 字节的答案。那将是 512 个十六进制字符,或者在 Base64 中它只会是 344,这就是为什么在这些情况下倾向于使用 Base64 编码的原因。

关于c++ - RSA可变加密长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41755801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com