我正在使用 M2Crypto (0.22.6rc4)。我想使用engine_pkcs11
OpenSC 项目和 Aladdin PKI 客户端进行基于令牌的身份验证来加密和解密数据。
from M2Crypto import Engine, m2, RSA, BIO
slot_id = "slot_01"
pin = "password"
dynamic = Engine.load_dynamic_engine("pkcs11", "/usr/lib/ssl/engines/libpkcs11.so")
pkcs11 = Engine.Engine("pkcs11")
pkcs11.ctrl_cmd_string("MODULE_PATH", "/usr/lib/watchdata/ICP/lib/libwdpkcs_icp.so")
pkcs11.init()
r = pkcs11.ctrl_cmd_string("PIN", pin)
pubkey = pkcs11.load_public_key(slot_id, pin)
priv = pkcs11.load_private_key(slot_id, pin)
enc = pubkey.get_rsa().public_encrypt("teste", RSA.pkcs1_oaep_padding)
dec = priv.get_rsa().private_decrypt(enc, RSA.pkcs1_oaep_padding)
print dec
出于某种原因,我可以加密数据,但是当尝试解密时,我得到了一个 RSA_pub 的实例并且这个错误:
File "pkcs11.py", line 14, in <module>
dec = priv.get_rsa().private_decrypt(enc, RSA.pkcs1_oaep_padding)
File "/usr/lib/python2.7/dist-packages/M2Crypto/RSA.py", line 279, in private_decrypt
raise RSAError, 'RSA_pub object has no private key'
M2Crypto.RSA.RSAError: RSA_pub object has no private key
任何帮助,将不胜感激!