0

I have a public key, however, I'm not sure on how to turn that into a key on Pycryptodome. I've been using this code that I found here

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

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

With key64 as the PublicKey, however, I get ValueError: Unexpected DER tag. Is there a better way of doing this in python 3.6 with Pycryptodome?

Nate
  • 51
  • 7
  • Try inspecting the source of Pycryptodome and see if you can reuse some of the code. – Jacob Birkett Jun 01 '17 at 00:48
  • I would if I knew what I was doing, but truth be told, I'm not too familiar with RSA and other types of encryption. I know how it works and etc, however, I just need to figure this out as part of bigger project I'm doing. However, if somone would like to explain RSA a little more indefinably, that would be greatly appreciated. – Nate Jun 01 '17 at 00:57

1 Answers1

0

We need these modules:

import os
from Cryptodome.Hash import SHA256
from Cryptodome.PublicKey import RSA
from Cryptodome.Signature import pkcs1_15

We have a plantext:

message = b'hello!'

Find a hash from plaintext:

h = SHA256.new(message)

Generate random key:

key = RSA.generate(1024, os.urandom)

Create a signature:

signature = pkcs1_15.new(key).sign(h)

And finally take a public key:

pub_key = key.publickey()

The check func will look like this:

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