npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

aix-aes

v0.0.4

Published

AES 加密工具库,支持 AES-ECB 模式,PKCS#5 填充

Readme

aix-aes

AES 加密工具库,支持 AES-ECB 模式,PKCS#5 填充。

安装

npm install aix-aes --save

或者使用 CDN 直接引入:

<script src="https://unpkg.com/aix-aes/dist/aes.min.js"></script>

基本用法

使用默认实例

import aes from 'aix-aes';

// 生成随机密钥
const key = await aes.generateKey();

// 加密文本
const result = await aes.encrypt('Hello, World!', key);
console.log('加密结果:', result.encrypted);

// 解密文本
const decrypted = await aes.decrypt(result.encrypted, key);
console.log('解密结果:', decrypted); // Hello, World!

使用类实例

import { AES } from 'aix-aes';

// 创建 AES 实例(默认 256 位密钥)
const aes = new AES();

// 或者指定密钥长度
const aes128 = new AES({ keyLength: 128 });
const aes192 = new AES({ keyLength: 192 });
const aes256 = new AES({ keyLength: 256 });

// 生成随机密钥
const key = await aes.generateKey();

// 加密
const encrypted = await aes.encrypt('Hello, World!', key);

// 解密
const decrypted = await aes.decrypt(encrypted.encrypted, key);

从密码生成密钥

import aes from 'aix-aes';

// 从密码生成密钥(自动生成盐值)
const { key, salt } = await aes.deriveKeyFromPassword('myPassword');

// 使用指定的盐值
const { key: key2, salt: salt2 } = await aes.deriveKeyFromPassword(
  'myPassword',
  'base64EncodedSalt',
  100000 // 迭代次数
);

// 保存 salt,用于后续解密
console.log('密钥:', key);
console.log('盐值:', salt);

加密/解密对象

import aes from 'aix-aes';

const key = await aes.generateKey();

// 加密对象(自动序列化为 JSON)
const data = { name: 'Alice', age: 30 };
const encrypted = await aes.encryptObject(data, key);

// 解密对象(自动反序列化 JSON)
const decrypted = await aes.decryptObject(encrypted.encrypted, key);
console.log(decrypted); // { name: 'Alice', age: 30 }

在浏览器中使用(UMD)

<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/aix-aes/dist/aes.min.js"></script>
</head>
<body>
  <script>
    (async function() {
      // 使用全局变量 AES
      const aes = new AES.AES();
      const key = await aes.generateKey();
      const encrypted = await aes.encrypt('Hello, World!', key);
      const decrypted = await aes.decrypt(encrypted.encrypted, key);
      console.log('解密结果:', decrypted);
    })();
  </script>
</body>
</html>

API 文档

AES 类

构造函数

constructor(options?: IAESOptions)
  • options.keyLength: 密钥长度,可选值为 128192256,默认为 256

方法

generateKey()

生成随机密钥。

async generateKey(): Promise<string>

返回值: Base64 编码的密钥字符串

示例:

const key = await aes.generateKey();
deriveKeyFromPassword()

从密码生成密钥(使用 PBKDF2)。

async deriveKeyFromPassword(
  password: string,
  salt?: string,
  iterations?: number
): Promise<{ key: string; salt: string }>

参数:

  • password: 密码字符串
  • salt: 盐值(可选,如果不提供则随机生成,Base64 编码)
  • iterations: 迭代次数,默认为 100000

返回值: 包含密钥和盐值的对象

示例:

const { key, salt } = await aes.deriveKeyFromPassword('myPassword');
encrypt()

AES 加密文本。

async encrypt(plainText: string, key: string): Promise<IEncryptResult>

参数:

  • plainText: 要加密的文本
  • key: 密钥(Base64 编码)

返回值: 包含加密结果的对象

示例:

const result = await aes.encrypt('Hello, World!', key);
console.log(result.encrypted); // Base64 编码的加密数据
decrypt()

AES 解密文本。

async decrypt(encryptedData: string, key: string): Promise<string>

参数:

  • encryptedData: 加密的数据(Base64 编码)
  • key: 密钥(Base64 编码)

返回值: 解密后的文本

示例:

const decrypted = await aes.decrypt(encryptedData, key);
encryptObject()

加密对象(自动序列化为 JSON)。

async encryptObject<T>(data: T, key: string): Promise<IEncryptResult>

参数:

  • data: 要加密的对象
  • key: 密钥(Base64 编码)

返回值: 包含加密结果的对象

示例:

const data = { name: 'Alice', age: 30 };
const result = await aes.encryptObject(data, key);
decryptObject()

解密对象(自动反序列化 JSON)。

async decryptObject<T>(encryptedData: string, key: string): Promise<T>

参数:

  • encryptedData: 加密的数据(Base64 编码)
  • key: 密钥(Base64 编码)

返回值: 解密后的对象

示例:

const decrypted = await aes.decryptObject<{ name: string; age: number }>(
  encryptedData,
  key
);

类型定义

IAESOptions

interface IAESOptions {
  /** 密钥长度,默认为 256 位 */
  keyLength?: 128 | 192 | 256;
}

IEncryptResult

interface IEncryptResult {
  /** 加密后的数据(Base64 编码) */
  encrypted: string;
}

技术说明

  • 加密模式: AES-ECB(Electronic Codebook)
  • 填充方式: PKCS#5(与 PKCS#7 兼容,AES 使用 16 字节块大小)
  • 密钥长度: 支持 128、192、256 位
  • 密钥派生: 使用 PBKDF2 从密码生成密钥

构建与开发

# 安装依赖
npm install

# 开发模式(监听文件变化)
npm run dev

# 构建
npm run build

# 清理构建产物
npm run clean

注意事项

  1. 安全性: ECB 模式不推荐用于加密大量数据,建议使用 CBC 或其他模式(未来版本可能支持)
  2. 密钥管理: 请妥善保管密钥,密钥丢失将无法解密数据
  3. 盐值保存: 使用 deriveKeyFromPassword 时,请保存盐值以便后续解密

License

MIT