我正在将python scrapy代码转换为我的电子应用程序的节点js代码 但那些python加密模块似乎与javascript加密模块不匹配。有什么建议可以正确转换此代码吗?
heroku config
节点代码还返回十六进制加密的代码 但是结果不等于期望值
我认为这可能是PKCS1填充问题,但不确定这一点
import base64
import binascii
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
def get_hex_rsa_encrypted_data(mod, exp, id, pw):
mod = int(mod, 16)
exp = int(exp, 16)
rsa_key = RSA.construct((mod, exp))
rsa = PKCS1_v1_5.new(rsa_key)
raw_encrypted_user_id = rsa.encrypt(str.encode(id))
raw_encrypted_password = rsa.encrypt(str.encode(pw))
hex_encrypted_user_id = binascii.hexlify(raw_encrypted_user_id).decode()
hex_encrypted_password = binascii.hexlify(raw_encrypted_password).decode()
# print(hex_encrypted_user_id)
# print(hex_encrypted_password)
return {'enc_user_id': hex_encrypted_user_id, 'enc_user_pw': hex_encrypted_password}
我的节点代码
5a615e468d2829c8146e241907e2dd38d08a02955c9ae4fd7992bb97a6b7d82dc12ed44e530837e4d20005180ff59faca6d6ae6948fe1f3081ccb3fbdd029f32bfb2957a06e058918bc527fb5b4c78d0da4a27829e98d95b5a36872a18ebe0e989f5b6efff931a4b48e6fd89355b86c9d6e633c0dd65df439853a843f0955afe
更新:
我找到了javascript(节点)解决方案。
'node-forge'软件包为我解决了这个问题。 https://www.npmjs.com/package/node-forge
const rsa = require('node-bignumber')
const ba = require('binascii')
function getHexRsaEncryptedData(pub_mod, pub_exp, user_id, password) {
pub_mod = pub_mod.toString(16);
pub_exp = pub_exp.toString(16);
const pub = new rsa.Key();
pub.setPublic(pub_mod, pub_exp);
const enc_id = pub.encrypt(user_id, 'RSAES-PKCS1-V1_5');
const enc_pw = pub.encrypt(password, 'RSAES-PKCS1-V1_5');
let hex_enc_id = ba.hexlify(enc_id);
let hex_enc_pw = ba.hexlify(enc_pw);
hex_enc_id = Buffer.from(hex_enc_id, 'hex').toString();
hex_enc_pw = Buffer.from(hex_enc_pw, 'hex').toString();
console.log(hex_enc_id);
console.log(hex_enc_pw);
return {
"enc_user_id": hex_enc_id,
"enc_user_pw": hex_enc_pw
};
}
node-forge添加了用于加密的正确pkcs1填充
问题解决了!