代码之家  ›  专栏  ›  技术社区  ›  kramer65

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

  •  0
  • kramer65  · 技术社区  · 6 年前

    我有一个代码库,它在数据库中存储了一些敏感数据。在将数据存储到数据库之前,我使用 this Crypto library ( docs 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 接住。我已经用一些示例代码手动尝试过了,但是我总是得到 .

    我假定数据是损坏的,但我不确定是否是这样。

    该记录位于一个表的中间,表中有15000条记录,这些记录都很好,代码的这一部分已经很久没有突然变化了。

    0 回复  |  直到 6 年前
        1
  •  0
  •   Scott Arciszewski    5 年前

    我假定数据是损坏的,但我不确定是否是这样。

    几乎可以肯定。最主要的问题是,如果记录太大,无法放入现场,MAC被截断。

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

    安装defuse/php加密。编辑 vendor/defuse/php-encryption/src/Crypto.php disable the integrity checks inside of decryptInternal() 然后将明文转储到文件中。

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