需要用到的 Package:

透過私鑰簽署:

from Crypto.PublicKey import RSA

透過公鑰驗證:

import sshpubkeys

載入私鑰簽署:

with open('id_rsa', 'r') as key_file:
    key = RSA.importKey(key_file.read())
string_data = 'Hello'
signature = key.sign(string_data.encode('utf8'), None)

載入公鑰驗證:

with open('id_rsa.pub', 'r') as key_file:
    pub_key = sshpubkeys.SSHKey(key_file.read())
string_data = 'Hello'
pub_key.rsa.verify(string_data.encode('utf8'), signature)

Share Your Thought