0

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

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

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

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

4

1 回答 1

2

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

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

下一步是导入您从上一步收到的 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)))
}

这个函数的结果可以直接传递cardNumbercardInfo.

在此之后,您将收到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 时遇到困难的人,因为这对我造成了影响……

于 2020-03-24T17:07:40.230 回答