zero

The Neighbor-Pair Cipher

10:37pm. The Neighbor-Pair cipher is a human operable, on-the-fly algorithm to encrypt the twenty-six alphabets of the English language. The words are converted into a string of Hindu-Arabic numerals of lengths of either 2*n+1 or 2*(n+1) where n is the number of letters in the word. For example, the word APPLE is encrypted to 10414133205. Note that this cipher does not have support for special characters or symbols, and cannot differentiate between uppercase and lowercase alphabets.

We first map the vowels of the English alphabet to a unique natural number starting from unity. Therefore,

Letter Encoding
A 1
E 2
I 3
O 4
U 5

We encrypt the word letter-by-letter where each letter is encoded into a two-digit number. The leading digit denotes the nearest vowel while the other digit denotes the distance from that vowel. We take a look at the first letter of the word and look for the nearest vowel to it if searched left-to-right. The vowel it’s nearest to is then encoded according to the map above. Then, we see how far the letter is from the vowel. This distance is set to the trailing digit.

For example, the nearest vowel to the letter C is A and its distance from A is three. So, C is encoded as 13. Similarly, the letter G comes exactly midway between E and I. But since we read from left-to-right, the nearest vowel is considered to be E. Therefore our leading digit is two. Now G is two alphabets far from E. Therefore, the trailing digit is two. Hence, G is encoded to 22. This procedure is repeated for each letter in the word and then all the numbers for each letter of the word are concatenated into one number. For example, the letters in DELHI are encoded as:

Letter Encoding
D 13
E 20
L 33
H 23
I 30

Upon concatenation, these numbers become 1320332330. As a “safety bit”, we add an extra number in the end to represent the length of the word. Since DELHI is of five letters, the final encoding becomes 13203323305.

The vice versa can be done to decode numbers into words. Moreover, it is worthy to note that for all words with less than nine letters, the length of encoding will be an odd number. The trailing digit must be ignored before translation and be used only for verification for correct translation. In case the encoded number is an even number, two trailing digits have to be used for verification as it means that the word includes more than nine letters. Below is a Python function to encode a word of less than hundred letters. The function to decode has been left as an exercise for the reader.

def encode(word: str) -> int:
    mapping = {
        "A": "10",
        "B": "11",
        "C": "12",
        "D": "13",
        "E": "20",
        "F": "21",
        "G": "22",
        "H": "23",
        "I": "30",
        "J": "31",
        "K": "32",
        "L": "33",
        "M": "34",
        "N": "35",
        "O": "40",
        "P": "41",
        "Q": "42",
        "R": "43",
        "S": "44",
        "T": "45",
        "U": "50",
        "V": "51",
        "W": "52",
        "X": "53",
        "Y": "54",
        "Z": "55"
    }

    encoding = ""
    for i in word:
        try:
            encoded_char = mapping[i.upper()]
        except KeyError:
            raise TypeError("Invalid characters found in passed string. Cannot encode.")
        encoding+=encoded_char

    word_length = len(word)
    if word_length < 10:
        return int(encoding) * 10 + word_length
    elif word_length < 100:
        return int(encoding) * 100 + word_length


    raise ValueError("Keyword size out of range.")

#blog #code