> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kasoftware.com/llms.txt
> Use this file to discover all available pages before exploring further.

# cryptoEncrypt

> 使用 AES 演算法以及儲存在知行之橋保管庫中的金鑰和初始化向量加密資料或檔案。

使用 `AES` 演算法加密指定的資料或檔案。

## 必需參數

* **keyVaultEntry**：用於金鑰的加密保管庫項的名稱。儲存在此保管庫項中的值必須與 keyVaultEntryFormat 中設定的編碼匹配，並滿足所選演算法的長度要求。
* **ivVaultEntry**：用於初始化向量 (IV) 的加密保管庫項的名稱。儲存在此保管庫項中的值必須與 ivVaultEntryFormat 中設定的編碼匹配，並滿足所選演算法的長度要求。

## 可選參數

* **algorithm**：用於加密的演算法名稱。AES 是目前唯一支援的值。它支援 128、192 和 256 位金鑰長度。
* **cipherMode**：用於處理和加密資料的方法。可接受的值包括：`CBC`、`ECB`、`OFB`、`CFB`、`CTS`、`8OFB`、`8CFB`、`GCM`、`CTR` 和 `XTS`。預設值為 `CBC`。
* **paddingMode**：用於在加密訊息時處理多餘或缺失資料的方法，確保正確構造明文。可接受的值包括：`PKCS7`、`Zeros`、`None`、`ANSIX923` 和 `ISO10126`。預設值為 `PKCS7`。
* **keyVaultEntryFormat**：keyVaultEntry 項中儲存值的編碼。可接受的值包括：`HEX`、`BASE64`、`8BIT` 和 `RAW`。預設值為 `HEX`。
* **ivVaultEntryFormat**：ivVaultEntry 項中儲存值的編碼。可接受的值包括：`HEX`、`BASE64`、`8BIT` 和 `RAW`。預設值為 `HEX`。
* **data**：要加密的資料。
* **file**：要加密的檔案。
* **outFile**：用於儲存加密資料的檔案。
* **inFormat**：加密輸入資料使用的格式。可接受的值包括：`HEX`、`BASE64`、`8BIT` 和 `RAW`。預設值為 `HEX`。
* **outFormat**：加密輸出資料使用的格式。可接受的值包括：`HEX`、`BASE64`、`8BIT` 和 `RAW`。預設值為 `HEX`。

## 輸出屬性

* **data**：如果未指定 outFile，則為加密後的資料。
* **outFile**：如果指定了 outFile，則為包含加密資料的檔案。

## 範例

在以下範例中，cryptoEncrypt 操作用於加密特定值。它對 keyVaultEntryFormat 和 ivVaultEntryFormat 參數使用預設 AES 演算法和預設 HEX 編碼。由於使用預設值，因此無需顯式設定這些參數。這意味著 keyVaultEntry 和 ivVaultEntry 的 `key` 和 `iv` 保管庫項中儲存的值必須恰好是 128 位 HEX 編碼值。例如：

* `key` 保管庫項中儲存的值：7A3F9D5C1E8B6A407D2E4C9F5B1A6D3E
* `iv` 保管庫項中儲存的值：C1D4A7F82E395B6C4D7A1E3F9B02586D

如果未使用正確長度或編碼的值，則會導致加密過程中出現錯誤。

```xml theme={null}
<!-- For the sake of this example, let's say this value comes from an API response. -->
<arc:set attr="secret.value" value="FooBar" />

<!-- Set both the Key and IV values. These are the names of the encrypted vault items. -->
<!-- If using the default AES algorithm, the values stored in each vault item need to be 128-bit HEX values. -->
<arc:set attr="input.keyVaultEntry" value="key"/>
<arc:set attr="input.ivVaultEntry" value="iv"/>

<!-- Default algorithm is AES -->
<arc:set attr="input.algorithm" value="AES" />

<!-- Pass in the input item and call cryptoEncrypt -->
<arc:set attr="input.data" value="[secret.value]"/>  
<arc:call op="cryptoEncrypt" in="input" out="result" >
  <!-- Here is where you can use the newly encrypted value/data. A message header is used here. -->
  <arc:set attr="output.Header:MyEncryptedValue" value="[result.data]" />
</arc:call>

<!-- Push the input message, with the new header, as output. -->
<arc:set attr="output.filepath" value="[Filepath]" />
<arc:push item="output" />
```
