text.crypto
encrypt and decrypt with AES, DES, ChaCha20 and RC4
Real ciphers, for puzzles and for reading data other programs encrypted. Data, keys and IVs can each be bytes or a string, which counts as its UTF-8 bytes.
good to know
Section titled “good to know”A block cipher encrypts one block at a time, 16 bytes for AES and 8 for DES, and the mode chains them. “ecb” encrypts each block alone, so equal blocks give equal output. “cbc” mixes each block with the one before, starting from an IV. Both pad the data with PKCS#7. “ctr” turns the cipher into a stream, so nothing is padded, and “gcm” (AES only) is ctr plus a 16-byte tag at the end that decrypting checks.
Leave out the IV when encrypting and a random one is made and put in front of the output, which is where decrypting looks for it when you leave it out there too.
Tags: crypto
aes_encryptencrypt with AES, the standard block cipher, in mode “ecb”, “cbc”, “ctr” or “gcm”aes_decryptdecrypt AES, the reverse of aes_encrypt with the same key, mode and IVdes_encryptencrypt with DES, or Triple DES given a 16- or 24-byte key, in mode “ecb”, “cbc” or “ctr”des_decryptdecrypt DES or Triple DES, the reverse of des_encryptchacha20encrypt or decrypt with ChaCha20 (RFC 8439), a 32-byte key and a 12-byte nonce, as the same call does bothrc4encrypt or decrypt with RC4, as the same call does both
Functions
Section titled “Functions”aes_encrypt
Section titled “aes_encrypt”aes_encrypt(data: str|list|bytes, key: str|list|bytes, mode: str = “cbc”, iv?: str|list|bytes) -> bytes
encrypt with AES, the standard block cipher, in mode “ecb”, “cbc”, “ctr” or “gcm”
The key is 16, 24 or 32 bytes for AES-128, -192 or -256. The IV is 16 bytes, or 12 for gcm, and without one a random IV goes in front of the output. gcm adds a 16-byte tag at the end
1aes_encrypt("hi", "YELLOW SUBMARINE", "ecb")b"3\xaf\xb4o\x88\x01\xb3N\xab\xd7\xc5\xf9\x1d\xc6\xaf\x9d"2aes_encrypt("hi", "YELLOW SUBMARINE", "cbc", b"\x00" * 16).hex"33afb46f8801b34eabd7c5f91dc6af9d"See also aes_decrypt, des_encrypt, pkcs7_pad
aes_decrypt
Section titled “aes_decrypt”aes_decrypt(data: str|list|bytes, key: str|list|bytes, mode: str = “cbc”, iv?: str|list|bytes) -> bytes
decrypt AES, the reverse of aes_encrypt with the same key, mode and IV
Without an IV it’s read from the front of the data. A wrong key or IV usually shows up as bad padding in ecb and cbc, or a tag that doesn’t match in gcm
1aes_decrypt(aes_encrypt("hi", "YELLOW SUBMARINE", "ctr"), "YELLOW SUBMARINE", "ctr").str"hi"See also aes_encrypt, pkcs7_unpad
des_encrypt
Section titled “des_encrypt”des_encrypt(data: str|list|bytes, key: str|list|bytes, mode: str = “cbc”, iv?: str|list|bytes) -> bytes
encrypt with DES, or Triple DES given a 16- or 24-byte key, in mode “ecb”, “cbc” or “ctr”
DES is long broken, since its 8-byte key has only 56 bits that count, but old systems and puzzles still use it. The IV is 8 bytes
1des_encrypt("hi", "8bytekey", "ecb").hex"48b0021dbf9b5190"See also des_decrypt, aes_encrypt
des_decrypt
Section titled “des_decrypt”des_decrypt(data: str|list|bytes, key: str|list|bytes, mode: str = “cbc”, iv?: str|list|bytes) -> bytes
decrypt DES or Triple DES, the reverse of des_encrypt
1des_decrypt(des_encrypt("hi", "8bytekey"), "8bytekey").str"hi"See also des_encrypt
chacha20
Section titled “chacha20”chacha20(data: str|list|bytes, key: str|list|bytes, nonce: str|list|bytes) -> bytes
encrypt or decrypt with ChaCha20 (RFC 8439), a 32-byte key and a 12-byte nonce, as the same call does both
A stream cipher: it XORs the data with a keystream, so the output is as long as the input. Never reuse a key and nonce pair
1chacha20("hi", "k" * 32, "n" * 12).hex"eeb1"2chacha20(chacha20("hi", "k" * 32, "n" * 12), "k" * 32, "n" * 12).str"hi"See also rc4, aes_encrypt
rc4(data: str|list|bytes, key: str|list|bytes) -> bytes
encrypt or decrypt with RC4, as the same call does both
An old stream cipher, broken but still found in CTFs and old protocols. The key is 1 to 256 bytes
1rc4("Plaintext", "Key").hex"bbf316e8d940af0ad3"2rc4(rc4("hi", "Key"), "Key").str"hi"More examples
Section titled “More examples”crypto
Section titled “crypto”1# encrypt and back2aes_decrypt(aes_encrypt("secret", "YELLOW SUBMARINE"), "YELLOW SUBMARINE").str"secret"3# ECB leaks repeated blocks4aes_encrypt(b"A" * 32, "YELLOW SUBMARINE", "ecb").hex"e44085fa2bda33d86aa340b4c16c05ade44085fa2bda33d86aa340b4c16c05ad60fa36707e45f499dba0f25b922301a5"5# RC4 undoes itself6rc4(rc4("attack at dawn", "Key"), "Key").str"attack at dawn"Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.