[英]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.