首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Crypto++加密,用Python.CRYPTO解密

用Crypto++加密,用Python.CRYPTO解密
EN

Stack Overflow用户
提问于 2017-01-17 23:24:31
回答 1查看 612关注 0票数 4

我正在做一个项目,它使用Crypto++来加密一些数据。

下面是我的Crypto++代码:

代码语言:javascript
复制
string plain = "Text123", encoded, cipher;
string pkey = "...";

StringSource ss1(pkey, true);
RSA::PublicKey publicKey;
PEM_Load(ss, publicKey);
RSAES_OAEP_SHA_Encryptor e(publicKey);

StringSource ss2(plain, true,
    new PK_EncryptorFilter(prng, e,
        new StringSink(cipher)
    ) 
); 

StringSource ss3(cipher, true,
    new Base64Encoder(
        new StringSink(encoded)
    )
);
cout << encoded;

我正试着用Python解密加密信息。以下是我的Python代码:

代码语言:javascript
复制
from Crypto.PublicKey import RSA
from base64 import b64decode  

cipher_text = "[THE OUTPUT OF C++]"
rsakey = RSA.importKey(open("private.txt", "r").read())
raw_cipher_data = b64decode(cipher_text)
decrypted = rsakey.decrypt(raw_cipher_data)

但是我得到了一些无法阅读的字符。

为什么我会得到不可读的字符?有人能帮我这个忙吗?

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-08-30 05:10:45

以下是在C++中加密明文和在Python中解密明文的完整工作示例:

先决条件

您已生成 key pair (reference).

  • 您有最新版本的crypto++ (我使用的是8.2.0),并且您已经应用了PEM补丁(reference).

  • 你有一个最新版本的pycryptodome (我用的是3.9.8)。pycryptodome是不再维护的pycrypto包(reference).

的临时替代品

加密

你的C++做了正确的事情。为了完整起见,我使用必要的头包含和公钥的反序列化扩展了您的代码:

代码语言:javascript
复制
#include <cryptopp/base64.h>
#include <cryptopp/files.h>
#include <cryptopp/filters.h>
#include <cryptopp/osrng.h>
#include <cryptopp/pem.h>
#include <cryptopp/rng.h>
#include <cryptopp/rsa.h>

#include <iostream>

int main(int argc, char **argv) {

  CryptoPP::AutoSeededRandomPool prng{};

  std::string plain{"Text123"};

  /**
   * Read public key in PEM format.
   */
  CryptoPP::FileSource fs{"public.pem", /*pumpAll=*/true};
  CryptoPP::RSA::PublicKey publicKey{};
  PEM_Load(fs, publicKey);

  std::string encrypted{};

  /**
   * Pump plain text through RSA encryption scheme with OAEP/SHA1 padding.
   *
   * In general, manual memory allocations should be avoided. However,
   * the CryptoPP API expects us to allocate memory for each transformer and
   * then pass the pointer to the next transformer, which then takes ownership
   * of the pointer and will free it. This design obviously predates
   * modern C++ smart pointers, which should be preferred when possible.
   */
  CryptoPP::RSAES_OAEP_SHA_Encryptor cipher{publicKey};
  auto *encoder{
      new CryptoPP::Base64Encoder{new CryptoPP::StringSink{encrypted}}};
  auto *encryptor{new CryptoPP::PK_EncryptorFilter{prng, cipher, encoder}};
  CryptoPP::StringSource ss{plain, /*pumpAll=*/true, encryptor};

  std::cout << encrypted;

  return 0;
}

解密

在Python代码中,您没有指定填充方案。由于您的C++代码使用OAEP/SHA1填充,因此您还必须在Python代码中指定此填充方案:

代码语言:javascript
复制
from base64 import b64decode

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Hash import SHA1

rsa_key = None
encrypted = None

with open("key.pem", "r") as f:
  rsa_key = RSA.importKey(f.read())

with open("encrypted", "r") as f:
  encrypted = b64decode(f.read())

cipher = PKCS1_OAEP.new(rsa_key, hashAlgo=SHA1)
decrypted = cipher.decrypt(encrypted)

print(decrypted)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41700910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档