> ## 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" />
```
