Title: | Classical Cryptography Methods for Words and Phrases |
---|---|
Description: | Classical cryptography methods for words and brief phrases. Substitution, transposition and concealment (null) ciphers are available, like Caesar, Vigenère, Atbash, affine, simple substitution, Playfair, rail fence, Scytale, single column, bifid, trifid, and Polybius ciphers. |
Authors: | Luigi Annicchiarico [cre, aut] |
Maintainer: | Luigi Annicchiarico <[email protected]> |
License: | GPL-3 |
Version: | 0.2.0 |
Built: | 2025-01-13 15:29:05 UTC |
Source: | https://github.com/luigi-annic/ciphertext |
The affine cipher is a monoalphabetic substitutoin cipher, where each letter is enciphered with the function (ax+b) mod 26 (26 is the number of letters in the alphabet)
affine(word, a, b, encrypt = TRUE)
affine(word, a, b, encrypt = TRUE)
word |
Word or phrase to be encrypted |
a |
First parameter. This value and 26 must be coprime |
b |
Second parameter. Magnitude of the shift |
encrypt |
If 'TRUE' (default), the program ciphers the input word, If 'FALSE', the program decrypts it. |
a string
https://en.wikipedia.org/wiki/Affine_cipher
affine("Hello", 1, -1)
affine("Hello", 1, -1)
The Atbash cipher is a type of monoalphabetic cipher which takes the alphabet and maps it to its reverse. It is a particular case of the affine cipher, with 'a'='b'= ('m'-1). As 'm' is the number of letters and is equal to 26, it means that 'a' = 'b' = 25. Encrypting and decrypting are not separate for this cipher.
atbash(word)
atbash(word)
word |
Word or phrase to be encrypted |
a string
https://en.wikipedia.org/wiki/Atbash
atbash("abcxyz")
atbash("abcxyz")
The bifid cipher is an encryption method that combines a substitution with a Polybius square and a transposition, and uses fractionation to achieve diffusion. It was invented by Felix Delastelle.
bifid_delastelle(input, key = "", period = 100, encrypt = TRUE)
bifid_delastelle(input, key = "", period = 100, encrypt = TRUE)
input |
Word or phrase to be encrypted, or character vector with the sequence of coordinate numbers if we need to decrypt |
key |
key Word for creating the modified Polybius square |
period |
period length for splitting the input phrase. If greater or equal to the length of the input then the split is not executed |
encrypt |
If 'TRUE' (default), the program ciphers the input word, If 'FALSE', the program decrypts it. |
a string
https://en.wikipedia.org/wiki/Bifid_cipher
bifid_delastelle("dcode", key = "secret", period = 3, encrypt = TRUE) bifid_delastelle("apiai", key = "secret", period = 3, encrypt = FALSE)
bifid_delastelle("dcode", key = "secret", period = 3, encrypt = TRUE) bifid_delastelle("apiai", key = "secret", period = 3, encrypt = FALSE)
caesar encryption
caesar(word, key = 1, encrypt = TRUE)
caesar(word, key = 1, encrypt = TRUE)
word |
Word or phrase to be encrypted |
key |
numeric key |
encrypt |
If 'TRUE' (default), the program ciphers the input word, If 'FALSE', the program decrypts it. |
a string
caesar("Hello", 1)
caesar("Hello", 1)
Use combinations of the ciphers. Once you provide the initial input word, the program applies the first cipher to the initial word and saves a first encryption, which is used as input word for the second cipher. This process repeats until the last cipher, and the final output is returned.
combciph(word, funcs, keys = rep(NA, length(funcs)))
combciph(word, funcs, keys = rep(NA, length(funcs)))
word |
Word or phrase to be encrypted or decrypted |
funcs |
Vector with the functions that are to be used for the sequential cipher. At the moment, only ciphers with a single key or no key are allowed (i.e., ciphers like affine which need 2 keys cannot be used) |
keys |
Single key to be used for each of the ciphers. Each key refers to the cipher in the same position in funcs argument. If no key needs to be added (or if you want to use the default value for key), use NA |
a string
combciph("hello", funcs = c(caesar, atbash, polybius), keys = c(3, NA, NA)) #' # which is equivalent to polybius(atbash(caesar("hello", key = 3)))
combciph("hello", funcs = c(caesar, atbash, polybius), keys = c(3, NA, NA)) #' # which is equivalent to polybius(atbash(caesar("hello", key = 3)))
A null cipher is an encryption method where the plaintext is mixed with a large amount of non-cipher material (decoy).
nullcipher(phrase, index, encrypt = FALSE)
nullcipher(phrase, index, encrypt = FALSE)
phrase |
Word or phrase to be decrypted |
index |
letter of interest for each word in the phrase. Also a pattern vector can be entered. |
encrypt |
Only Decryption is possible for now, but will be updated in the future |
a string
https://en.wikipedia.org/wiki/Null_cipher
nullcipher("handy set false posts", c(1,2,3))
nullcipher("handy set false posts", c(1,2,3))
The Playfair cipher is a symmetric method which encrypts pairs of letters using a modified Polybius square
playfair(word, key = "", added_letter = "x", encrypt = TRUE)
playfair(word, key = "", added_letter = "x", encrypt = TRUE)
word |
Word or phrase to be encrypted or decrypted |
key |
Word for creating the modified Polybius square |
added_letter |
Letter to be added in case two letters of a pair are identical; usually "x" is used |
encrypt |
If 'TRUE' (default), the program ciphers the input word, If 'FALSE', the program decrypts it. |
a string
https://en.wikipedia.org/wiki/Playfair_cipher
playfair( "instruments", "monarchy", added_letter = "z") playfair("gatlmzclrqtx", "monarchy", added_letter = "z", encrypt = FALSE)
playfair( "instruments", "monarchy", added_letter = "z") playfair("gatlmzclrqtx", "monarchy", added_letter = "z", encrypt = FALSE)
The polybius square is a device which associates each letter to a pair of coordinates. The letter J is excluded and replaced with I in order to get 25 letters and create a 5x5 matrix.
polybius(input, encrypt = TRUE)
polybius(input, encrypt = TRUE)
input |
Word or phrase to be encrypted, or character vector with the sequence of coordinate numbers if we need to decrypt |
encrypt |
If 'TRUE' (default), the program ciphers the input word, If 'FALSE', the program decrypts it. |
a string
https://en.wikipedia.org/wiki/Polybius_square
polybius("hello world") polybius("23 15 31 31 34 52 34 42 31 14", encrypt = TRUE)
polybius("hello world") polybius("23 15 31 31 34 52 34 42 31 14", encrypt = TRUE)
The rail fence is a transposition cipher where the text is written upwards and downwards diagonally (zigzag) on the rails of the fence
railfence(word, key = 3)
railfence(word, key = 3)
word |
Word or phrase to be encrypted |
key |
numeric key (number of rails) |
a string
https://en.wikipedia.org/wiki/Rail_fence_cipher
railfence('we are discovered flee at once',3)
railfence('we are discovered flee at once',3)
The Scytale is a transposition cipher The diameter of the Scytale (the number of turns) can be regarded as the key of the cipher.
scytale(word, key = 3, encrypt = TRUE)
scytale(word, key = 3, encrypt = TRUE)
word |
Word or phrase to be encrypted or decrypted |
key |
Number of turns of the band |
encrypt |
If 'TRUE' (default), the program ciphers the input word, If 'FALSE', the program decrypts it. |
a string
https://en.wikipedia.org/wiki/Scytale
scytale('we are discovered flee at once',3)
scytale('we are discovered flee at once',3)
simple substitution cipher. Each letter is monoalphabetically associated with a different one used for the encryption.
simple_substitution(word, key = "", seed = sample(1:1000, 1))
simple_substitution(word, key = "", seed = sample(1:1000, 1))
word |
Word or phrase to be encrypted |
key |
Word to be used as key for the encryption. If not provided, a random shuffle is performed |
seed |
Seed for reproducibility of the encryption if key is not provided |
a list with custom class "cipher", which modifies the printing defaults. The list contains the initial phrase (initial), the ciphered output (encrypted), and the alphabet order (keyalphabet)
simple_substitution("hello world", seed = 1234) simple_substitution("hello world", key = "zebras")
simple_substitution("hello world", seed = 1234) simple_substitution("hello world", key = "zebras")
In a columnar transposition cipher, the message is written out in rows of a fixed length, and then read out again column by column. The order of the column follows the alphabetical order of the letters present in the key
singlecolumn(word, key, rm.blanks = TRUE)
singlecolumn(word, key, rm.blanks = TRUE)
word |
Word or phrase to be encrypted |
key |
word key: for example, the key "bcea" suggests that the column order is "2-3-4-1" |
rm.blanks |
Should spaces between words be removed? By default set to 'TRUE' |
a string
https://www.geeksforgeeks.org/columnar-transposition-cipher/
singlecolumn("This is wikipedia", "cipher")
singlecolumn("This is wikipedia", "cipher")
The trifid cipher is an encryption method that uses a 3-dimensional grid It was invented by Felix Delastelle in 1902. As a 3x3x3 grid is used, 27 character are needed. Thus, we use all the 26 alphabet letter and add the "+" sign at the bottom.
trifid_delastelle(input, key = "", period = 100, encrypt = TRUE)
trifid_delastelle(input, key = "", period = 100, encrypt = TRUE)
input |
Word or phrase to be encrypted, or character vector with the sequence of coordinate numbers if we need to decrypt |
key |
key Word for creating the modified Polybius square |
period |
period length for splitting the input phrase. If greater or equal to the length of the input then the split is not executed |
encrypt |
If 'TRUE' (default), the program ciphers the input word, If 'FALSE', the program decrypts it. |
a string
https://en.wikipedia.org/wiki/Trifid_cipher
trifid_delastelle("secret", key = "", period = 5, encrypt = TRUE) trifid_delastelle("sjlkzt", key = "", period = 5, encrypt = FALSE)
trifid_delastelle("secret", key = "", period = 5, encrypt = TRUE) trifid_delastelle("sjlkzt", key = "", period = 5, encrypt = FALSE)
Vigenère cipher is a method of encrypting alphabetic text where each letter of the plaintext is encoded with a different Caesar cipher, whose increment is determined by the corresponding letter the key
vigenere(word, key, encrypt = TRUE)
vigenere(word, key, encrypt = TRUE)
word |
Word or phrase to be encrypted |
key |
character key |
encrypt |
If 'TRUE' (default), the program ciphers the input word, If 'FALSE', the program decrypts it. |
a string
https://en.wikipedia.org/wiki/Vigen
vigenere("hello world", "opla")
vigenere("hello world", "opla")