如何解密C++中的字符串数据与加密货币++其中原始字符串在 Python 中使用 pyCrypto 加密

发布于 2024-10-10 06:24:53 字数 321 浏览 10 评论 0原文

我刚刚用 pyCrypto 轻松加密了一个数据字符串,但不知道如何在 crypto++ 中解密它。任何人都可以帮助使用 crypto++ 编写 C++ 示例解密代码吗? 这是我的Python代码:

key = '0123456789abcdef' 
data = "aaaaaaaaaaaaaaaa" 
iv = ''.join(chr(random.randint(0, 0xFF)) for i in range(16)) 
encryptor = AES.new(key, AES.MODE_CBC, iv) 
enc = encryptor.encrypt(data)

I've just encrypted a data string with pyCrypto easily, but don't know how to decrypt it in crypto++. Anybody can help with a sample decryption code in C++ with crypto++?
Here is my python code:

key = '0123456789abcdef' 
data = "aaaaaaaaaaaaaaaa" 
iv = ''.join(chr(random.randint(0, 0xFF)) for i in range(16)) 
encryptor = AES.new(key, AES.MODE_CBC, iv) 
enc = encryptor.encrypt(data)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这样的小城市 2024-10-17 06:24:53

与@Jon相同的方法,有点简化

std::string ciphertext = "..."; // what Python encryption produces
std::string decryptedtext;

byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];

// populate key and iv with the correct values

CryptoPP::CBC_Mode< CryptoPP::AES >::Decryption decryptor;
decryptor.SetKeyWithIV(key, sizeof(key), iv);


CryptoPP::StringSource(ciphertext, true,
        new CryptoPP::StreamTransformationFilter( decryptor,
            new CryptoPP::StringSink( decryptedtext )
        )
);

CryptoPP::StringSourcetrue参数意味着“消耗整个输入”

请注意,(显然)你需要你的C++解密器知道您用于加密的 IV。由于您在 python 中生成随机 IV,因此流行的技术是将 IV 添加到加密文本的前面。

same approach as @Jon, a bit simplified

std::string ciphertext = "..."; // what Python encryption produces
std::string decryptedtext;

byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];

// populate key and iv with the correct values

CryptoPP::CBC_Mode< CryptoPP::AES >::Decryption decryptor;
decryptor.SetKeyWithIV(key, sizeof(key), iv);


CryptoPP::StringSource(ciphertext, true,
        new CryptoPP::StreamTransformationFilter( decryptor,
            new CryptoPP::StringSink( decryptedtext )
        )
);

The true parameter to CryptoPP::StringSource means "consume the whole input"

Note that (obviously) you need your C++ decryptor to know the IV you used for encryption. Since you generate a random IV in python, the popular technique is to prepend the IV to the encrypted text.

旧瑾黎汐 2024-10-17 06:24:53

此代码来自 2005 年的示例,但它应该为您提供一个良好的起点:

std::string ciphertext = "..."; // what Python encryption produces
std::string decryptedtext;

byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];

// populate key and iv with the correct values

CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );

CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.size() );
stfDecryptor.MessageEnd();

// it's all in decryptedText now

This code is from an example from 2005, but it should give you a good starting point:

std::string ciphertext = "..."; // what Python encryption produces
std::string decryptedtext;

byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];

// populate key and iv with the correct values

CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );

CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.size() );
stfDecryptor.MessageEnd();

// it's all in decryptedText now
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文