doushitang4276 2015-09-28 17:22
浏览 104

Python M2Crypto替代PHP

Folks,

I am doing an API integration for fetching some data from my Vendor database. But they have this Python script for doing an integration. I just wanted to have a head's up from you guys to help me out with how I can do the same with PHP

import M2Crypto
import hashlib

payload_template = """<payload><apiVersion>2.0</apiVersion><vendorId>jinna</vendorId><txnId>UTFTVAIAD</txnId><emailId>{encrypted_email_id}</emailId><destination>fetchData</destination><myAmount>100000</myAmount><Duration>24</Duration><Type>Home</Type><returnUrl>https://mydomainname.com</returnUrl></payload>""" email_id = "jinna@mywebsite.com"

Read public key. This particular piece of code is to read the public key that these guys have shared with me.

public_key = M2Crypto.RSA.load_pub_key('/home/jinna/work/insights-client-keys/clients/mywebsite/public_key')

Encrypt email_id using Public Key and encode in Hex (Base16)

encrypted_email_id = public_key.public_encrypt(email_id, M2Crypto.RSA.pkcs1_padding).encode("hex")

Create payload.

payload = payload_template.format(encrypted_email_id = encrypted_email_id)

Generate SHA1 of payload and encode in Hex (Base16)

digest = hashlib.sha1(payload).hexdigest()

Read private key.

private_key = M2Crypto.RSA.load_key('/home/jinna/work/insights-client-keys/clients/mywebsite/private_key')

Encrypt digest using private key and encode in Hex (Base16)

signature = private_key.private_encrypt(digest, M2Crypto.RSA.pkcs1_padding).encode("hex")

Print payload and signature.

print payload, signature

Will appreciate your support.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度