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的加密方案被知道是嚴重破碎。
你可以使用僞造的JS端,它支持OAEP:https://github.com/digitalbazaar/forge#rsa – dlongley
@dlongley你可以給一個實現的參考嗎? – Tejas