One-Time Pad

From TheAlmightyGuru
Revision as of 13:22, 12 October 2017 by TheAlmightyGuru (talk | contribs)
Jump to: navigation, search

A one-time pad is a form of encryption that, when encrypted with a truly random key, cannot be cracked, and decryption is only possible with the key. It was first professionally described in 1882 by Frank Miller. Despite being uncrackable, it has several failings that make it unattractive for modern use.


Using a one-time pad, you must have a key that is at least as long as the plaintext. To encrypt the plaintext, you process each letter of the plaintext with each letter of the key and perform a reversible calculation. The simplest calculation is modular addition, where you simply rotate the character along the alphabet a number of times equal to the key. The result after this process is the ciphertext. For example, assuming our alphabet consists of 27 values (A = 1, B = 2, C = 3, ... Z = 26, and space = 27), a letter of plaintext that is C (3), added to the letter R (18) in the key, would result with ciphertext letter U (21). If the sum were to exceed the size pf the alphabet, for example a plaintext X (24), added to G (7), we would simply roll back to the beginning of the alphabet so the ciphertext would be D (4). Using this technique, we can do the following:

 plaintext: ATTACK TONIGHT
       key: IQENEPLRB ZAZF
ciphertext: JJYOH LIQNHHGZ


Decryption is done using the reverse of the encryption, so, if modular addition was used for the encryption, the decryption is performed with modular subtraction. Thus:

ciphertext: BPQ QXI
       key: KKCILWP
 plaintext: RETREAT


  • The largest benefit of a random one-time pad is that the encryption is unbreakable without the key because you can get any possible result from a different key. For example, in the decryption example above, they key KKCILWP would accurately decrypt the text to RETREAT, but the key PVWEDPN would decrypt the text to OFFENSE, while NQIAXKJ would decrypt it to LASAGNA.
  • It's very simple to encrypt and decrypt a one-time pad. Most modern encryption methods require the use of powerful computers, but to use a one-time pad you don't even need to know how to add, only to count.


  • It requires large keys which cannot be memorized.


I wrote this FreeBASIC program that can take a message written in the printable characters of 7-bit ASCII, generate a random pad, encrypt the plaintext with modular addition and decrypt it with modular subtraction.

' This program will encrypt a message with a randomly generated one-time pad using modular addition, and 
' then decrypt it using modular subtraction. Since it uses the ASCII code as its alphabet, the math is a 
' little more complex than a simple alphabet, but this allows it to be more usable on computers.
' © Copyright 2017, Dean Tersigni

Randomize Timer
Dim As String sPlainText, sKey, sCipherText, sEncyptedLetter, sDecryptedText
Dim As Integer iPlace, iRandom, iEncryptedLetter

' This is the plaintext message.
sPlainText = "Attack tonight @ 9 PM!"

' Generate a random one-time pad key the same length as the plaintext.
For iPlace = 1 To Len(sPlainText)
    iRandom = Int(Rnd * 94) + 32      ' Only use printable characters in the key.
    sKey = sKey + Chr(iRandom)
Next iPlace

' Encrypt the plaintext using modular addition.
For iPlace = 1 To Len(sPlainText)
    iEncryptedLetter = Asc(Mid(sPlainText, iPlace, 1)) + (Asc(Mid(sKey, iPlace, 1)) - 31)
    If iEncryptedLetter > 126 Then
        iEncryptedLetter = iEncryptedLetter - 95
    End If
    sCipherText = sCipherText + Chr(iEncryptedLetter)
Next iPlace

' Decrypt the ciphertext using modular subtraction.
For iPlace = 1 To Len(sPlainText)
    iEncryptedLetter = Asc(Mid(sCipherText, iPlace, 1)) - (Asc(Mid(sKey, iPlace, 1)) - 31)
    If iEncryptedLetter < 32 Then
        iEncryptedLetter = iEncryptedLetter + 95
    End If
    sDecryptedText = sDecryptedText + Chr(iEncryptedLetter)
Next iPlace

Print " Plaintext: " + sPlainText
Print "       Key: " + sKey
Print "Ciphertext: " + sCipherText
Print "Decryption: " + sDecryptedText