# One-time pad

A * one-time pad* is a form of encryption that cannot be cracked, so 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.

## Encryption

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 bit of the plaintext with each bit of the key and perform a reversible calculation like modular addition. The result is the ciphertext. For example:

Assuming A=1, B=2, C=3, ... Z=26, and space=0. plaintext: ATTACK TONIGHT key: IQENEPLRB ZAZF ciphertext:

## Program

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 Sleep