一个Python应用程序数据存储在MongoDB中的解密数据中解密

  我们有一个Python应用程序,将字符串作为加密的二进制数据存储在MongoDB中,它使用了

  from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305

  在NodeJS方面,我一直不知道如何解密数据,我有我们的盐,我们的密钥,但据我所知,没有IV,或者说python模块可能只是把所有这些隐藏在引擎盖下js 密码加密,因为python应用程序所要做的就是调用encrypt(value, salt) 和 decrypt(value, salt)

  Python:

   class ChaChaEncryptedStringField(EncryptedStringField):

    """
    A field which, given an encryption key and salt, will automatically encrypt/decrypt
    sensitive data to avoid needing to do this before passing in. This encryption
    method reliably produces a searchable string.
    """
    def __init__(self, key, salt, *args, **kwargs):
        """Initialize the ChaChaEncryptedStringField.
        Args:
            key (str) -
            salt (str) -
        """
        class Hook:
            def __init__(self, key, salt):
                self.salt = salt
                self.chacha = ChaCha20Poly1305(key)
            def encrypt(self, value):
                return self.chacha.encrypt(self.salt, value, None)
            def decrypt(self, value):
                return self.chacha.decrypt(self.salt, value, None)
        self.encryption_hook = Hook(b64decode(key), b64decode(salt))
        super(EncryptedStringField, self).__init__(*args, **kwargs)

  Javascript(这不是在工作,但接近)。

   const authTagLocation = data.buffer.length - 16;

    const ivLocation = data.buffer.length - 28;
    const authTag = data.buffer.slice(authTagLocation);
    const iv = data.buffer.slice(ivLocation, authTagLocation);
    const encrypted = data.buffer.slice(0, ivLocation);
    const decipher = crypto.createDecipheriv('chacha20-poly1305', keyBuffer, iv,{ authTagLength: 16 } );
    let dec = decipher.update(
      data.buffer, 'utf-8', 'utf-8'
    );
    dec += decipher.final('utf-8');
    return dec.toString();

  通过一些研究和试验,我克服了它抱怨IV不正确的问题,密钥长度也是正确的js 密码加密,但仍然得到乱码的数据。

  所以我实际上得到了下面的代码,但我不打算声称完全理解发生了什么。

  工作的Javascript(盐是从秘密中提取的,使用IV提取失败)。

   const authTagLength = 16

    const authTagLocation = data.buffer.length - authTagLength;
    const ivLocation = data.buffer.length - 16;
    const authTag = data.buffer.slice(authTagLocation);
    const iv = data.buffer.slice(ivLocation, authTagLocation);
    const encrypted = data.buffer.slice(0, ivLocation);
    const decipher = crypto.createDecipheriv('chacha20-poly1305', keyBuffer, saltBuffer,{ authTagLength: authTagLength } );
    let dec = decipher.update(
      encrypted, 'utf-8', 'utf-8'
    );
    dec += decipher.final('utf-8');
    return dec.toString();
文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/1407
0 评论
669

发表评论

!