我已经用Python加密了一条消息:
import base64
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
key = """-----BEGIN RSA PRIVATE KEY-----
SOME_SECURE_KEY
-----END RSA PRIVATE KEY-----"""
private_key = RSA.import_key(key)
cipher = PKCS1_v1_5.new(private_key)
print(base64.b64encode(cipher.encrypt(b"SECRET MESSAGE")).decode())
我想使用 OpenSSL 在 C++ 中解密它。 我怎样才能做到这一点?
int main() {
std::string privateKey = "MY-KEY";
BIO *bio = BIO_new_mem_buf(privateKey.c_str(), privateKey.length()); // -1: assume string is null terminated
RSA *rsa = PEM_read_bio_RSAPrivateKey(bio, nullptr, nullptr, nullptr);
BIO_free(bio);
auto vec = a_function_that_decodes_base64(my_cipher_text);
unsigned char arr[vec.size()];
std::copy(vec.begin(), vec.end(), arr);
unsigned char decrypted[4098] = {};
int decrypt_len = RSA_private_decrypt(RSA_size(rsa),
arr, decrypted, rsa, RSA_PKCS1_PADDING);
if (decrypt_len == -1) {
char *err = ERR_error_string(ERR_get_error(), NULL);
std::cout << err << std::endl;
}
RSA_free(rsa);
}
我已经尝试过这段代码,但它不起作用,并且出现错误,提示填充不正确
无符号字符解密消息[256]; int解密长度= RSA_private_decrypt(bytesRead,加密消息,解密消息,私有密钥,RSA_PKCS1_OAEP_PADDING);