Skip to content

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.

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_encrypt encrypt with AES, the standard block cipher, in mode “ecb”, “cbc”, “ctr” or “gcm”
  • aes_decrypt decrypt AES, the reverse of aes_encrypt with the same key, mode and IV
  • des_encrypt encrypt with DES, or Triple DES given a 16- or 24-byte key, in mode “ecb”, “cbc” or “ctr”
  • des_decrypt decrypt DES or Triple DES, the reverse of des_encrypt
  • chacha20 encrypt or decrypt with ChaCha20 (RFC 8439), a 32-byte key and a 12-byte nonce, as the same call does both
  • rc4 encrypt or decrypt with RC4, as the same call does both
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

1
aes_encrypt("hi", "YELLOW SUBMARINE", "ecb")b"3\xaf\xb4o\x88\x01\xb3N\xab\xd7\xc5\xf9\x1d\xc6\xaf\x9d"
2
aes_encrypt("hi", "YELLOW SUBMARINE", "cbc", b"\x00" * 16).hex"33afb46f8801b34eabd7c5f91dc6af9d"
Example outputCtrl+Enter to run

See also aes_decrypt, des_encrypt, pkcs7_pad

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

1
aes_decrypt(aes_encrypt("hi", "YELLOW SUBMARINE", "ctr"), "YELLOW SUBMARINE", "ctr").str"hi"
Example outputCtrl+Enter to run

See also aes_encrypt, pkcs7_unpad

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

1
des_encrypt("hi", "8bytekey", "ecb").hex"48b0021dbf9b5190"
Example outputCtrl+Enter to run

See also des_decrypt, aes_encrypt

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

1
des_decrypt(des_encrypt("hi", "8bytekey"), "8bytekey").str"hi"
Example outputCtrl+Enter to run

See also des_encrypt

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

1
chacha20("hi", "k" * 32, "n" * 12).hex"eeb1"
2
chacha20(chacha20("hi", "k" * 32, "n" * 12), "k" * 32, "n" * 12).str"hi"
Example outputCtrl+Enter to run

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

1
rc4("Plaintext", "Key").hex"bbf316e8d940af0ad3"
2
rc4(rc4("hi", "Key"), "Key").str"hi"
Example outputCtrl+Enter to run

See also chacha20, xor

1
# encrypt and back
2
aes_decrypt(aes_encrypt("secret", "YELLOW SUBMARINE"), "YELLOW SUBMARINE").str"secret"
3
# ECB leaks repeated blocks
4
aes_encrypt(b"A" * 32, "YELLOW SUBMARINE", "ecb").hex"e44085fa2bda33d86aa340b4c16c05ade44085fa2bda33d86aa340b4c16c05ad60fa36707e45f499dba0f25b922301a5"
5
# RC4 undoes itself
6
rc4(rc4("attack at dawn", "Key"), "Key").str"attack at dawn"
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.