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

问题描述 投票:1回答:1

我最近一直在研究一个项目,该项目应该在协议的帮助下连接到服务器。到目前为止,一切都很好,但是当我梳理解密程序包时,我很快注意到有些东西无法正常工作。所有数据包的前16个字节被错误地解密,因此不可读。我已经尝试过使用不同的库,但这也不起作用。我使用C ++语言工作,到目前为止,使用CryptoPP和OpenSSL进行解密均未成功。在此Link下,您可以找到协议,在这里是解密协议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:

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)));

我认为值得一提的是,我对密钥和iv(初始向量)使用了一个相同的共享密钥。在其他帖子中,这通常被标记为问题,但是在这种情况下,由于协议需要,我不知道如何解决。

我期待着建设性的反馈。

您诚挚的卢卡斯:D

c++ encryption openssl protocols crypto++
1个回答
0
投票
EVP_EncryptInit_ex(m_EncryptCTX, EVP_aes_128_cfb8(), nullptr,
    (unsigned char*)sharedSecretKey.c_str(), (unsigned char*)sharedSecretKey.c_str()))

和:

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模式的反馈大小的代码可以在Crypto ++ Wiki的CFB Mode中找到。您需要页面下方的第3个或第4个示例。

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

这是一种传递参数的尴尬方式。它在源文件和Wiki的NameValuePairs中记录。它允许您通过一致的接口传递任意参数。一旦您领会了它,它就会很强大。

然后使用params密钥加密器和解密器:

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 );

在生产代码中,您应该使用唯一键,并且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从种子中导出密钥和iv。 sharedSecret+0是16字节的密钥,sharedSecret+16是16字节的iv。

© www.soinside.com 2019 - 2024. All rights reserved.