In love and war, ciphers have been used to pass secret messages. Now, we typically use strong encryption, but in days gone by, secret messages often used a cipher code. These are typically encoding methods, of translating our letters into another form, or processed using a secret key. A fun encoding cipher uses the periodic table to map letters in plaintext to the number of protons that the element has. Thus, we have ‘H’ (Hydrogen) mapped to 1, ‘He’ Helium) mapped to 2, and ‘Li’ (Lithium) mapped to 3. Thus, we have ‘H’ (Hydrogen) mapped to 1, ‘He’ Helium) mapped to 2, and ‘Li’ (Lithium) mapped to 3.
Periodic Cipher |
Theory
In love and war, ciphers have been used to pass secret messages. Now, we typically use strong encryption, but in days gone by, secret messages often used a cipher code. These are typically encoding methods, of translating our letters into another form, or processed using a secret key. A fun encoding cipher uses the periodic table to map letters in plaintext to the number of protons that the element has:
Thus, we have ‘H’ (Hydrogen) mapped to 1, ‘He’ Helium) mapped to 2, and ‘Li’ (Lithium) mapped to 3. In Python, this list can then be defined with a dictionary of:
periodic = {1:'h',2:'he',3:'li',4:'be',5:'b',6:'c',7:'n',8:'o',9:'f',10:'ne', 11:'na',12:'mg',13:'al',14:'si',15:'p',16:'s',17:'cl',18:'ar',19:'k',20:'ca', 21:'sc',22:'ti',23:'v',24:'cr',25:'mn',26:'fe',27:'co',28:'ni',29:'cu',30:'zn', 31:'ga',32:'ge',33:'as',34:'se',35:'br',36:'kr',37:'rb',38:'sr',39:'y',40:'zr', 41:'nb',42:'mo',43:'tc',44:'ru',45:'rh',46:'pd',47:'ag',48:'cd',49:'in',50:'sn', 51:'sb',52:'te',53:'i',54:'xe',55:'cs',56:'ba',71:'lu',72:'hf',73:'ta',74:'w', 75:'re',76:'os',77:'ir',78:'pt',79:'au',80:'hg',81:'tl',82:'pb',83:'bi', 84:'po',85:'at',86:'rn',87:'fr',88:'ra',103:'lr',104:'rf',105:'db',106:'sg', 107:'bh',108:'hs',109:'mt',110:'ds',111:'rg',112:'cn',113:'nh',114:'fl', 115:'mc',116:'lv',117:'ts',118:'og',57:'la',58:'ce',59:'pr',60:'nd', 61:'pm',62:'sm',63:'eu',64:'gd',65:'tb',66:'dy',67:'ho',68:'er',69:'tm', 70:'yb',89:'ac',90:'th',91:'pa',92:'u',93:'np',94:'pu',95:'am',96:'cm', 97:'bk',98:'cf',99:'es',100:'fm',101:'md',102:'no'}
Zinc (Zn) is thus ciphered as 30. Now, we see that if we have “H” in a string, it could be ciphered as 1, 2 (if it is followed by an ‘e’), 90 (if it is preceded by a ‘t’). Thus we could shuffle our dictionary, and then process in sequence:
periodic_shuf = list(periodic.keys()) random.shuffle(periodic_shuf)
In this case, we take the values of the periodic elements (as defined by the keys) and then shuffle them. We can sequence through the shuffled periodic table and replace the letters with their corresponding value:
cipher=msg for ll in periodic_shuf: cipher = cipher.replace(periodic[ll]," "+str(ll)+" ")
The full code is:
import random import sys periodic = {1:'h',2:'he',3:'li',4:'be',5:'b',6:'c',7:'n',8:'o',9:'f',10:'ne',11:'na',12:'mg',13:'al',14:'si',15:'p',16:'s',17:'cl',18:'ar',19:'k',20:'ca',21:'sc',22:'ti',23:'v',24:'cr',25:'mn',26:'fe',27:'co',28:'ni',29:'cu',30:'zn',31:'ga',32:'ge',33:'as',34:'se',35:'br',36:'kr',37:'rb',38:'sr',39:'y',40:'zr',41:'nb',42:'mo',43:'tc',44:'ru',45:'rh',46:'pd',47:'ag',48:'cd',49:'in',50:'sn',51:'sb',52:'te',53:'i',54:'xe',55:'cs',56:'ba',71:'lu',72:'hf',73:'ta',74:'w',75:'re',76:'os',77:'ir',78:'pt',79:'au',80:'hg',81:'tl',82:'pb',83:'bi',84:'po',85:'at',86:'rn',87:'fr',88:'ra',103:'lr',104:'rf',105:'db',106:'sg',107:'bh',108:'hs',109:'mt',110:'ds',111:'rg',112:'cn',113:'nh',114:'fl',115:'mc',116:'lv',117:'ts',118:'og',57:'la',58:'ce',59:'pr',60:'nd',61:'pm',62:'sm',63:'eu',64:'gd',65:'tb',66:'dy',67:'ho',68:'er',69:'tm',70:'yb',89:'ac',90:'th',91:'pa',92:'u',93:'np',94:'pu',95:'am',96:'cm',97:'bk',98:'cf',99:'es',100:'fm',101:'md',102:'no'} msg="test" if (len(sys.argv)>1): msg=str(sys.argv[1]) periodic_shuf = list(periodic.keys()) random.shuffle(periodic_shuf) cipher=msg for ll in periodic_shuf: cipher = cipher.replace(periodic[ll]," "+str(ll)+" ") print("Input: ",msg) print("Mapping: ",periodic) print("Shuffled: ",periodic_shuf) print ("\nCipher: ",cipher)
Unfortunately, not all the letters are covered, such as ‘e’ and ‘a’ in some circumstances. For, “audience”, we get:
au-> 79 d-> (no mapping) i -> 53 e -> (no mapping) n -> 7 c -> 6 e (no mapping)
This gives:
Input: audience Mapping: {1: 'h', 2: 'he', 3: 'li', 4: 'be', 5: 'b', 6: 'c', 7: 'n', 8: 'o', 9: 'f', 10: 'ne', 11: 'na', 12: 'mg', 13: 'al', 14: 'si', 15: 'p', 16: 's', 17: 'cl', 18: 'ar', 19: 'k', 20: 'ca', 21: 'sc', 22: 'ti', 23: 'v', 24: 'cr', 25: 'mn', 26: 'fe', 27: 'co', 28: 'ni', 29: 'cu', 30: 'zn', 31: 'ga', 32: 'ge', 33: 'as', 34: 'se', 35: 'br', 36: 'kr', 37: 'rb', 38: 'sr', 39: 'y', 40: 'zr', 41: 'nb', 42: 'mo', 43: 'tc', 44: 'ru', 45: 'rh', 46: 'pd', 47: 'ag', 48: 'cd', 49: 'in', 50: 'sn', 51: 'sb', 52: 'te', 53: 'i', 54: 'xe', 55: 'cs', 56: 'ba', 71: 'lu', 72: 'hf', 73: 'ta', 74: 'w', 75: 're', 76: 'os', 77: 'ir', 78: 'pt', 79: 'au', 80: 'hg', 81: 'tl', 82: 'pb', 83: 'bi', 84: 'po', 85: 'at', 86: 'rn', 87: 'fr', 88: 'ra', 103: 'lr', 104: 'rf', 105: 'db', 106: 'sg', 107: 'bh', 108: 'hs', 109: 'mt', 110: 'ds', 111: 'rg', 112: 'cn', 113: 'nh', 114: 'fl', 115: 'mc', 116: 'lv', 117: 'ts', 118: 'og', 57: 'la', 58: 'ce', 59: 'pr', 60: 'nd', 61: 'pm', 62: 'sm', 63: 'eu', 64: 'gd', 65: 'tb', 66: 'dy', 67: 'ho', 68: 'er', 69: 'tm', 70: 'yb', 89: 'ac', 90: 'th', 91: 'pa', 92: 'u', 93: 'np', 94: 'pu', 95: 'am', 96: 'cm', 97: 'bk', 98: 'cf', 99: 'es', 100: 'fm', 101: 'md', 102: 'no'} Shuffled: [70, 29, 54, 42, 110, 88, 14, 62, 116, 1, 49, 80, 81, 90, 87, 61, 96, 2, 107, 63, 53, 26, 41, 68, 82, 23, 105, 108, 48, 86, 111, 104, 17, 109, 8, 75, 71, 118, 65, 117, 89, 114, 46, 55, 9, 31, 60, 12, 18, 97, 74, 25, 32, 34, 84, 100, 112, 45, 21, 43, 69, 50, 28, 20, 47, 98, 38, 103, 3, 52, 115, 94, 83, 40, 19, 39, 33, 99, 95, 15, 37, 11, 35, 27, 7, 57, 67, 36, 6, 91, 13, 76, 85, 78, 44, 79, 92, 77, 16, 101, 59, 24, 10, 4, 73, 58, 5, 72, 66, 113, 51, 56, 93, 106, 30, 22, 102, 64] Cipher: 79 d 53 e 7 6 e
But depending on how we shuffle, we could also get [here]:
au-> 79 d-> (no mapping) i -> 53 e -> (no mapping) n -> 7 ce -> 58
As defined by:
Input: audience Mapping: {1: 'h', 2: 'he', 3: 'li', 4: 'be', 5: 'b', 6: 'c', 7: 'n', 8: 'o', 9: 'f', 10: 'ne', 11: 'na', 12: 'mg', 13: 'al', 14: 'si', 15: 'p', 16: 's', 17: 'cl', 18: 'ar', 19: 'k', 20: 'ca', 21: 'sc', 22: 'ti', 23: 'v', 24: 'cr', 25: 'mn', 26: 'fe', 27: 'co', 28: 'ni', 29: 'cu', 30: 'zn', 31: 'ga', 32: 'ge', 33: 'as', 34: 'se', 35: 'br', 36: 'kr', 37: 'rb', 38: 'sr', 39: 'y', 40: 'zr', 41: 'nb', 42: 'mo', 43: 'tc', 44: 'ru', 45: 'rh', 46: 'pd', 47: 'ag', 48: 'cd', 49: 'in', 50: 'sn', 51: 'sb', 52: 'te', 53: 'i', 54: 'xe', 55: 'cs', 56: 'ba', 71: 'lu', 72: 'hf', 73: 'ta', 74: 'w', 75: 're', 76: 'os', 77: 'ir', 78: 'pt', 79: 'au', 80: 'hg', 81: 'tl', 82: 'pb', 83: 'bi', 84: 'po', 85: 'at', 86: 'rn', 87: 'fr', 88: 'ra', 103: 'lr', 104: 'rf', 105: 'db', 106: 'sg', 107: 'bh', 108: 'hs', 109: 'mt', 110: 'ds', 111: 'rg', 112: 'cn', 113: 'nh', 114: 'fl', 115: 'mc', 116: 'lv', 117: 'ts', 118: 'og', 57: 'la', 58: 'ce', 59: 'pr', 60: 'nd', 61: 'pm', 62: 'sm', 63: 'eu', 64: 'gd', 65: 'tb', 66: 'dy', 67: 'ho', 68: 'er', 69: 'tm', 70: 'yb', 89: 'ac', 90: 'th', 91: 'pa', 92: 'u', 93: 'np', 94: 'pu', 95: 'am', 96: 'cm', 97: 'bk', 98: 'cf', 99: 'es', 100: 'fm', 101: 'md', 102: 'no'} Shuffled: [90, 80, 87, 2, 113, 11, 26, 37, 71, 41, 58, 5, 74, 44, 104, 98, 76, 95, 111, 28, 112, 94, 22, 93, 110, 114, 79, 21, 36, 66, 102, 30, 52, 19, 69, 47, 3, 75, 68, 50, 83, 81, 72, 57, 42, 85, 7, 15, 100, 91, 17, 84, 51, 34, 82, 86, 67, 46, 70, 18, 78, 48, 45, 88, 56, 23, 53, 62, 32, 35, 43, 54, 1, 59, 33, 108, 118, 16, 9, 8, 13, 103, 4, 105, 55, 77, 115, 97, 101, 25, 38, 117, 89, 73, 99, 60, 96, 106, 29, 65, 64, 31, 39, 20, 27, 116, 12, 61, 10, 92, 40, 107, 6, 14, 24, 63, 109, 49] Cipher: 79 d 53 e 7 58
The deciphering is fairly easy, as we just map the numbers back to the letters in the dictionary.