我正在尝试获取我创建的隐藏服务的公共密钥。有两种方法,一种是我拥有的私钥,另一种是访问隐藏的服务描述符并采用公钥字段。
我写了一个python脚本来测试两者。但是,它们是不同的!
这是脚本(pkey是之前从Tor控制器生成的测试私钥)
import stem
from stem.control import Controller
from Cryptodome.PublicKey import RSA
from base64 import b64encode
controller = Controller.from_port(port=9051)
controller.authenticate()
pkey="MIICXQIBAAKBgQCsqrc5jvobvNiZ4g0IdoxkQmYtFtRqGMeGCfh8IZ1s7IJ2owzrZ+8GE+HJhBr1YYFJ/9UegD9APfnybIfgmVY+PCxNlE16uMreigogAqpN4Vp8aic0s/6sXbf9EyHkafcHdMX3N3LUYhOQZfI02zNjUTRSkHp9jS+MFcWwOeYMzwIDAQABAoGANX5uZZa2RgGr2YD/KLARrr4jibSY+oXSyuQr58lCTIy5nqfFSOlaVDe0rAoRkBQ1SgfIrzpsXirVVRXqwtq+rOcRjVT4cRZnb3MNRqSFiqSocL2/TkJg97pXKfGgLkoopsGfvrgXmlpI03w8TMwbcSHvObUkC9xKqcKABtdYfQECQQDSQgyvh7YnQ2qdYeFYc9f9kT3XaUjYMgel7Zq8GAQEmsZ3YqW/vDmNVYsnQj/ZHyS/VwKFyE66jw/po+p8bBKZAkEA0jsX3AZyrd24lVkK/5gcMQLfRuOyJDtEjNXUNWxS2axUoVDBIRcArsa5IKG4yRYYRgIVSEDLxGxciTWgU5MjpwJAShTRiWskp/Ld9rPNBTBT0tjA1EwZSWtfPHxaP5mqmMC72sacJdE46e5RCDUUb/jEJtKOZfqsmpjbf3Oso8gkSQJBAJkBrUHT548nOfpojhqsSIhbQMnFWpgIEsI1tncTWdFXHVky/vzoUvThaUeYrHaeABij1314u1VAULEEOKXbBy0CQQDJJeDolidHG8UkWISq2jP6QIkpqOx3E6zxMNXAKwfz/O+yyanNtL1+MpFbk0H5n1fUhSDjR5daziUcmU93NOW9"
response = controller.create_ephemeral_hidden_service([9000, 9001],key_type ='RSA1024',key_content =pkey,await_publication=True)
onionAddr = response.service_id
#privKey = pkey#response.private_key
print('onion: '+onionAddr)
print("\nPrivate Key: ")
print(pkey)
print("================")
pkey ="-----BEGIN RSA PRIVATE KEY-----\n"+pkey+"\n-----END RSA PRIVATE KEY-----"
privKeyObj = RSA.importKey(pkey)
#pubKey = b64encode(privKeyObj.publickey().exportKey('DER',pkcs=8))
pubKey = privKeyObj.publickey().exportKey('PEM',pkcs=1).decode()
print(pubKey)
print("\n\n\n HSD response:")
print(controller.get_hidden_service_descriptor(onionAddr+".onion"))
我从私钥那里得到的公钥是:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsqrc5jvobvNiZ4g0IdoxkQmYt
FtRqGMeGCfh8IZ1s7IJ2owzrZ+8GE+HJhBr1YYFJ/9UegD9APfnybIfgmVY+PCxN
lE16uMreigogAqpN4Vp8aic0s/6sXbf9EyHkafcHdMX3N3LUYhOQZfI02zNjUTRS
kHp9jS+MFcWwOeYMzwIDAQAB
-----END PUBLIC KEY-----
和隐藏的服务描述符中的一个是:
-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAKyqtzmO+hu82JniDQh2jGRCZi0W1GoYx4YJ+HwhnWzsgnajDOtn7wYT
4cmEGvVhgUn/1R6AP0A9+fJsh+CZVj48LE2UTXq4yt6KCiACqk3hWnxqJzSz/qxd
t/0TIeRp9wd0xfc3ctRiE5Bl8jTbM2NRNFKQen2NL4wVxbA55gzPAgMBAAE=
-----END RSA PUBLIC KEY-----
进行一些搜索后,我发现RSA的密钥格式不同。我试图使格式匹配,但是我没有运气!
如何解决这个问题?
答案 0 :(得分:0)
我错了,从一开始它们是相同的,只是编码的版本不同。 PyCryptoDome可以处理这种编码差异。 如果两个键都导入到键对象中并进行了比较,则它们是相等的