[英]I am using PyNacl at the backend for digital signatures. Which library should I use at frontend?
我创建了一个 API,它在后端基于 PyNacl 验证数据。 我为我的简单 Crypto API 接受长度为 64 的十六进制编码的发件人和收件人帐号,并基于 PyNacl 库验证签名。 我想知道在我的前端使用什么 Javascript 库,以便我使用基于 React 发送的数据与我的后端 API 保持一致。 我查看了tweetnacl ,但不确定它们是否具有相同的工作模式。 你能给我一些关于我是否可以使用 tweetnacl 的信息,还是我必须创建一个 python 脚本,该脚本使用 PyNacl 生成签名密钥/验证密钥,并对消息进行签名?
谢谢。
更新! 我们已经成功地在 TweetLua 和 PyNaCl 之间传递文件,编写代码的 Lua 端的人有一个“off by one”错误(愚蠢?但不是我们的大部分错误。),一旦我们得到了正确的部分他们应有的地方。 这是一个瞬间。
我知道使用 Lua 而不是 JavaScript 不是这个问题的完美匹配,但我希望发现这个问题的人会得到一些相同的使用。 归结为:是的,正如您所期望的那样,TweetNaCl 和 PyNaCl 是兼容的。
此过程中的重要元素:
Lua 加密:
local function main(flag, files, keys)
local pt = chunkpt(flag, files) # We broke large files down
files.fout_size = companyfilesize(flag, pt)
files.fout = assert(io.open(flag.outfile, "wb"))
local current = files.fout:seek()
files.fout:seek("set", files.fout_size - 1)
files.fout:write("x")
files.fout:seek("set", current)
local err
local ct = {}
local nonce = {}
local mac = {}
local root
local nonceroot
local macroot
local n = #pt
for i = n, 1, -1 do
nonce[i] = nacl.randombytes(NONCE_LEN)
if i == n then
ct[i], err = nacl.box(pt[i], nonce[i], keys.p_rx, keys.k)
if err ~= nil then error("boxing error") end
else
ct[i], err = nacl.box(pt[i] .. nonce[i + 1] .. mac[i + 1], nonce[i],
keys.p_rx, keys.k)
if err ~= nil then error("boxing error") end
end
mac[i] = ct[i]:sub(1, MAC_LEN)
ct[i] = ct[i]:sub(MAC_LEN + 1, -1)
end
files.fout:seek("set", 0)
local header = header_info
files.fout:write(header)
files.fout:write(keys.p_tx)
files.fout:write(nonce[1])
files.fout:write(mac[1])
files.fout:write(ct[1])
files.fin:close()
files.fout:close()
return 0
end
Python解密:
def decrypt_box():
with open("encrypted_file.companybox", 'rb') as f:
header = f.read(16) # We use this for internal info
senderPubKey = f.read(32)
cyphertext = f.read()
f.close()
# Import the secret key for use in the decryption
imported_private_key = nacl.public.PrivateKey(BOB_SECRET_KEY)
# Import the public key we just found in the file
imported_public_key = nacl.public.PublicKey(senderPubKey)
# Make a box with the 2 keys
plain_box = Box(imported_private_key, imported_public_key)
# Pass the remaining text (that includes the Nonce and MAC) to decode
plain = plain_box.decrypt(cyphertext)
print(plain.decode('utf-8'))
以前的回应:据我所知,不,TweetNaCl 和 PyNaCl 不兼容。 我的小组正在尝试使用 c# TweetNaCl 加密文件并使用 python 解密,我总是以一般nacl.exceptions.CryptoError: An error occurred trying to decrypt the message
结束。 如果您/其他人想出了一个解决方案,我很想听听!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.