Botan C ++。如何解密ssh包?

时间:2016-08-14 17:50:36

标签: c++ ssh botan

我正在尝试与您的库实施SSH协议。我尝试在cbc模式下使用3des algo。

我按如下方式创建加密:

_encrypt.reset(new Pipe(encryptFilter = new CBC_Encryption(cipher->clone(), new Null_Padding, c2s_key, c2s_iv)));`

我按如下方式创建解密:

_decrypt.reset(new Pipe(decryptFilter = new CBC_Decryption(cipher->clone(), new Null_Padding, s2c_key, s2c_iv))); 

我尝试解密如下:

bool crypto::decryptPacket(Botan::SecureVector<Botan::byte> &decrypted, Botan::SecureVector<Botan::byte> &packet, uint32_t len)
{
    uint32_t pLen = packet.size();

    if (pLen % _decryptBlock != 0)
    {
        len = pLen + (pLen % _decryptBlock);
    }

    for (uint32_t pktIndex = 0; pktIndex < len; pktIndex += _decryptBlock)
    {
            Botan::SecureVector<Botan::byte> e(packet.begin() + pktIndex, packet.size() + pktIndex + _decryptBlock);
            _decrypt->process_msg(e, _decryptBlock);

            decrypted += _decrypt->read_all(_decrypt->message_count() - 1);
    }

    return true;
}

我尝试加密如下:

bool crypto::encryptPacket(Botan::SecureVector<Botan::byte> &crypted, Botan::SecureVector<Botan::byte> &hmac, Botan::SecureVector<Botan::byte> &packet, uint32_t seq)
{
    SecureVector<Botan::byte> macStr;
    uint32_t nSeq = (uint32_t)htonl(seq);

    _encrypt->start_msg();
    _encrypt->write(packet.begin(), packet.size());
    _encrypt->end_msg();

    crypted = _encrypt->read_all(_encrypt->message_count() - 1);

    if (_hmacOut)
    {
        macStr = SecureVector<Botan::byte>((Botan::byte*)&nSeq, 4);
        macStr += packet;
        hmac = _hmacOut->process(macStr);
    }

    return true;
}

我尝试加密如下:

bool crypto::encryptPacket(Botan::SecureVector<Botan::byte> &crypted, Botan::SecureVector<Botan::byte> &hmac, Botan::SecureVector<Botan::byte> &packet, uint32_t seq)
{
    SecureVector<Botan::byte> macStr;
    uint32_t nSeq = (uint32_t)htonl(seq);

    _encrypt->start_msg();
    _encrypt->write(packet.begin(), packet.size());
    _encrypt->end_msg();

    crypted = _encrypt->read_all(_encrypt->message_count() - 1);

    if (_hmacOut)
    {
        macStr = SecureVector<Botan::byte>((Botan::byte*)&nSeq, 4);
        macStr += packet;
        hmac = _hmacOut->process(macStr);
    }

    return true;
}

因此,加密数据包通常,但当我尝试解密第二个数据包和下一个包第一个块解密错误。有什么问题?

0 个答案:

没有答案