# Decrypt Data

> 使用 AES 解密数据。

Source: https://docs.sommatic.ai/zh/nodes/crypto.decrypt/

- 标识: `crypto.decrypt`
- 类别: 身份
- 版本: 2.0.0
- 状态: 可用
- 执行方式: sync

使用 AES 解密数据。

## 配置

- `connection_id` (string, 可选): Sommatic AI credential resolved server-side (recommended). Accepts credentials of type `encryption-key`.
- `key` (string, 可选): Encryption key as a literal. Allowed, but it is stored in the flow, its version history and Git: prefer connection_id.
- `encrypted` (string, 必填): v1.<iv>.<tag>.<ciphertext>

## 输出端口

- `out` (默认): 解密后的字符串
- `error`: 解密失败

## 输入契约

- `connection_id` (string, 可选)
- `key` (string, 可选)
- `encrypted` (string, 必填)

## 输出契约

- `data` (any, 可选): Decrypted value

## 在表达式中

后续节点可通过 `$("Decrypt Data").item.json.<路径>` 读取此节点输出中的内容（请把 `Decrypt Data` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `out` 返回的路径，依据“端口”一节所示的输出。

- `$("Decrypt Data").item.json.data` (any) = `{…}`: Decrypted value
- `$("Decrypt Data").item.json.data.card_last4` (string) = `"4242"` (编辑器不提示)
- `$("Decrypt Data").item.json.data.amount` (number) = `129.9` (编辑器不提示)
- `$("Decrypt Data").item.json.data.currency` (string) = `"USD"` (编辑器不提示)


## 工作原理

解密由 `crypto.encrypt` 生成的 `v1.<iv>.<tag>.<ciphertext>` 令牌，并在 `data` 中返回原始 JSON 值。

1. 解析配置；`encrypted` 通常为 `{{ $json.encrypted }}`。
2. 当 `encrypted` 为空时，以 `encrypted data is required` 从 `error` 输出。
3. 选择密钥：优先 `connection_id`，否则使用字面量 `key`。
4. 检查格式：由点号分隔的四段，第一段为 `v1`。
5. 使用 AES-256-GCM 解密并校验认证标签：密钥错误或令牌被篡改都会失败。
6. 从 `out` 返回 `{ data: <原始值> }`，审计记录位于 `meta.crypto`。

## 在工作流中

### 节点

```json
{
  "node_id": "node-decrypt-payment",
  "label": "Decrypt payment details",
  "operator_slug": "crypto.decrypt",
  "operator_version": "2.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "encrypted": "{{ $json.encrypted }}"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "out",
  "data": {
    "data": {
      "card_last4": "4242",
      "amount": 129.9,
      "currency": "USD"
    }
  },
  "meta": {
    "crypto": {
      "op": "decrypt",
      "key_source": "credential",
      "alias": "connection",
      "credential_id": "pi-credential-xxxx",
      "algorithm": "aes-256-gcm",
      "ok": true
    },
    "warnings": []
  }
}
```

## 输出端口 (代码中的行为)

### `out`

令牌已解密并通过认证。

```json
{
  "data": {
    "card_last4": "4242",
    "amount": 129.9,
    "currency": "USD"
  }
}
```

### `error`

令牌缺失或格式错误、密钥错误，或凭据无法解析。

```json
{
  "error": "Decryption failed: wrong key or tampered value"
}
```

## 代码中的行为

- `connection_id`: 优先于字面量 `key`。凭据在节点运行时于服务端解析，密钥不会出现在流程中。字符串密钥直接作为密钥使用；对象密钥必须包含 `key` 或 `secret`。必须与 `crypto.encrypt` 使用的密钥相同。
- `key`: 字面量密钥可以使用，但会保存在流程、版本历史和 Git 中，并且每次运行都会在 `meta.warnings` 中报告。建议使用 `connection_id`。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `encrypted data is required` | `error` | `encrypted` 为空。 |
| `Invalid encrypted value: expected v1.<iv>.<tag>.<ciphertext>` | `error` | 该值不是 `crypto.encrypt` 生成的令牌（例如传入了整个 `{ encrypted, algorithm }` 对象，而不是 `encrypted`）。 |
| `Decryption failed: wrong key or tampered value` | `error` | 密钥与加密时使用的不同，或令牌被修改或截断。 |
| `A key is required: pass { credential: "<alias>" } (recommended) or { key }` | `error` | `connection_id` 和字面量密钥字段都未设置。请在 `connection_id` 中选择一个凭据。 |
| `credential binding 'connection' has no key usable for symmetric` | `error` | 凭据已解析，但其密钥为空，或是缺少可用字段的对象。请将密钥存为字符串，或存为包含本操作所读字段的对象（见 `connection_id`）。 |
| `Failed to resolve connection <connection_id>: <detail>` | `error` | 凭据 id 不存在，或该工作流无权使用。请检查 id 和凭据类型。 |

## 说明与限制

- 解密值位于 `data.data`：在下游用 `{{ $json.data.amount }}` 读取。
- 从这里开始，解密后的值在运行中是明文数据：避免把它发送到不需要的日志或外部系统。
- 从 `error` 端口输出不会使运行失败：引擎沿 `error` 上的连线继续；若没有连线，该分支直接结束。


## 契约（JSON Schema）

### All (`*.definition.js`)

```json
{
  "slug": "crypto.decrypt",
  "name": "Decrypt Data",
  "category": "identity",
  "version": "2.0.0",
  "description": "Decrypts a value produced by crypto.encrypt or $encrypt. Fails if the key is wrong or the value was altered.",
  "capabilities": [
    "crypto",
    "decryption"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "Encryption key credential",
        "description": "Sommatic AI credential resolved server-side (recommended). Accepts credentials of type `encryption-key`.",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "encryption-key"
          ]
        }
      },
      "key": {
        "type": "string",
        "ui_widget": "password",
        "x-literal-secret": true,
        "description": "Encryption key as a literal. Allowed, but it is stored in the flow, its version history and Git: prefer connection_id."
      },
      "encrypted": {
        "type": "string",
        "description": "v1.<iv>.<tag>.<ciphertext>"
      }
    },
    "required": [
      "encrypted"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "key": {
        "type": "string"
      },
      "encrypted": {
        "type": "string"
      }
    },
    "required": [
      "encrypted"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "data": {
        "description": "Decrypted value"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Decrypted",
      "description": "Decrypted string",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Decryption failed",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "sync",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard"
  ],
  "tags": [
    "crypto",
    "security"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_veripass",
    "profile": {
      "name": "Veripass",
      "verified": true,
      "url": "https://me.veripass.com/veripass",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string",
      "title": "Encryption key credential",
      "description": "Sommatic AI credential resolved server-side (recommended). Accepts credentials of type `encryption-key`.",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "encryption-key"
        ]
      }
    },
    "key": {
      "type": "string",
      "ui_widget": "password",
      "x-literal-secret": true,
      "description": "Encryption key as a literal. Allowed, but it is stored in the flow, its version history and Git: prefer connection_id."
    },
    "encrypted": {
      "type": "string",
      "description": "v1.<iv>.<tag>.<ciphertext>"
    }
  },
  "required": [
    "encrypted"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "key": {
      "type": "string"
    },
    "encrypted": {
      "type": "string"
    }
  },
  "required": [
    "encrypted"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "data": {
      "description": "Decrypted value"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Decrypted",
    "description": "Decrypted string",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Decryption failed",
    "is_default": false,
    "supports_condition": false
  }
]
```
