我正在开发一个原型,因此需要在 Chrome 扩展和 Python 服务器之间使用 RSA。
到目前为止我计划使用https://sourceforge.net/projects/pidcrypt/ and https://www.dlitz.net/。然而,虽然我可以按照文档进行解密和加密,但我无法解密彼此的消息。
有人可以建议可以互操作的库,或者让我知道我是否对这个库做错了什么吗?
根据我的研究,pidder 使用 RSA PKCS#1 加密式填充(类型 2)。通过谷歌搜索,我发现这是 PyCrypto 称为 PKCS1_OAEP 的类型。我不太确定,但我已经尝试过标准和另外两个。
非常感谢您的帮助!
Javascript 库 (pidCrypt) 使用 PKCS#1 v1.5 进行 RSA 加密,而不是 OAEP。
PyCrypto 支持这一点(参见here)。这是加密的示例:
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA
message = 'To be encrypted'
h = SHA.new(message)
key = RSA.importKey(open('pubkey.der').read())
cipher = PKCS1_v1_5.new(key)
ciphertext = cipher.encrypt(message+h.digest())
并解密:
from Crypto.Hash import SHA
from Crypto import Random
key = RSA.importKey(open('privkey.der').read())
dsize = SHA.digest_size
sentinel = Random.new().read(15+dsize) # Let's assume that average data length is 15
cipher = PKCS1_v1_5.new(key)
message = cipher.decrypt(ciphertext, sentinel)
digest = SHA.new(message[:-dsize]).digest()
if digest==message[-dsize:]: # Note how we DO NOT look for the sentinel
print "Encryption was correct."
else:
print "Encryption was not correct."
请注意,已知 PKCS#1 v1.5 加密方案是badly broken.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)