# RSA 密钥生成指南

## 为什么要使用 RSA 密钥？

为了保障通讯安全，FaTPay API 使用 RSA 密钥为通信做签名。RSA 属于非对称加密的一种，相较对称式加密具有更好的安全性，具体请参考 [RSA加密演算法](https://zh.wikipedia.org/wiki/RSA%E5%8A%A0%E5%AF%86%E6%BC%94%E7%AE%97%E6%B3%95)

## FaTPay和合作伙伴通信所需密钥

| 密钥名称               | 密钥标准                                | 说明                                                                                                                                         |
| ------------------ | ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| `APIPrivateKey`    | <p>2048 bits<br>PKCS #8(Base64)</p> | <p>合作伙伴生成的私钥， 用于在访问 FaTPay 时 <a href="/pages/rkLKL3eVbRwxBcXNKOIy#api-qing-qiu-jie-kou-jia-qian">加签</a>。<br>该密钥需妥善保管，避免泄露</p>              |
| `APIPublicKey`     | PKCS #8(Base64)                     | <p>合作伙伴生成的公钥，与<code>APIPrivateKey</code> 是一对。<br>需要提供给 FaTPay</p>                                                                          |
| `WebhookPublicKey` | PKCS #8(Base64)                     | <p>FaTPay 生成的公钥，并提供给合作伙伴。<br>合作伙伴在收到 FaTPay 的 webhook 回调时，用该公钥<a href="/pages/rkLKL3eVbRwxBcXNKOIy#webhook-hui-tiao-yan-qian">验证签名</a></p> |

## 如何生成 `APIPrivateKey` 和 `APIPublicKey`

我们推荐合作伙伴使用 OpenSSL 在本地生成 RSA 密钥对（公/私钥），这是最安全的生成方式。&#x20;

同时也给出了一个使用第三方线上工具（CSFG RSA Key Generator）生成 RSA 密钥对的教程，仅供参考。

{% hint style="warning" %}
请注意，FaTPay 不承担任何因合作伙伴私钥泄露而导致的损失，也不对任何第三方密钥生成工具承担责任。请务必谨慎使用密钥生成工具，务必做到妥善保管密钥
{% endhint %}

### 使用 CSFG RSA Key Generator 生成密钥对

1. 浏览器访问 <https://www.csfieldguide.org.nz/en/interactives/rsa-key-generator/>
2. Key Size 选择 2048 bits
3. Format Scheme 选择 PKCS #8(Base64)
4. 点击 Generate
5. 此时即生成了一对密钥
6. 复制 Public Key 的全部内容，保存为文本文件，即为 `APIPublicKey`
7. 复制 Private Key 的全部内容，保存为文本文件，即为 `APIPrivateKey`

<figure><img src="/files/wBLKsziHTfpQYCZYE7w0" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dev.fatpay.org/zh/appendix/rsa-keys-generation-guide.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
