Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取公钥字符串并将其转换为Pycryptodome密钥(Python)_Python_Python 3.x_Public Key Encryption_Pycryptodome - Fatal编程技术网

如何获取公钥字符串并将其转换为Pycryptodome密钥(Python)

如何获取公钥字符串并将其转换为Pycryptodome密钥(Python),python,python-3.x,public-key-encryption,pycryptodome,Python,Python 3.x,Public Key Encryption,Pycryptodome,我有一个公钥,但是,我不知道如何将其转换为Pycryptodome上的密钥。我一直在使用我找到的代码 但是,使用key64作为公钥,我会得到ValueError:Unexpected DER tag。在Python3.6中使用Pycryptodome是否有更好的方法来实现这一点?我们需要以下模块: import os from Cryptodome.Hash import SHA256 from Cryptodome.PublicKey import RSA from Cryptodome.Si

我有一个公钥,但是,我不知道如何将其转换为Pycryptodome上的密钥。我一直在使用我找到的代码

但是,使用key64作为公钥,我会得到
ValueError:Unexpected DER tag
。在Python3.6中使用Pycryptodome是否有更好的方法来实现这一点?

我们需要以下模块:

import os
from Cryptodome.Hash import SHA256
from Cryptodome.PublicKey import RSA
from Cryptodome.Signature import pkcs1_15
我们有一个plantext:

message = b'hello!'
从纯文本中查找哈希:

h = SHA256.new(message)
生成随机密钥:

key = RSA.generate(1024, os.urandom)
创建签名:

signature = pkcs1_15.new(key).sign(h)
最后使用公钥:

pub_key = key.publickey()
检查函数将如下所示:

 def sign(message, pubkey, signature):
      h = SHA256.new(message)
      try:
          pkcs1_15.new(pubkey).verify(h, signature)
          print('Success!')
      except ValueError:
          print('Invalid signature!')

试着检查Pycryptodome的源代码,看看是否可以重用一些代码。如果我知道我在做什么,我会的,但说实话,我对RSA和其他类型的加密不太熟悉。我知道它是如何工作的等等,然而,我只需要把它作为我正在做的更大项目的一部分来解决。然而,如果有人想对RSA进行更为不确定的解释,我们将不胜感激。
 def sign(message, pubkey, signature):
      h = SHA256.new(message)
      try:
          pkcs1_15.new(pubkey).verify(h, signature)
          print('Success!')
      except ValueError:
          print('Invalid signature!')