aix-aes
v0.0.4
Published
AES 加密工具库,支持 AES-ECB 模式,PKCS#5 填充
Maintainers
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: 密钥长度,可选值为128、192或256,默认为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注意事项
- 安全性: ECB 模式不推荐用于加密大量数据,建议使用 CBC 或其他模式(未来版本可能支持)
- 密钥管理: 请妥善保管密钥,密钥丢失将无法解密数据
- 盐值保存: 使用
deriveKeyFromPassword时,请保存盐值以便后续解密
License
MIT
