PyCrypto 和 Crypto++ 中生成的加密字符串大小不同

发布于 2024-10-11 03:46:02 字数 2024 浏览 7 评论 0原文

我最近使用Python脚本来加密字符串。但无法使用 Crypto++ 在 C++ 中解密它。我刚刚比较了生成的加密字符串,发现它们不一样。有人可以帮忙吗?

这是我的 Python 代码:

key  = "0123456789abcdef"
data = "ccccccccccccccccdddddddddddddddd"
iv = "aaaaaaaaaaaaaaaa"
encryptor = AES.new(key, AES.MODE_CBC, iv)
enc = encryptor.encrypt(data)
print enc

这是我的 C++ 代码:

std::string key = "0123456789abcdef";
std::string iv  = "aaaaaaaaaaaaaaaa";


std::string plaintext = "ccccccccccccccccdddddddddddddddd";
std::string ciphertext;
std::string decryptedtext;

std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl;
std::cout << plaintext;
std::cout << std::endl << std::endl;

CryptoPP::AES::Encryption aesEncryption((byte *)key.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, (byte *)iv.c_str() );

CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext ) );
stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() + 1 );
stfEncryptor.MessageEnd();

std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;

for( int i = 0; i < ciphertext.size(); i++ ) {

    std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " ";
}

CryptoPP::AES::Decryption aesDecryption((byte *)key.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, (byte *)iv.c_str() );

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

std::cout << "Decrypted Text: " << std::endl;
std::cout << decryptedtext;
std::cout << std::endl << std::endl;

I recently used a Python script to encrypt a string. But could not decrypt it in C++ using Crypto++. I just compared generated encrypted strings and found they are not the same. Anybody can help?

Here is my Python code:

key  = "0123456789abcdef"
data = "ccccccccccccccccdddddddddddddddd"
iv = "aaaaaaaaaaaaaaaa"
encryptor = AES.new(key, AES.MODE_CBC, iv)
enc = encryptor.encrypt(data)
print enc

Here is my C++ code:

std::string key = "0123456789abcdef";
std::string iv  = "aaaaaaaaaaaaaaaa";


std::string plaintext = "ccccccccccccccccdddddddddddddddd";
std::string ciphertext;
std::string decryptedtext;

std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl;
std::cout << plaintext;
std::cout << std::endl << std::endl;

CryptoPP::AES::Encryption aesEncryption((byte *)key.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, (byte *)iv.c_str() );

CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext ) );
stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() + 1 );
stfEncryptor.MessageEnd();

std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;

for( int i = 0; i < ciphertext.size(); i++ ) {

    std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " ";
}

CryptoPP::AES::Decryption aesDecryption((byte *)key.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, (byte *)iv.c_str() );

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

std::cout << "Decrypted Text: " << std::endl;
std::cout << decryptedtext;
std::cout << std::endl << std::endl;

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

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

发布评论

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

评论(1

じее 2024-10-18 03:46:02

默认情况下,Crypto++ 对明文字符串使用 PKCS5 填充。这会在字符串末尾添加填充,以确保字符串是块大小的倍数,对于 AES,块大小为 16 字节。如果明文已经是 16 的倍数,Crypto++ 会在明文中添加另外 16 个字节的填充,然后加密整个内容。 PyCrypto 不会添加这种额外的填充,而是由用户来确保正确的块大小。请注意,当您解密 Crypto++ 加密的密文时,多余的填充会自动删除。

By default, Crypto++ employs PKCS5 padding to the plaintext strings. This adds padding to the end of the string to make sure the string is a multiple of the block size which, for AES, is 16 bytes. If the plaintext is already a multiple of 16, Crypto++ adds another 16 bytes of padding to the plaintext and then encrypts the whole thing. PyCrypto doesn't add this extra padding, leaving it up to the user to ensure the correct block size. Note that when you decrypt the Crypto++-encrypted cipher text, the extra padding is automatically removed.

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