1

当在 CryptoPP 中使用 DefaultEncryptorWithMAC 时,它的核心似乎是使用这个 Mash 函数

// The purpose of this function Mash() is to take an arbitrary length input
// string and *deterministicly* produce an arbitrary length output string such
// that (1) it looks random, (2) no information about the input is
// deducible from it, and (3) it contains as much entropy as it can hold, or
// the amount of entropy in the input string, whichever is smaller.

static void Mash(const byte *in, size_t inLen, byte *out, size_t outLen, int iterations)
{
    if (BytePrecision(outLen) > 2)
        throw InvalidArgument("Mash: output legnth too large");

    size_t bufSize = RoundUpToMultipleOf(outLen, (size_t)DefaultHashModule::DIGESTSIZE);
    byte b[2];
    SecByteBlock buf(bufSize);
    SecByteBlock outBuf(bufSize);
    DefaultHashModule hash;

    unsigned int i;
    for(i=0; i<outLen; i+=DefaultHashModule::DIGESTSIZE)
    {
        b[0] = (byte) (i >> 8);
        b[1] = (byte) i;
        hash.Update(b, 2);
        hash.Update(in, inLen);
        hash.Final(outBuf+i);
    }

    while (iterations-- > 1)
    {
        memcpy(buf, outBuf, bufSize);
        for (i=0; i<bufSize; i+=DefaultHashModule::DIGESTSIZE)
        {
            b[0] = (byte) (i >> 8);
            b[1] = (byte) i;
            hash.Update(b, 2);
            hash.Update(buf, bufSize);
            hash.Final(outBuf+i);
        }
    }

    memcpy(out, outBuf, outLen);
}

根据此页面http://www.cryptopp.com/wiki/DefaultEncryptorWithMAC

DefaultEncryptorWithMAC 使用 2-key Triple DES 作为默认加密器,并使用 SHA1 作为 MAC 的默认散列。分组密码在 CBC 模式下运行。密码是混搭的,而不是使用基于密码的密钥派生函数派生的。由于使用了基于时间和时钟的盐,每次运行 DefaultEncryptorWithMAC 都会产生不同的结果。

我正在尝试用另一个库读取这个加密字符串,并且真的很挣扎,即执行 DefaultDecryptorWithMAC 的等效操作(http://www.cryptopp.com/wiki/DefaultDecryptorWithMAC

如果我将我的密钥通过并在线 SHA1 加密,我得到的结果与上面的 Mash 函数不同吗?

根据上面的网页,它似乎表明它正在使用标准加密技术,我无法用其他任何东西来解密结果

希望这里有人有从这个库中解密这些函数结果的经验

提前致谢

4

1 回答 1

0

从评论:

我显然知道整个 salt/clock/mashing 的秘密密钥,这使我几乎不可能解密已使用上述方法加密的字符串。

如果您查看DefaultEncryptorWithMAC的代码(h 文件cpp 文件),您会看到输出内容不仅仅是在混合密码下加密的数据。因此,仅在 mash 函数中导出“密钥”并键入DES_EDE2密码并在 CBC 模式下操作它是不够的。

查看第 83 行的函数DefaultEncryptor::FirstPut(继承自ProxyFilter),其中写了序言。序言是盐和密钥检查。所以加密数据的布局是:

[SALT][KEYCHECK][ENCRYPTED DATA]

SALT 是根据DefaultHashModule::DIGESTSIZE; 但只写入了 8 个字节。然后,KEYCHECK 基于DefaultHashModule::DIGESTSIZE; 但只Default_BlockCipher::Encryption::BLOCKSIZE写了。

到了解密的时候,你去掉了盐并用它来重新推导 IV。您剥离密钥检查并验证您派生的密钥。然后,您使用派生密钥和 iv 对解密器进行密钥处理。

于 2014-12-15T03:16:08.023 回答