我们有一个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();
TAGS:编程语言 js 密码加密 js eval 加密 js混淆加密 python 大数据 mongodb 数据存储
!如链接失效请在下方留言。本站所有资源均来源于网络,版权属于原作者!仅供学习参考,本站不对您的使用负任何责任。如果有侵权之处请第一时间联系我们删除,敬请谅解!