一个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):

  1. """
  2. A field which, given an encryption key and salt, will automatically encrypt/decrypt
  3. sensitive data to avoid needing to do this before passing in. This encryption
  4. method reliably produces a searchable string.
  5. """
  6. def __init__(self, key, salt, *args, **kwargs):
  7. """Initialize the ChaChaEncryptedStringField.
  8. Args:
  9. key (str) -
  10. salt (str) -
  11. """
  12. class Hook:
  13. def __init__(self, key, salt):
  14. self.salt = salt
  15. self.chacha = ChaCha20Poly1305(key)
  16. def encrypt(self, value):
  17. return self.chacha.encrypt(self.salt, value, None)
  18. def decrypt(self, value):
  19. return self.chacha.decrypt(self.salt, value, None)
  20. self.encryption_hook = Hook(b64decode(key), b64decode(salt))
  21. super(EncryptedStringField, self).__init__(*args, **kwargs)

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

   const authTagLocation = data.buffer.length - 16;

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

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

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

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

   const authTagLength = 16

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

发表评论

!