我正在尝试将一些PHP代码转换为Ruby。代码段位于此处:https://developer.2c2p.com/docs/read-payment-response-1(位于页面底部),我试图了解如何在Ruby中实现此功能。我已经知道了:
payment_response = params.dig(:paymentResponse)
cert_store = OpenSSL::X509::Store.new
my_cert = OpenSSL::X509::Certificate.new(File.read('config/twoctwop_keys/demo2.crt'))
signature = OpenSSL::PKCS7.new(File.read('config/twoctwop_keys/demo2.pem'))
signature.verify([my_cert], cert_store, payment_response, OpenSSL::PKCS7::NOVERIFY)
signature.data
但是,上述情况似乎出现以下错误:
ArgumentError:无法解析PKCS7:嵌套的asn1错误
从以下行:
OpenSSL::PKCS7.new(File.read('config/twoctwop_keys/demo2.pem'))
在未粘贴demo2.pem
文件的内容的情况下,似乎可以包含多个证书,因为我可以看到:
Bag Attributes
Microsoft Local Key set: <No Values>
localKeyID: 01 00 00 00
friendlyName: omitted
Microsoft CSP Name: Microsoft RSA SChannel Cryptographic Provider
Key Attributes
X509v3 Key Usage: 10
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,3A2DD8162BC67378
..... key......
-----END RSA PRIVATE KEY-----
Bag Attributes
localKeyID: 01 00 00 00
friendlyName: demo2.2c2p.com
subject=/C=SG/ST=Singapore/L=Singapore/O=xx Pte Ltd./OU=xxx IT/CN=xxx
issuer=xxxx
-----BEGIN CERTIFICATE-----
....key....
-----END CERTIFICATE-----
Bag Attributes: <Empty Attributes>
subject=/CN=SinaptIQ CA
issuer=/CN=SinaptIQ CA
-----BEGIN CERTIFICATE-----
....key....
-----END CERTIFICATE-----
这里有我不了解的东西吗?
答案 0 :(得分:0)
这很简单:
payment_response = params.dig(:paymentResponse)
certificate = OpenSSL::X509::Certificate.new(File.read('config/twoctwop_keys/demo2.crt'))
private_key = OpenSSL::PKey::RSA.new(File.read('config/twoctwop_keys/demo2.pem'), <passphrase>)
body = OpenSSL::PKCS7.new(Base64.strict_decode64(payment_response))
Hashie::Mash.new(Hash.from_xml(@body.decrypt(@private_key, @certificate))['PaymentResponse'])