我必须在Javascript中加密某些内容并使用RSA在Python中解密。我决定将pidCrypt https://www.pidder.com/pidcrypt/?page=demo_rsa-encryption用于前者,将Pycrypto库用于后者。但是当我尝试使用pyCrypto库解密时(使用PKCS v1.5模块),我收到此错误"密文长度不正确。"可能导致这种情况的原因是什么方法可以解决这个问题?这是我的代码:
加密JS:
var crypted = encrypt("Hello");
var base64Crypted = pidCryptUtil.encodeBase64(crypted);
我将加密数据写入文件encr.txt,然后在python中读取和解密,如下所示:
解密Python:
mk = open('mykey.pem','r')
myKey = RSA.importKey(mk.read())
cipher = PKCS1_v1_5.new(myKey)
ef = open('encr.txt','r')
encrData = base64.b64decode(ef.read())
decr = cipher.decrypt(encrData, sentinel)
这是我的关键:
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAsKMVf7W1ZFNYrgROnDsiYdKTy4f7JK7m2BIUq/KrXVlSjsd1
hpzT7aLPv4veUxljlySG3OWIIuGwFcdf8xE5tvp1jTgG7w7yL27TYOPI53DhSfOH
Ma/8DUHfc93lg0PYAgBL35g8xVnCL0t8/erVvOjafrOGrmtj4gN2zF95LxdTv8UB
ldLR2rIWJ+t+ythqVdf1O7QEc9NR6fslzqJT0MObC7xF7VDS6xO+ZR/8zK1EGILr
15LDsdZQ3xp1rcSu3/375Yw8CUEwvaUjnSaaVSmrnzCEgRNzcGPxcvavic5T5v+E
lhSchA+waxxMgkocv9gKRfDxpkNiP9FwShfi/QIDAQABAoIBAAXIAK8iYmJ7O97O
M0h3wwNSohW6SKUFuHZ/+P87vRIcF1CnBJi4T1NqzcPmLwunPBQ5VNSsCo2ht2Kv
GNvax3nOrsRFfiya36BfzzlPm0a/8g+6kcU1jllcu5SOQbHPKaW/HrydvfdznaBw
pxGkewRo1qXo+FKDH0zJcjz0LwI0gkvwTSBkePtkhheoVkYKymAjJM3BTYLgb0CB
2kUua7sfwczT0KIM3v4CnDOs5E+s7uwxRwve6zl145fCBTrj8kEfHJAALW8rsNXt
ASQJEGhBCGPPW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