How to use the one-time pad generator
- Select Generate Pad to create a random key, choosing either Modular Addition (letters only) or XOR (any text, pad as hex bytes).
- Switch to Encrypt, paste your plaintext and the generated pad, then click Run to get the ciphertext.
- To reverse the process, select Decrypt, enter the ciphertext and the exact same pad, and click Run.
- For XOR mode, the ciphertext is shown as space-separated hexadecimal values so non-printable characters are preserved.
How One Time Pad Encryption Works
A one time pad achieves perfect secrecy by combining each character of the plaintext with a corresponding character from a truly random key. The key must be at least as long as the message, used only once, and kept completely secret.
In the modular addition method, letters A through Z are mapped to numbers 0 through 25. Each plaintext letter P and key letter K produce a ciphertext letter C using the formula: C = (P + K) mod 26. Decryption reverses this: P = (C - K + 26) mod 26.
For example, to encrypt the word HELLO with the pad XMCKL:
- H (7) + X (23) = 30 mod 26 = 4 (E)
- E (4) + M (12) = 16 (Q)
- L (11) + C (2) = 13 (N)
- L (11) + K (10) = 21 (V)
- O (14) + L (11) = 25 (Z)
The resulting ciphertext is EQNVZ. Without the pad, every five-letter combination is equally likely, making brute-force attacks mathematically impossible.
Modular Addition vs XOR
This tool offers two methods for combining the plaintext with the pad. Understanding the difference helps you choose the right approach for your use case.
| Feature | Modular Addition (mod 26) | XOR |
|---|---|---|
| Character set | Uppercase A-Z only | Any Unicode character |
| Operation | (P + K) mod 26 | P ^ K (bitwise) |
| Ciphertext format | Letters | Hexadecimal bytes |
| Historical use | Diplomatic cables, spy networks | Computer-based encryption |
The XOR method operates on the UTF-8 bytes of your text, and its pad is a string of random bytes written as hex (for example 4A 9F 03). Because XOR is its own inverse, applying the same operation with the same pad recovers the original text: (P ^ K) ^ K = P. The output is displayed as hex-encoded values to avoid issues with non-printable characters.
Rules for Perfect Secrecy
Claude Shannon proved in 1949 that a one time pad provides information-theoretic security, meaning the ciphertext reveals zero information about the plaintext without the key. However, this guarantee holds only if four strict rules are followed:
- True randomness: The pad must be generated by a non-deterministic source. This tool uses
crypto.getRandomValues(), which draws from the operating system's cryptographic random number generator. - Key length: The pad must be at least as long as the plaintext. Shorter keys force reuse, which destroys security.
- Single use: Never encrypt two different messages with the same pad. If you do, an attacker can XOR the two ciphertexts together to cancel out the key and analyze the combined plaintexts.
- Secrecy: The pad must be exchanged over a secure channel before communication begins and destroyed immediately after use.
Violating any of these rules reduces the one time pad to a weaker cipher that can be broken with standard cryptanalysis techniques.
Practical Use Cases
Despite being theoretically unbreakable, the one time pad is rarely used for everyday communication because distributing long random keys securely is logistically difficult. It remains relevant in specific scenarios where absolute secrecy outweighs convenience.
During the Cold War, the Washington-Moscow hotline used one-time key tapes delivered by courier. Intelligence agencies still train operatives in manual OTP encryption as a fallback when electronic devices might be compromised. In modern software, OTP principles appear in stream ciphers and in protocols like XOR encryption for lightweight obfuscation, though those typically rely on pseudo-random generators rather than true one-time keys.
For developers exploring cryptography, building a one time pad tool is an excellent exercise in understanding why key management is often harder than the mathematics of encryption itself. You can also compare it with algorithmic approaches like AES encryption to see how practical ciphers trade theoretical perfection for usability.
Common Mistakes to Avoid
Even when the math is correct, implementation errors can compromise a one time pad system. Here are pitfalls to watch for:
- Reusing pads: The most common error. If two ciphertexts share a pad, XORing them yields the XOR of the two plaintexts, which frequency analysis can crack.
- Pseudo-random generators: Using
Math.random()instead of a cryptographic source produces predictable sequences. Always usecrypto.getRandomValues()in browsers or equivalent OS-level APIs. - Ignoring encoding: Mixing UTF-8 byte lengths with character counts causes misalignment. The XOR mode in this tool counts UTF-8 bytes, so a message with accented letters or emoji needs a pad with at least as many bytes as the text has UTF-8 bytes.
- Insecure storage: Saving the pad to disk without encryption defeats the purpose. Treat the pad file with the same care as the plaintext itself.
Worked examples
| Example | Input | Result |
|---|---|---|
| Generate a 20-character pad | Mode: generate; Method: mod26; Pad length (for Generate mode; letters for A-Z, bytes for XOR): 20 | Pad: AEORCBWTOZMFUQNOXJWK; Length: 20 characters; Method: Modular Addition (A-Z) |
| Encrypt HELLO with mod 26 | Mode: encrypt; Method: mod26; Plaintext or Ciphertext: HELLO; Pad / Key: XMCKL; Pad length (for Generate mode; letters for A-Z, bytes for XOR): 5 | Ciphertext: EQNVZ; Method: Modular Addition (mod 26); Characters processed: 5 |
| Decrypt EQNVZ with mod 26 | Mode: decrypt; Method: mod26; Plaintext or Ciphertext: EQNVZ; Pad / Key: XMCKL; Pad length (for Generate mode; letters for A-Z, bytes for XOR): 5 | Plaintext: HELLO; Method: Modular Addition (mod 26); Characters processed: 5 |
| Encrypt Hi! with XOR | Mode: encrypt; Method: xor; Plaintext or Ciphertext: Hi!; Pad / Key: 41 62 63; Pad length (for Generate mode; letters for A-Z, bytes for XOR): 3 | Ciphertext (hex): 09 0B 42; Method: XOR (UTF-8 bytes); Bytes processed: 3 |
FAQ
Is a one time pad really unbreakable?
Yes, provided the key is truly random, at least as long as the message, never reused, and kept secret. Under those conditions, every possible plaintext of that length is equally likely, so no amount of computing power can determine the original message.
Why is it called a one time pad?
The name comes from the physical paper pads used by spies and diplomats. Each page contained a random key used to encrypt exactly one message, then torn off and destroyed so it could never be reused.
What is the difference between modular addition and XOR?
Modular addition works on letters A through Z by adding their numeric positions modulo 26. XOR operates on the binary representation of any character and is its own inverse, making it more flexible for computer-based encryption.
Can I use this tool for real secrets?
This tool runs entirely in your browser and nothing is uploaded, but you must handle key distribution and destruction yourself. For serious operational security, verify the environment and ensure the pad is exchanged over a trusted channel.
Why does the XOR ciphertext look like hex numbers?
XOR operations can produce non-printable characters. Displaying the result as space-separated hexadecimal values ensures the ciphertext can be copied, stored, and decrypted accurately without encoding corruption.
What happens if my pad is shorter than my message?
The tool will return an error. A shorter pad forces key reuse, which breaks the mathematical guarantee of perfect secrecy and makes the cipher vulnerable to known-plaintext and statistical attacks.