如何调试/研究损坏的加密数据?

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

我有一个代码库,可以在数据库中存储一些敏感数据。在将数据存储到DB中之前,我先使用this Crypto librarydocs here)加密了数据。

为了解密它,我使用以下内容

use \Defuse\Crypto\Crypto;
use \Defuse\Crypto\Exception as Ex;

// The following is inside a class, but for clarity I only copy pasted this part    

try {
    return Crypto::decrypt($aStr, Crypt::$cryptoKey);

} catch (Ex\InvalidCiphertextException $ex) { // VERY IMPORTANT
    // Either:
    //   1. The ciphertext was modified by the attacker,
    //   2. The key is wrong, or
    //   3. $ciphertext is not a valid ciphertext or was corrupted.
    // Assume the worst.
    die('The ciphertext has been tampered with! Message:'.$ex->getMessage());
}
// I've got some more catch blocks here but they're not relevant for this question

该代码很好用,但是今天我偶然发现了一个数据库记录,该记录使整个事情都死于InvalidCiphertextException捕获。我已经用一些示例代码手动尝试过,但是我总是得到InvalidCiphertextException

我认为数据已损坏,但不确定是否是这种情况。

[记录位于表的中间,包含15000条记录,这些记录都很好,并且年龄的那部分代码没有突然改变。

我还能做更多调查(甚至解密)的事情吗?还是我可以找到有关此记录的更多信息或进一步调试此信息的方法?

欢迎所有提示!

php encryption corruption corrupt data-integrity
1个回答
0
投票

我认为数据已损坏,但不确定是否是这种情况。

几乎可以肯定。可能出错的主要问题是,如果记录太大而无法放入该字段,并且MAC被截断了。

如何调试/调查损坏的加密数据?

获取密文/密钥的副本,将其放置在受限的环境中。

安装defuse / php-encryption。编辑vendor/defuse/php-encryption/src/Crypto.phpdisable the integrity checks inside of decryptInternal(),然后将纯文本转储到文件中。

这将使您恢复不良记录。注意:不保证这是您的用户提供的没有完整性标记的记录。

现在,请确保您的列足够大以容纳所需的密文,然后重新加密并覆盖它。

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