我读了一篇帖子:节点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代码中的^{} 返回的} 对象(一个
encrypted
是一个^{bytes
子类),它保存连接的(24字节)nonce和密文。注意EncryptedMessage
还具有属性nonce
和ciphertext
由于代码使用级联(Base64编码)数据,因此必须首先分离这些数据(在Base64解码之后),并且必须在^{} 中分别处理nonce和密文。请注意,tweet使用typedarray。对于Utf8或Base64之间的转换,可以使用tweetnacl util中的函数:
请注意,此代码段是纯JavaScript。在NodeJS环境中,必须将
nacl.util
替换为utils