> For the complete documentation index, see [llms.txt](https://intella.gitbook.io/scan2pay/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://intella.gitbook.io/scan2pay/api-general-rules/api-jia-jie-mi-ming.md).

# API資料傳輸加解密

Scan2Pay交易過程傳輸資料皆需要進行加密。

## 說明

原始的Request資料, 使用AES-128加密, 置放於JSON中的"Request"欄位, 並將加密鎖使用的AES Key, 使用intella提供的RSA public key加密後, 置於JSON中的"ApiKey"欄位, 然後發送到Scan2Pay API.&#x20;

加密流程如下圖所示

![](https://1774462262-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEghnyONNhc4F57Y7YQ%2F-LEjGhcANyAuqedzKyOe%2F-LEjPtnbJJSTb8fU9_Kn%2FScan2Pay-encryption.png?alt=media\&token=0989ace4-ae47-4c34-ae6b-d2d7314ff527)

## 產生AES-128金鑰

傳輸資料內容要使用AES 128-bit (16位元) 加密，AES key由用戶端產生

{% hint style="warning" %}
&#x20;建議每筆交易請求皆重新產生，以確保資料的安全性
{% endhint %}

## 使用 AES\_KEY 將 Request 加密

所要發送的JSON格式資料, 需要使用AES加密

{% hint style="info" %}
AES加密採用128-bit金鑰, CBC模式, PKCS5填充, IV由intella提供
{% endhint %}

## 使用 Scan2 Pay 提供之 Public Key 將 AES\_KEY 進行加密

將加密所使用的AES Key, 使用intella所提供的RSA Public Key加以加密

**算法**: RSA      **工作模式**: ECB     **填充模式**: PKCS#1 v1.5

{% hint style="info" %}
&#x20;AES Key需要先進行Base64編碼後成為字串, 再以RSA公鑰進行加密.
{% endhint %}

## 進行交易請求

將AES加密過的原始傳輸資料置於JSON中的"Request"欄位, RSA加密的AES Key置於"ApiKey"欄位, 發送到Scan2Pay API, 並取得回覆的加密結果

範例: 原始Request資料

```
{
    "Header":
    {
        "Method":"00000",
        "ServiceType":"OLPay",
        "MchId":"103543903790001",
        "TradeKey":"03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4",
        "CreateTime": "20180313120000"
    },
    "Data":"{\"TimeExpire\":\"201803121000\", \"StoreOrderNo\": \"skb0001\",\"Body\": \"Milk Tea\",\"TotalFee\":\ "35\",\"FeeType\": \"TWD\",\"Detail\": \"商品細節\",\"StoreInfo\": \"Intella0001\",\"Token\": \"QWERTYUIOPASDFGHJKLZXCVBNM123456\"}"
}
```

AES加密, 並含入RSA加密後的AES Key資料 (加密資料皆使用Base64 encode)

```
{
    "Request":"Y7mg/9WbP6FPVkYew5dRRuxtjH7OOR6NJWakEj/kwqZ56AjjQzJAV.....TtOGcm",
    "ApiKey":"unhqcdoY9l6yLf....."
}
```

## 使用 AES\_KEY 解密收到加密過的結果

收到的Response資料, 格式如下

```
{
    "Response":"WCTG0AI44/Ju/AEM6F.....UPLqHMDuI"
}
```

Response內容為AES加密資料(Base64 encode), 使用加密Request相同的AES Key來進行解密, 取得原始的Response JSON內容. (一樣是CBC模式, PKCS5填充, 並使用intella提供的IV值)

{% hint style="info" %}
Response欄位所帶的加密字串中, 請先移除 "\n" 字串, 並將 "\u003d" 置換成 "="後, 再進行AES解密
{% endhint %}
