企业级加密工具库,提供 AES、RSA、MD5、SHA256 加解密能力。所有密钥均由外部调用方传入,库内无任何硬编码默认值。
jsencryptpnpm add @iap/encryptor
import { aesEncrypt, aesDecrypt, aesGenerateKey, aesGenerateIV } from '@iap/encryptor'
const key = aesGenerateKey(16) // 随机 128 位密钥
const iv = aesGenerateIV(16) // 随机 IV
const cipher = aesEncrypt('hello world', { key, iv })
const plain = aesDecrypt(cipher, { key, iv }) // 'hello world'
import { aesEncrypt, aesDecrypt, aesGenerateKey, aesGenerateIV } from '@iap/encryptor'
| 函数 | 说明 |
|---|---|
aesEncrypt(plaintext, { key, iv }) |
AES-CBC + PKCS7 加密,返回 Base64 字符串 |
aesDecrypt(ciphertext, { key, iv }) |
AES-CBC + PKCS7 解密,返回原始字符串 |
aesGenerateKey(length) |
生成随机密钥字符串(通常 16 或 32 位) |
aesGenerateIV(length) |
生成随机 IV 字符串(通常 16 位) |
import { rsaEncrypt, rsaDecrypt } from '@iap/encryptor'
| 函数 | 说明 |
|---|---|
rsaEncrypt(data, publicKeyPem) |
使用 RSA 公钥加密(PEM 格式) |
rsaDecrypt(ciphertext, privateKeyPem) |
使用 RSA 私钥解密(PEM 格式) |
加解密失败时均会抛出 Error。
import { md5Hash } from '@iap/encryptor'
md5Hash('hello') // '5D41402ABC4B2A76B9719D911017C592'
import { sha256Hash } from '@iap/encryptor'
sha256Hash('hello') // '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'
sha256Hash('') // ''
import type { AESEncryptOptions, EncryptorConfig } from '@iap/encryptor'