← Back to section · Tutorials & Labs
Never Roll Your Own Crypto: A Concrete Catalog of Failures
Home-grown encryption algorithms, hand-assembled protocols, hashing used as encryption, ECB mode... This article lists, one by one, the most common ways developers roll their own crypto and fail, explains why each mistake is dangerous, and gives the correct off-the-shelf approach.
“Don’t roll your own crypto” has been repeated in security circles for decades, yet the failures never stop. The reason is that home-grown cryptography has a distinctive deceptiveness: unlike ordinary business code, it does not raise errors when it is wrong. A hand-written encryption function still encrypts, decrypts, and passes functional tests, looking perfectly healthy — until someone arrives with an attack technique and the confidentiality turns out never to have existed. Worse, the real failure points are usually not the algorithm itself, but mode selection, parameter assembly, and integrity protection — details that “look like mere configuration”. This article lists the most common failure patterns, each with the wrong way, why it is dangerous, and the right way, so development and review can check against it item by item.
Failure one: inventing an algorithm, or modifying a standard one
“Improving” a standard algorithm — adding a few home-made rounds to a block cipher, truncating and concatenating hash functions, inventing a “lightweight encryption” from XOR and shifts — is the most dangerous form of rolling your own. A standard algorithm’s security rests on years of accumulated, unsuccessful attacks by researchers worldwide; any unreviewed modification zeroes out that accumulation. XOR-style home-grown schemes are especially common in IoT and mini-program contexts, and once their keystream is exposed under known plaintext, the rest is simple algebra.
There is no workaround for the correct approach: use authenticated encryption such as AES-GCM or ChaCha20-Poly1305, hashes from the SHA-2 family starting at SHA-256 or SHA-3, and standard KDFs for key derivation. Where a standard algorithm feels too heavy, the answer is to pick a lighter standard algorithm or optimize how it is called — not to invent a new one.
Failure two: wrong mode and parameters — it runs, but it does not protect
The common trait of this class is that the algorithm is right and the usage is wrong. ECB mode encrypts identical plaintext blocks into identical ciphertext blocks, so the outlines of images and structured data show straight through — the textbook counterexample, still found in legacy code today. CBC mode with a fixed or predictable IV makes identical plaintexts produce identical ciphertext beginnings, leaving the door open to chosen-plaintext attacks. Setting the IV to all zeros, or reusing a nonce under the same key — especially for counter-style modes like GCM, where nonce reuse breaks both confidentiality and the authentication key — has consequences far worse than a single ciphertext leaking.
The correct approach: prefer authenticated encryption modes and let the library handle mode details; generate nonces from a secure random source and guarantee they never repeat under one key, or use misuse-resistant constructions; store and transmit all parameters (salt, IV, iteration counts) in plaintext alongside the ciphertext — they do not need secrecy, they need to be generated correctly.
Failure three: encryption without authentication — ciphertext is freely malleable
Many home-grown schemes encrypt only, assuming “if they can’t read it, they can’t change it”. In reality, ciphertext without integrity protection is malleable: an attacker who cannot read the content can still flip bits, truncate, reorder, or replay old ciphertext to manipulate the decryption result, and padding-oracle-style attacks can turn a decryption endpoint into a tool that recovers plaintext byte by byte. Likewise, using a bare hash as a file’s “tamper protection”, or checking integrity with a home-concatenated string comparison, rests integrity on the wrong primitive.
The correct approach: use authenticated encryption (AEAD) by default, solving confidentiality and integrity in one step; where authentication is needed on its own, use a standard message authentication code such as HMAC, and compare MACs with a constant-time comparison function to prevent byte-by-byte timing guessing. “Decryption failed” and “authentication failed” must return one and the same vague error, giving the attacker no oracle to distinguish them.
Failure four: hand-assembled protocols and invented encodings
Bolting primitives together by hand into a “protocol” is also rolling your own crypto: designing your own handshake sequence, your own token format, your own password storage scheme. Password storage is the classic disaster zone — fast hashes for passwords, a single SHA-256 with a fixed salt, or home-made “encrypt it several times” schemes all fall to offline brute force; the correct approach is a slow, salted password hash designed for the purpose (such as Argon2id or bcrypt), with parameters calibrated to current hardware. Tokens and sessions are the same: prefer standard protocols and the session mechanisms of mature libraries, with tokens themselves being sufficiently long random strings plus server-side state, rather than a hand-encoded string of predictable structure.
Checkpoint: in review, whenever you see hand-assembled tokens, a custom “encryption format”, or home-made verification logic, ask which standard solution it replaces and why that solution was not good enough — if there is no answer, go back to the standard solution.
Failure five: mistaking encoding and obfuscation for encryption
Another well-disguised form of rolling your own: running data through Base64, XORing it with a fixed byte, or scrambling the order by a private rule, then declaring it “encrypted”. The purpose of encoding is representation conversion; all its rules are public, and anyone can reverse it exactly. Obfuscation merely raises the reading threshold slightly and is effectively plaintext to a patient analyst. The test is simple: if a scheme’s security depends on “the other side not knowing how I transform it”, it is not encryption — real encryption stays secure when the algorithm is fully public, with confidentiality coming only from the key. Any data-protection scheme in review whose selling point is the secrecy of the transformation itself should be treated as unencrypted and replaced with standard authenticated encryption.
Wrap-up: write the red lines into team rules
Home-grown crypto keeps happening not because developers want to show off, but because the team lacks an explicit red line. Turn this article’s catalog into three enforceable rules: cryptographic functionality may only call whitelisted libraries and algorithm combinations; any hand-written cryptographic logic (including mode assembly and token formats) is rejected by default in review, with exceptions requiring separate security approval; and legacy ECB, fixed IVs, and unauthenticated encryption are graded as vulnerabilities and scheduled for fixing. The correct posture for cryptography has never been “written cleverly” but “used correctly” — save the cleverness for business logic, and leave cryptography to battle-tested standards and libraries.
Tags: Secure CodingAnti-PatternsEncryption PracticeCode Review