【问题标题】:CyberSource card number encryption (RSA-OAEP-256)Cyber​​Source 卡号加密 (RSA-OAEP-256)
【发布时间】:2020-03-25 18:21:30
【问题描述】:

这里有谁知道我如何在网络资源中加密卡号?

我尝试使用在线 RSAOAEP 加密工具加密我的,但我得到了这个响应

{
    "responseStatus": {
        "status": 400,
        "reason": "DECRYPTION_ERROR",
        "message": "Cannot decrypt PAN (RsaOaep256): data hash wrong",
        "correlationId": null,
        "details": [],
        "_embedded": {}
    },
    "_links": {
        "self": null,
        "documentation": [],
        "next": []
    }
}

对于像我这样的新手来说,文档似乎还不够

【问题讨论】:

  • 如果对你有帮助的话,NPM 上有一个 SDK。

标签: typescript api react-native rsa cybersource


【解决方案1】:

我已经能够让 Flex API 工作。但是您一直在使用哪种 SDK?我已经用React Native v0.61.5Typescript 和多个密码库实现了它:react-native-cryptoisomorphic-webcryptocrypto-jsjs-base64buffer。但基本上这可以在任何 Javascript 框架上完成。

我想你有 /Keys 请求工作,我猜你已经指定了 encryptionTypeRsaOaep256

下一步是导入您从上一步收到的 JSON 网络密钥 (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 传递。

在此之后,您将收到tokensignaturesignedFields 和其他一些字段。您应该根据签名验证收到的值,以确保这些值没有被篡改。

验证签名

这个很简单,我们只需要来自/Keys 请求的公钥,我们可以在der/publicKey 下找到。

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")
},

我希望这将帮助您或其他在实施 Cyber​​Source 的 Flex API 时遇到困难的人,因为这对我造成了影响......

【讨论】:

  • 这对我帮助很大!谢谢。我必须做的一个小改变是在电话webcrypto.subtle.encrypt(...) 中。似乎不再需要“哈希”设置。
  • 你摇滚!多年来第一次登录我只是为了支持这个
  • 谢谢@IkeemWilson,很高兴我能帮上忙!
  • 谢谢@AndrewTempleton,我很感激!
猜你喜欢
  • 1970-01-01
  • 2016-10-14
  • 2017-05-24
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 2017-07-11
  • 2015-06-19
  • 1970-01-01
相关资源
最近更新 更多