在python 3.7中加密,在NODEJS 12中解码

在python 3.7中加密,在NODEJS 12中解码,python,node.js,encryption,Python,Node.js,Encryption,我读了一篇帖子:节点js和python之间的加密数据 我需要实现反向路径,用python创建加密并在节点中解码 在Python方面,我做到了: from nacl.secret import SecretBox from base64 import b64encode import nacl.secret import nacl.utils secret_key = bytes('_THIS_IS_MY_32_CHARS_SECRET_KEY_', 'utf8') message = byt

我读了一篇帖子:节点js和python之间的加密数据

我需要实现反向路径,用python创建加密并在节点中解码

在Python方面,我做到了:

from nacl.secret import SecretBox
from base64 import b64encode
import nacl.secret
import nacl.utils

secret_key = bytes('_THIS_IS_MY_32_CHARS_SECRET_KEY_', 'utf8')
message =  bytes('Some Italians hate wine','utf-8')

nonce = nacl.utils.random(24)

box = SecretBox(secret_key)
encrypted = box.encrypt(message,nonce)
ctext = encrypted.ciphertext
print(ctext)

plaintext = box.decrypt(encrypted)
print(plaintext.decode('utf-8'))

print(b64encode(nonce))
print(b64encode(encrypted
在节点中:

const nacl = require('tweetnacl');
const utils = require('tweetnacl-util');
const secretKey = Buffer.from('_THIS_IS_MY_32_CHARS_SECRET_KEY_', 'utf8');
const nonce = utils.decodeBase64('KRmiqOFUN1HklmPZgbd0BINNDDCu3dyB');
      
const encryptedMessage = utils.decodeBase64('KRmiqOFUN1HklmPZgbd0BINNDDCu3dyB/s4tdTjcw65K6Lr5N797+7zoLm9WClCXIDNLAqrNGwF2MybtJu+U');
    
const originalMessage = nacl.secretbox.open(
    encryptedMessage,
    nonce,
    secretKey
);
结果为空

进行此集成的正确方法是什么?

在PyNaCl代码中,加密返回的是一个对象a bytes子类,该子类保存连接的24字节nonce和密文。请注意,EncryptedMessage还具有nonce和ciphertext属性

由于TweetNaCl代码使用串联的Base64编码数据,因此这些数据必须在Base64解码后首先分离,并且必须在中单独处理nonce和密文。请注意,TweetNaCl使用TypedArrays。对于Utf8或Base64之间的转换,可以使用tweetnacl util的函数:

const secretKey=nacl.util.decodeUTF8“这是我的32个字符,是秘密密钥”; const encryptedMessage=nacl.util.decodeBase64'KRmiqOFUN1HklmPZgbd0BINNDDCu3dyB/s4tdTjcw65K6Lr5N797+7ZOLM9WCLCXindlAQRNGWF2MYBTJU+U'; //分开的nonce和密文 const nonce=encryptedMessage.slice0,24 常量密文=encryptedMessage.24 //解密 const originalMessage=nacl.secretbox.openciphertext,nonce,secretKey; const decryptedText=nacl.util.encodeUTF8originalMessage; console.logdecryptedText;//有些意大利人讨厌葡萄酒