# @aws-crypto/client-browser 中文文档

规范地址：https://npmcn.com/package/%40aws-crypto/client-browser/

版本：5\.0\.2
npm 元数据更新时间：2026-07-23T21:16:10.312Z
中文译文更新时间：2026-11-07T16:34:34.620Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40aws-crypto/client-browser
原文对照：https://npmcn.com/package/%40aws-crypto/client-browser/?lang=original
项目主页：https://github.com/aws/aws-encryption-sdk-javascript#readme
源码仓库：https://github.com/aws/aws-encryption-sdk-javascript

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：24.2 kB，9 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：9 个
- 首次发布：2019-06-21T23:34:51.851Z
- 最新版本发布：2026-07-23T21:16:09.952Z
- 近 30 天下载量：4753（2026-10-08 至 2026-11-06，npm 官方下载统计）
- 已发布版本数：36（近 12 个月 4 个）

## 中文简介

这个 _客户端浏览器_ 模块包括您需要使用 JavaScript 网页浏览器的 AWS 加密 SDK 模块.

## 安装

```sh
npm install @aws-crypto/client-browser
```

也可以使用：`pnpm add @aws-crypto/client-browser`、`yarn add @aws-crypto/client-browser`、`bun add @aws-crypto/client-browser`

## README

# 浏览器 JavaScript 客户端的 AWS 加密 SDK

# @aws-crypto/客户端浏览器

这个 _客户端浏览器_ 模块包括您需要使用 AWS 加密 SDK 的所有模块 javaScript 网页浏览器 .

-   解密浏览器
-   加密浏览器
-   keys-ring-浏览器
-   材料管理浏览器
-   隐形-材料-管理-浏览器
-   原始键盘浏览器
-   原始的 rsa- keyring- 浏览器
-   网页后端

对于显示如何创建密钥环和加密及解密数据的模块的代码示例,请安装 [实例浏览器](https://github.com/aws/aws-encryption-sdk-javascript/tree/master/modules/example-browser) 模块.

## 安装

要安装此模块, 请使用 npm 软件包管理器 。 安装帮助请参见 [https://www.npmjs.com/get-npm (英语)](https://www.npmjs.com/get-npm)。 。 。 .

```sh
npm install @aws-crypto/client-browser
```

## 使用情况

详细代码示例 告诉你如何使用这些模块 创建密钥环 并加密和解密数据, 安装 [实例浏览器](https://github.com/aws/aws-encryption-sdk-javascript/tree/master/modules/example-browser) 模块.

```javascript

/* Start by constructing a keyring. We'll create a KMS keyring.
 * Specify an AWS Key Management Service (AWS KMS) customer master key (CMK) to be the
 * generator key in the keyring. This CMK generates a data key and encrypts it. 
 * To use the keyring to encrypt data, you need kms:GenerateDataKey permission 
 * on this CMK. To decrypt, you need kms:Decrypt permission. 
 */
const generatorKeyId = 'arn:aws:kms:us-west-2:658956600833:alias/EncryptDecrypt'

/* You can specify additional CMKs for the keyring. The data key that the generator key
 * creates is also encrypted by the additional CMKs you specify. To encrypt data, 
 *  you need kms:Encrypt permission on this CMK. To decrypt, you need kms:Decrypt permission.
 */ 
const keyIds = ['arn:aws:kms:us-west-2:658956600833:key/b3537ef1-d8dc-4780-9f5a-55776cbb2f7f']

/* Create a KMS client provider with your AWS credentials */
const clientProvider = getClient(KMS, {
  credentials: {
    accessKeyId,
    secretAccessKey
  }
})

/* Create the KMS keyring */
const keyring = new KmsKeyringBrowser({ clientProvider, generatorKeyId, keyIds })

/* Set an encryption context For more information: 
 * https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
 */
const context = {
    stage: 'demo',
    purpose: 'simple demonstration app',
    origin: 'us-west-2'
  }
 
/* Create a string to encrypt */
const plainText = new Uint8Array([1, 2, 3, 4, 5])

/* Encrypt the string using the keyring and the encryption context 
 * the Encryption SDK returns an "encrypted message" (`result`) that includes the ciphertext, 
 * the encryption context, and the encrypted data keys.
 */ 
const { result } = await encrypt(keyring, plainText, { encryptionContext: context })

/* Decrypt the result using the same keyring */
const { plaintext, messageHeader } = await decrypt(keyring, result)

/* Get the encryption context */
const { encryptionContext } = messageHeader

/* Verify that all values in the original encryption context are in the 
 * current one. (The Encryption SDK adds extra values for signing.) 
 */
Object
  .entries(context)
  .forEach(([key, value]) => {
    if (encryptionContext[key] !== value) throw new Error('Encryption Context does not match expected values')
    })

/* If the encryption context is verified, log the plaintext. */
document.write('</br>Decrypted:' + plaintext)
console.log(plaintext)

```

## 测试

```sh
npm test
```

## 兼容性考虑

### WebCrypto 可用性

WebCrypto API 并非所有浏览器都可用. 后退可以配置 . 一个倒计时库的例子有: [MSR 密码](https://www.microsoft.com/en-us/research/project/msr-javascript-cryptography-library/)

```javascript
import { configureFallback } from '@aws-crypto/client-browser'
configureFallback(msrCrypto)
```

详细情况 `configureFallback` 见: [@aws-crypto/web-crypto后端](https://npmjs.com/package/@aws-crypto/web-crypto-backend)

### 0字节 AES-GCM 操作

Safari的现代版本在0字节上不支持AES-GCM. AWS加密SDK需要这个操作. 要解决这个问题, 配置一个倒置库 . AWS加密SDK将只使用倒计时来进行零字节操作.

### RSA 选项

WebCrypto API 不支持 `PKCS1v15` RSA 密钥包装 .

### 192 位键

浏览器不支持192比特的密钥长度.

## 许可证

此 SDK 分布于 [Apache 许可证,2.0版本](http://www.apache.org/licenses/LICENSE-2.0), (中文(简体) ), 更多信息见LICENSE.txt和Notice.txt.

## 常见问题

### @aws-crypto/client-browser 是什么？

这个 客户端浏览器 模块包括您需要使用 JavaScript 网页浏览器的 AWS 加密 SDK 模块\.

### 如何安装 @aws-crypto/client-browser？

在项目目录运行 npm install @aws-crypto/client-browser，或使用 pnpm add / yarn add / bun add。

### @aws-crypto/client-browser 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @aws-crypto/client-browser 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @aws-crypto/client-browser 可以商用吗？

@aws-crypto/client-browser 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @aws-crypto/client-browser 的下载量有多少？

根据 npm 官方下载统计，@aws-crypto/client-browser 在 2026/10/08 – 2026/11/06 这 30 天内被下载 4,753 次，日均约 158 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @aws-crypto/client-browser 还在维护吗？

最新版本 5\.0\.2 发布于 2026/07/24（3 个月前）；至今共发布 36 个版本，其中过去 12 个月发布了 4 个；首次发布于 2019/06/22。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

