【问题标题】:How do I take a Public Key String and turn it into a Pycryptodome key (Python)如何获取公钥字符串并将其转换为 Pycryptodome 密钥(Python)
【发布时间】:2017-06-01 00:31:08
【问题描述】:

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

keyDER = b64decode(key64)
seq = asn1.DerSequence()
seq.decode(keyDER)
keyPub = RSA.construct((seq[0], seq[1]))

print(keyPub.encrypt('test',"Unguessable"))

但是,使用 key64 作为 PublicKey,我得到了 ValueError: Unexpected DER tag。有没有更好的方法在 python 3.6 中使用 Pycryptodome 做到这一点?

【问题讨论】:

  • 尝试检查 Pycryptodome 的源代码,看看是否可以重用部分代码。
  • 如果我知道我在做什么,我会的,但说实话,我对 RSA 和其他类型的加密不太熟悉。我知道它是如何工作的等等,但是,我只需要把它作为我正在做的更大项目的一部分来解决。但是,如果有人想更不确定地解释 RSA,那将不胜感激。

标签: python python-3.x public-key-encryption pycryptodome


【解决方案1】:

我们需要这些模块:

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!')

【讨论】:

    猜你喜欢
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 2020-07-05
    • 1970-01-01
    • 2021-09-16
    • 2019-03-01
    相关资源
    最近更新 更多