gpt4 book ai didi

c++ - AES-128 CFB-8 解密的前 16 个字节已损坏

转载 作者:行者123 更新时间:2023-11-28 01:15:13 31 4
gpt4 key购买 nike

我最近一直在做一个项目,该项目应该借助协议(protocol)连接到服务器。到目前为止一切顺利,但是当我梳理解密这些包时,我很快注意到有些地方工作不正常。

所有数据包的前 16 个字节都被错误地解密。我已经用不同的库尝试过,但这也不起作用。我使用 C++ 语言工作,到目前为止使用 Crypto++ 和 OpenSSL 进行解密,但没有成功。

这下Link你可以找到协议(protocol),这里是解密协议(protocol)Link这是我对应的代码:

OpenSSL:

void init() {

unsigned char* sharedSecret = new unsigned char[AES_BLOCK_SIZE];

std::generate(sharedSecret,
sharedSecret + AES_BLOCK_SIZE,
std::bind(&RandomGenerator::GetInt, &m_RNG, 0, 255));

for (int i = 0; i < 16; i++) {
sharedSecretKey += sharedSecret[i];
}

// Initialize AES encryption and decryption
if (!(m_EncryptCTX = EVP_CIPHER_CTX_new()))
std::cout << "123" << std::endl;

if (!(EVP_EncryptInit_ex(m_EncryptCTX, EVP_aes_128_cfb8(), nullptr, (unsigned char*)sharedSecretKey.c_str(), (unsigned char*)sharedSecretKey.c_str())))
std::cout << "123" << std::endl;

if (!(m_DecryptCTX = EVP_CIPHER_CTX_new()))
std::cout << "123" << std::endl;

if (!(EVP_DecryptInit_ex(m_DecryptCTX, EVP_aes_128_cfb8(), nullptr, (unsigned char*)sharedSecretKey.c_str(), (unsigned char*)sharedSecretKey.c_str())))
std::cout << "123" << std::endl;

m_BlockSize = EVP_CIPHER_block_size(EVP_aes_128_cfb8());
}

std::string result;
int size = 0;
result.resize(1000);
EVP_DecryptUpdate(m_DecryptCTX, &((unsigned char*)result.c_str())[0], &size, &sendString[0], data.size());

加密++:

CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption AESDecryptor((byte*)sharedSecret.c_str(), (unsigned int)16, sharedSecret.c_str(), 1);

std::string sTarget("");
CryptoPP::StringSource ss(data, true, new CryptoPP::StreamTransformationFilter(AESDecryptor, new CryptoPP::StringSink(sTarget)));

我认为值得一提的是,我对 key 和 iv(初始化 vector )使用了同一个共享 key 。在其他帖子中,这通常被标记为问题。我不知道在这种情况下如何解决它,因为协议(protocol)需要它。

我期待有建设性的反馈。

最佳答案

EVP_EncryptInit_ex(m_EncryptCTX, EVP_aes_128_cfb8(), nullptr,
(unsigned char*)sharedSecretKey.c_str(), (unsigned char*)sharedSecretKey.c_str()))

And:

CFB_Mode<AES>::Decryption AESDecryptor((byte*)sharedSecret.c_str(),
(unsigned int)16, sharedSecret.c_str(), 1);

std::string sTarget("");
StringSource ss(data, true, new StreamTransformationFilter(AESDecryptor, new StringSink(sTarget)));

这不是很明显,但您需要为 Crypto++ 中的分组密码操作模式设置反馈大小。 Crypto++ 反馈大小默认为 128。

设置 CFB 模式反馈大小的代码可以在 CFB Mode 找到。在 Crypto++ 维基上。您想要页面下方的第 3 个或第 4 个示例。

AlgorithmParameters params =
MakeParameters(Name::FeedbackSize(), 1 /*8-bits*/)
(Name::IV(), ConstByteArrayParameter(iv));

这是一种传递参数的尴尬方式。它记录在源文件和 wiki 上 NameValuePairs .它允许您通过一致的接口(interface)传递任意参数。一旦你尝到了它的味道,它就会很强大。

然后使用params 为加密器和解密器设置 key :

CFB_Mode< AES >::Encryption enc;
enc.SetKey( key, key.size(), params );

// CFB mode must not use padding. Specifying
// a scheme will result in an exception
StringSource ss1( plain, true,
new StreamTransformationFilter( enc,
new StringSink( cipher )
) // StreamTransformationFilter
); // StringSource

我相信您的调用看起来像这样(如果我正确解析 OpenSSL):

const byte* ptr = reinterpret_cast<const byte*>(sharedSecret.c_str());

AlgorithmParameters params =
MakeParameters(Name::FeedbackSize(), 1 /*8-bits*/)
(Name::IV(), ConstByteArrayParameter(ptr, 16));

CFB_Mode< AES >::Encryption enc;
enc.SetKey( ptr, 16, params );

在您的生产代码中,您应该使用唯一 key 和 iv。所以使用 HKDF 做这样的事情:

std::string seed(AES_BLOCK_SIZE, '0');
std::generate(seed, seed + AES_BLOCK_SIZE,
std::bind(&RandomGenerator::GetInt, &m_RNG, 0, 255));

SecByteBlock sharedSecret(32);
const byte usage[] = "Key and IV v1";

HKDF<SHA256> hkdf;
hkdf.DeriveKey(sharedSecret, 32, &seed[0], 16, usage, COUNTOF(usage), nullptr, 0);

AlgorithmParameters params =
MakeParameters(Name::FeedbackSize(), 1 /*8-bits*/)
(Name::IV(), ConstByteArrayParameter(sharedSecret+16, 16));

CFB_Mode< AES >::Encryption enc;
enc.SetKey(sharedSecret+0, 0, params);

在上面的代码中,sharedSecret 是它需要的两倍大。您可以使用 HDKF 从种子中导出 key 和 iv。 sharedSecret+0是16字节的 key ,sharedSecret+16是16字节的iv。

关于c++ - AES-128 CFB-8 解密的前 16 个字节已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58916216/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com