AES (ECB)
AES (ECB)
AES, every 16-byte block on its own.
- Keyspace
- 2^128, 2^192 or 2^256
- Decode
- same options back
- Works on
- UTF-8 or hex, hex out
- Family
- 15 in sp network
Options
Access
- Create
create("aes") - CLI
ciphers encode aes 'ATTACK AT DAWN' --key 2b7e151628aed2a6abf7158809cf4f3c - Tryplayground with the sample above
- Kinaes-cbc, aes-cfb, aes-ofb, aes-ctr +10
AES from FIPS-197 with a 128, 192 or 256-bit key, picked by the key length: 32, 48 or 64 hex digits. Case doesn't matter and spaces are ignored. The mode is ECB, the electronic codebook. The text is encoded as UTF-8 and padded with PKCS#7, then each 16-byte block is encrypted with the same key and nothing else. The ciphertext comes back as lowercase hex, and decode wants hex too.
const aes = create("aes");
const key = "2b7e151628aed2a6abf7158809cf4f3c";
aes.encode("ATTACK AT DAWN", { key }).text; // "bef12e48d0f1739d732326cbecbef389"
aes.decode("bef12e48d0f1739d732326cbecbef389", { key }).text; // "ATTACK AT DAWN"
OpenSSL gives the same bytes, so you can check it without this package:
printf %s "ATTACK AT DAWN" | openssl enc -aes-128-ecb -K 2b7e151628aed2a6abf7158809cf4f3c | xxd -p
The block function is tested against the FIPS-197 Appendix C examples and the ECB vectors from NIST SP 800-38A.
Bytes in, bytes out
Got bytes, not text? Then UTF-8, padding and hex just get in the way. ecb from @agntn/ciphers/aes drops all three:
import { ecb } from "@agntn/ciphers/aes";
const key = Uint8Array.fromHex("000102030405060708090a0b0c0d0e0f");
const block = Uint8Array.fromHex("00112233445566778899aabbccddeeff");
ecb(block, key, "encrypt").toHex(); // "69c4e0d86a7b0430d8cdb78070b4c55a"
That's the FIPS-197 C.1 block again, now without a round trip through strings. The subpath brings AES and nothing else, no registry and no Enigma. The data must be whole 16-byte blocks and the key 16, 24 or 32 bytes. Anything else throws before the first round. The error says how many key bytes came in, never which ones.
Why ECB leaks
Same key, same block, same output. Thirty-two As are two identical blocks of plaintext, and they come out as two identical blocks of ciphertext:
aes.encode("A".repeat(32), { key: "000102030405060708090a0b0c0d0e0f" }).text;
// dd4b1a0b47daa7067d0b59d95d58a6ae dd4b1a0b47daa7067d0b59d95d58a6ae 954f64f2e4e86e9eee82d20216684899
Spaces added here to show the blocks. The third block is only padding. That's the whole weakness: nobody learns the key, but anyone sees which blocks repeat, and can cut blocks out or swap them without knowing the key either. It's why ECB is the mode CTFs pick and the mode textbooks warn about.
A key that isn't 32, 48 or 64 hex digits is an InvalidOptionError, a missing one a MissingOptionError. On decode, a ciphertext that isn't whole 16-byte blocks of hex is a CipherError. So is a wrong key, because the last block then almost never ends in valid PKCS#7 padding. Decrypted bytes that aren't UTF-8 are an error too, unless bytes: "hex" asks for them as hex.
This implementation is for learning and for puzzles. It's plain TypeScript with table lookups, not constant time, and ECB hides nothing about the structure of the message. Don't protect anything real with it.