简体   繁体   English

我在后端使用 PyNacl 进行数字签名。 我应该在前端使用哪个库?

[英]I am using PyNacl at the backend for digital signatures. Which library should I use at frontend?

I have created an API that validates data based on PyNacl at the backend.我创建了一个 API,它在后端基于 PyNacl 验证数据。 I am accepting length 64 hexadecimal-encoded sender and recipient account numbers for my simple Crypto API and validating the signature based on PyNacl library.我为我的简单 Crypto API 接受长度为 64 的十六进制编码的发件人和收件人帐号,并基于 PyNacl 库验证签名。 I was wondering what Javascript library to use on my frontend so that the data I send using my React-based, it is coherent to my backend API.我想知道在我的前端使用什么 Javascript 库,以便我使用基于 React 发送的数据与我的后端 API 保持一致。 I looked at tweetnacl , but am not sure if they have the same working pattern.我查看了tweetnacl ,但不确定它们是否具有相同的工作模式。 Can you give me some information about whether or not I can use tweetnacl, or will I have to create a python script that uses PyNacl to generate Signing Keys / Verify Keys, and signs the message?你能给我一些关于我是否可以使用 tweetnacl 的信息,还是我必须创建一个 python 脚本,该脚本使用 PyNacl 生成签名密钥/验证密钥,并对消息进行签名?

Thanks.谢谢。

Update!更新! We have been successful at passing files between TweetLua and PyNaCl, The person writing the Lua side of the code had an "off by one" error (silly? but aren't most of our errors.), Once we got the right pieces in their proper places.我们已经成功地在 TweetLua 和 PyNaCl 之间传递文件,编写代码的 Lua 端的人有一个“off by one”错误(愚蠢?但不是我们的大部分错误。),一旦我们得到了正确的部分他们应有的地方。 it was a snap.这是一个瞬间。

I know the use of Lua instead of JavaScript isn't a perfect match to this question, but I hope that people who find this will get some use all the same.我知道使用 Lua 而不是 JavaScript 不是这个问题的完美匹配,但我希望发现这个问题的人会得到一些相同的使用。 It boils down to: Yes, TweetNaCl and PyNaCl are compatible, just as you'd expect.归结为:是的,正如您所期望的那样,TweetNaCl 和 PyNaCl 是兼容的。

Important element in this process:此过程中的重要元素:

  • TweetNaCl takes the MAC, Nonce, P_key, and K_key as separate arguments when boxing and unboxing. TweetNaCl 在装箱和拆箱时将 MAC、Nonce、P_key 和 K_key 作为单独的 arguments。
  • PyNaCl does NOT. PyNaCl 没有。 Capture the sender's P_key, import it, make a box, and then pass the remaining cyphertext through as a unit.捕获发送者的P_key,导入,做一个盒子,然后将剩余的密文作为一个单元传递。 PyNaCl will pull out the Nonce and MAC for you. PyNaCl 将为您提取 Nonce 和 MAC。

Lua encryption: 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 decryption: 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'))

Previous response: So far as I can tell, no, TweetNaCl and PyNaCl are not compatible.以前的回应:据我所知,不,TweetNaCl 和 PyNaCl 不兼容。 My group is attempting to encrypt a file with c# TweetNaCl and decrypt with python, and I always end up with a general nacl.exceptions.CryptoError: An error occurred trying to decrypt the message .我的小组正在尝试使用 c# TweetNaCl 加密文件并使用 python 解密,我总是以一般nacl.exceptions.CryptoError: An error occurred trying to decrypt the message结束。 If you / someone else figures out a solution, I'd love to hear it!如果您/其他人想出了一个解决方案,我很想听听!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该使用哪个 javascript 库? - Which javascript library should I use? 我正在尝试使用套接字将我的 HTML 前端连接到节点 js 后端 - I am trying to connection my HTML frontend to node js backend using sockets 我应该使用哪个JavaScript库进行客户端连字? - Which JavaScript library should I use for client-side hyphenation? 我应该为 ReactJs 中的烛台图使用哪个图表模块/库? - Which chart module/library should I use for Candlestick graphs in ReactJs? 今天(2011年)应该使用哪个JavaScript hashchange / history库? - Which JavaScript hashchange/history library should I use today (2011)? 我可以在前端使用后端的OAuth令牌吗? - Can I use an OAuth token from backend in my frontend? 我有一个 NodeJS 后端我正在开发与 ReactJS 前端一起工作,但我不断收到 500 错误 - I have a NodeJS backend I am developing to work alongside a ReactJS frontend but I keep getting a 500 error 我没有从 angular 前端到达后端 controller 中的 api 路由。 我在做什么不正确? - I am not hitting my api route in backend controller from angular frontend. What am I doing incorrect? 我应该将哪些特效库与GWT集成? - Which effects library should I integrate with GWT? 我应该如何直接从前端或通过后端获取 google api 响应? - how should I fetch the google api responses, from directly frontend or going through backend?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM