有没有人知道如何在cybersource中加密卡号?
我尝试使用在线RSAOAEP加密工具来加密我的,但是我得到了这样的响应
{
"responseStatus": {
"status": 400,
"reason": "DECRYPTION_ERROR",
"message": "Cannot decrypt PAN (RsaOaep256): data hash wrong",
"correlationId": null,
"details": [],
"_embedded": {}
},
"_links": {
"self": null,
"documentation": [],
"next": []
}
}对于我这样的新手来说,这些文档似乎还不够
发布于 2020-03-25 01:07:40
我已经能够让Flex API工作了。但是你一直在使用什么样的SDK呢?我已经用React Native v0.61.5、Typescript和多个加密库实现了它:react-native-crypto、isomorphic-webcrypto、crypto-js、js-base64和buffer。但基本上这可以在任何Javascript框架上完成。
我猜想您已经让/Keys请求工作了,并且我猜想您已经指定了RsaOaep256的encryptionType。
下一步是导入上一步收到的JSON web密钥(JWK),并使用导入的密钥对卡号进行加密。
导入JWK
import webcrypto from "isomorphic-webcrypto"
const importKey = async (jsonWebKey: any) => {
return webcrypto.subtle.importKey(
"jwk",
{
...jsonWebKey,
alg: "RSA-OAEP-256",
ext: true,
},
{
name: "RSA-OAEP",
hash: "SHA-256",
},
false,
["encrypt"],
)
}加密卡号
import { Buffer } from "buffer"
import webcrypto from "isomorphic-webcrypto"
import { Base64 } from "js-base64"
const encryptCardNumber = async (cardNumber: string, jsonWebKey: any): Promise<T> = {
const cardNumberBuffer = Buffer.from(cardNumber)
const publicKey = await importKey(jsonWebKey, "encrypt")
const encryptedCardNumberBuffer = await webcrypto.subtle.encrypt(
{
name: "RSA-OAEP",
hash: "SHA-256",
},
publicKey,
cardNumberBuffer
)
return Base64.btoa(String.fromCharCode.apply(null, new Uint8Array(encryptedCardNumberBuffer)))
}此函数的结果可以在cardInfo下的请求体中以cardNumber的形式直接传递。
在此之后,您将收到token、signature、signedFields和其他一些字段。您应该根据签名验证收到的值,以确保这些值没有被篡改。
验证签名
这个非常简单,我们只需要在der/publicKey下可以找到的/Keys请求中的公钥。
import crypto from "react-native-crypto"
const verifySignature = (publicKey: string, signature: string, signedFields: string, data: any): booelan => {
const dataToVerify = data.signedFields.split(",").map(field => data[field]).join(",")
const verificationKey = `-----BEGIN PUBLIC KEY-----\n${publicKey}\n-----END PUBLIC KEY-----`
return crypto.createVerify("RSA-SHA512").update(dataToVerify).verify(verificationKey, signature, "base64")
},我希望这能帮助你或其他在实现CyberSource的Flex API方面遇到困难的人,因为这已经让我付出了代价……
https://stackoverflow.com/questions/60590314
复制相似问题