我有一个创建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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KURe1xvMCgYBVYYcEjWqMuAvyferFGV+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 | wc | 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
时,我做错了什么?
答案 0 :(得分:2)
在C / C ++中char*
和unsigned char*
可以表示“字符串”或“某些字节”,由开发人员知道哪个是哪个(尽管unsigned char*
通常意思是“一些字节”,“无符号”是线索)。
如果你有一个40位的RSA密钥,它可以产生一个答案21 74 65 78 74
。指向该位置的指针上的strlen
将返回...某个至少为5
的数字。如果它返回5,则表示我们很幸运,下一段内存已经被分配为0x00
。在这个幸运的情况下,我们可以printf(%s)指针,它将发出!text
。
它可能产生25 00 F2 1B D5
strlen
,1
为0x00
,因为第二个字节为strlen
,{{1}}解释“{1}}的结尾字符串”
如果您需要输出为文本,则需要将其转换为文本格式。十六进制是短值的常用方法,但RSA-2048产生256字节的答案。这将是512个十六进制字符,或者在Base64中它只有344,这就是为什么Base64编码倾向于在这些情况下使用。