@webbuf/aescbc
Version:
Rust/wasm optimized AES+CBC encryption/decryption for web, node.js, deno and bun.
52 lines (35 loc) • 1.42 kB
Markdown
AES-CBC encryption and decryption, optimized with Rust/WASM.
> **Note**: This library does not provide message authentication. Use `@webbuf/acb3` for authenticated encryption.
```bash
npm install @webbuf/aescbc
```
```typescript
import { aescbcEncrypt, aescbcDecrypt } from "@webbuf/aescbc";
import { WebBuf } from "@webbuf/webbuf";
import { FixedBuf } from "@webbuf/fixedbuf";
// AES-256 key (32 bytes)
const aesKey = FixedBuf.fromRandom<32>(32);
// Optional: custom IV (16 bytes). If not provided, random IV is generated.
const iv = FixedBuf.fromRandom<16>(16);
// Encrypt
const plaintext = WebBuf.fromUtf8("Hello, world!");
const ciphertext = aescbcEncrypt(plaintext, aesKey, iv);
// Note: IV is prepended to ciphertext
// Decrypt
const decrypted = aescbcDecrypt(ciphertext, aesKey);
console.log(decrypted.toUtf8()); // "Hello, world!"
```
| Function | Description |
| --------------------------------------- | --------------------------------------------------- |
| `aescbcEncrypt(plaintext, aesKey, iv?)` | Encrypt with AES-CBC. IV prepended to output. |
| `aescbcDecrypt(ciphertext, aesKey)` | Decrypt AES-CBC. Expects IV at start of ciphertext. |
- **AES-128**: `FixedBuf<16>`
- **AES-192**: `FixedBuf<24>`
- **AES-256**: `FixedBuf<32>`
MIT