AES 加密工具库,支持 AES-ECB 模式,PKCS#5 填充
npm install aix-aesAES 加密工具库,支持 AES-ECB 模式,PKCS#5 填充。
``bash`
npm install aix-aes --save
或者使用 CDN 直接引入:
`html`
`javascript
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!
`
`javascript
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);
`
`javascript
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);
`
`javascript
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 }
`
`html`
#### 构造函数
`typescript`
constructor(options?: IAESOptions)
- options.keyLength: 密钥长度,可选值为 128、192 或 256,默认为 256
#### 方法
##### generateKey()
生成随机密钥。
`typescript`
async generateKey(): Promise
返回值: Base64 编码的密钥字符串
示例:
`javascript`
const key = await aes.generateKey();
##### deriveKeyFromPassword()
从密码生成密钥(使用 PBKDF2)。
`typescript`
async deriveKeyFromPassword(
password: string,
salt?: string,
iterations?: number
): Promise<{ key: string; salt: string }>
参数:
- password: 密码字符串salt
- : 盐值(可选,如果不提供则随机生成,Base64 编码)iterations
- : 迭代次数,默认为 100000
返回值: 包含密钥和盐值的对象
示例:
`javascript`
const { key, salt } = await aes.deriveKeyFromPassword('myPassword');
##### encrypt()
AES 加密文本。
`typescript`
async encrypt(plainText: string, key: string): Promise
参数:
- plainText: 要加密的文本key
- : 密钥(Base64 编码)
返回值: 包含加密结果的对象
示例:
`javascript`
const result = await aes.encrypt('Hello, World!', key);
console.log(result.encrypted); // Base64 编码的加密数据
##### decrypt()
AES 解密文本。
`typescript`
async decrypt(encryptedData: string, key: string): Promise
参数:
- encryptedData: 加密的数据(Base64 编码)key
- : 密钥(Base64 编码)
返回值: 解密后的文本
示例:
`javascript`
const decrypted = await aes.decrypt(encryptedData, key);
##### encryptObject()
加密对象(自动序列化为 JSON)。
`typescript`
async encryptObject
参数:
- data: 要加密的对象key
- : 密钥(Base64 编码)
返回值: 包含加密结果的对象
示例:
`javascript`
const data = { name: 'Alice', age: 30 };
const result = await aes.encryptObject(data, key);
##### decryptObject()
解密对象(自动反序列化 JSON)。
`typescript`
async decryptObject
参数:
- encryptedData: 加密的数据(Base64 编码)key
- : 密钥(Base64 编码)
返回值: 解密后的对象
示例:
`javascript`
const decrypted = await aes.decryptObject<{ name: string; age: number }>(
encryptedData,
key
);
#### IAESOptions
`typescript`
interface IAESOptions {
/* 密钥长度,默认为 256 位 /
keyLength?: 128 | 192 | 256;
}
#### IEncryptResult
`typescript`
interface IEncryptResult {
/* 加密后的数据(Base64 编码) /
encrypted: string;
}
- 加密模式: AES-ECB(Electronic Codebook)
- 填充方式: PKCS#5(与 PKCS#7 兼容,AES 使用 16 字节块大小)
- 密钥长度: 支持 128、192、256 位
- 密钥派生: 使用 PBKDF2 从密码生成密钥
`bash安装依赖
npm install
注意事项
1. 安全性: ECB 模式不推荐用于加密大量数据,建议使用 CBC 或其他模式(未来版本可能支持)
2. 密钥管理: 请妥善保管密钥,密钥丢失将无法解密数据
3. 盐值保存: 使用
deriveKeyFromPassword` 时,请保存盐值以便后续解密MIT