def test_frequency(): cipher = "CGZNL YJBEN QYDLQ ZQSUQ NZCYD SNQVU BFGBK GQUQZ QSUQN UZCYD SNJDS UDCXJ ZCYDS NZQSU QNUZB WSBNZ QSUQN UDCXJ CUBGS BXJDS UCTYV SUJQG WTBUJ KCWSV LFGBK GSGZN LYJCB GJSZD GCHMS UCJCU QJLYS BXUMA UJCJM JCBGZ CYDSN CGKDC ZDSQZ DVSJJ SNCGJ DSYVQ CGJSO JCUNS YVQZS WALQV SJJSN UBTSX COSWG MTASN BXYBU CJCBG UWBKG JDSQV YDQAS JXBNS OQTYV SKCJD QUDCX JBXQK BMVWA SNSYV QZSWA LWAKB MVWAS ZBTSS QGWUB BGJDS TSJDB WCUGQ TSWQX JSNRM VCMUZ QSUQN KDBMU SWCJJ BZBTT MGCZQ JSKCJ DDCUE SGSNQ VUJDS SGZNL YJCBG UJSYY SNXBN TSWAL QZQSU QNZCY DSNCU BXJSG CGZBN YBNQJ SWQUY QNJBX TBNSZ BTYVS OUZDS TSUUM ZDQUJ DSICE SGNSZ CYDSN QGWUJ CVVDQ UTBWS NGQYY VCZQJ CBGCG JDSNB JULUJ STQUK CJDQV VUCGE VSQVY DQASJ UMAUJ CJMJC BGZCY DSNUJ DSZQS UQNZC YDSNC USQUC VLANB FSGQG WCGYN QZJCZ SBXXS NUSUU SGJCQ VVLGB ZBTTM GCZQJ CBGUS ZMNCJ LUDQF SUYSQ NSYNB WMZSW TBUJB XDCUF GBKGK BNFAS JKSSG QGWDC USQNV LYVQL UKSNS TQCGV LZBTS WCSUQ GWDCU JBNCS UESGN SUDSN QCUSW JBJDS YSQFB XUBYD CUJCZ QJCBG QGWQN JCUJN LALJD SSGWB XJDSU COJSS GJDZS GJMNL GSOJD SKNBJ STQCG VLJNQ ESWCS UMGJC VQABM JCGZV MWCGE DQTVS JFCGE VSQNQ GWTQZ ASJDZ BGUCW SNSWU BTSBX JDSXC GSUJS OQTYV SUCGJ DSSGE VCUDV QGEMQ ESCGD CUVQU JYDQU SDSKN BJSJN QECZB TSWCS UQVUB FGBKG QUNBT QGZSU QGWZB VVQAB NQJSW KCJDB JDSNY VQLKN CEDJU TQGLB XDCUY VQLUK SNSYM AVCUD SWCGS WCJCB GUBXI QNLCG EHMQV CJLQG WQZZM NQZLW MNCGE DCUVC XSJCT SQGWC GJKBB XDCUX BNTSN JDSQJ NCZQV ZBVVS QEMSU YMAVC UDSWJ DSXCN UJXBV CBQZB VVSZJ SWSWC JCBGB XDCUW NQTQJ CZKBN FUJDQ JCGZV MWSWQ VVAMJ JKBBX JDSYV QLUGB KNSZB EGCUS WQUUD QFSUY SQNSU" cipher = "QVJDB MEDGB QJJSG WQGZS NSZBN WUXBN JDSYS NCBWU MNICI STBUJ ACBEN QYDSN UQENS SJDQJ UDQFS UYSQN SKQUS WMZQJ SWQJJ DSFCG EUGSK UZDBB VCGUJ NQJXB NWQXN SSUZD BBVZD QNJSN SWCGQ ABMJQ HMQNJ SNBXQ TCVSX NBTDC UDBTS ENQTT QNUZD BBVUI QNCSW CGHMQ VCJLW MNCGE JDSSV CPQAS JDQGS NQAMJ JDSZM NNCZM VMTKQ UWCZJ QJSWA LVQKJ DNBME DBMJS GEVQG WQGWJ DSUZD BBVKB MVWDQ ISYNB ICWSW QGCGJ SGUCI SSWMZ QJCBG CGVQJ CGENQ TTQNQ GWJDS ZVQUU CZUQJ JDSQE SBXUD QFSUY SQNST QNNCS WJDSL SQNBV WQGGS DQJDQ KQLJD SZBGU CUJBN LZBMN JBXJD SWCBZ SUSBX KBNZS UJSNC UUMSW QTQNN CQESV CZSGZ SBGGB ISTAS NJKBB XDQJD QKQLU GSCED ABMNU YBUJS WABGW UJDSG SOJWQ LQUUM NSJLJ DQJJD SNSKS NSGBC TYSWC TSGJU JBJDS TQNNC QESJD SZBMY VSTQL DQISQ NNQGE SWJDS ZSNST BGLCG UBTSD QUJSU CGZSJ DSKBN ZSUJS NZDQG ZSVVB NQVVB KSWJD STQNN CQESA QGGUJ BASNS QWBGZ SCGUJ SQWBX JDSMU MQVJD NSSJC TSUQG GSUYN SEGQG ZLZBM VWDQI SASSG JDSNS QUBGX BNJDC UUCOT BGJDU QXJSN JDSTQ NNCQE SUDSE QISAC NJDJB QWQME DJSNU MUQGG QKDBK QUAQY JCUSW BGTQL JKCGU UBGDQ TGSJQ GWWQM EDJSN RMWCJ DXBVV BKSWQ VTBUJ JKBLS QNUVQ JSNQG WKSNS AQYJC USWBG XSANM QNLDQ TGSJW CSWBX MGFGB KGZQM USUQJ JDSQE SBXQG WKQUA MNCSW BGQME MUJQX JSNJD SACNJ DBXJD SJKCG UJDSN SQNSX SKDCU JBNCZ QVJNQ ZSUBX UDQFS UYSQN SMGJC VDSCU TSGJC BGSWQ UYQNJ BXJDS VBGWB GJDSQ JNSUZ SGSCG ASZQM USBXJ DCUEQ YUZDB VQNUN SXSNJ BJDSL SQNUA SJKSS GQGWQ UUDQF SUYSQ NSUVB UJLSQ NUACB ENQYD SNUQJ JSTYJ CGEJB QZZBM GJXBN JDCUY SNCBW DQISN SYBNJ SWTQG LQYBZ NLYDQ VUJBN CSUGC ZDBVQ UNBKS UDQFS UYSQN SUXCN UJACB ENQYD SNNSZ BMGJS WQUJN QJXBN WVSES GWJDQ JUDQF SUYSQ NSXVS WJDSJ BKGXB NVBGW BGJBS UZQYS YNBUS ZMJCB GXBNW SSNYB QZDCG EQGBJ DSNSC EDJSS GJDZS GJMNL UJBNL DQUUD QFSUY SQNSU JQNJC GEDCU JDSQJ NCZQV ZQNSS NTCGW CGEJD SDBNU SUBXJ DSQJN SYQJN BGUCG VBGWB GRBDG QMANS LNSYB NJSWJ DQJUD QFSUY SQNSD QWASS GQZBM GJNLU ZDBBV TQUJS NUBTS JKSGJ CSJDZ SGJMN LUZDB VQNUD QISUM EESUJ SWJDQ JUDQF SUYSQ NSTQL DQISA SSGST YVBLS WQUQU ZDBBV TQUJS NALQV SOQGW SNDBE DJBGB XVQGZ QUDCN SQZQJ DBVCZ VQGWB KGSNK DBGQT SWQZS NJQCG KCVVC QTUDQ FSUDQ XJSCG DCUKC VVGBS ICWSG ZSUMA UJQGJ CQJSU UMZDU JBNCS UBJDS NJDQG DSQNU QLZBV VSZJS WQXJS NDCUW SQJD" # cipher = "DSNSM YBGVS ENQGW QNBUS KCJDQ ENQIS QGWUJ QJSVL QCNQG WANBM EDJTS JDSAS SJVSX NBTQE VQUUZ QUSCG KDCZD CJKQU SGZVB USWCJ KQUQA SQMJC XMVUZ QNQAQ SMUQG WQJJD QJJCT SMGFG BKGJB GQJMN QVCUJ UBXZB MNUSQ ENSQJ YNCPS CGQUZ CSGJC XCZYB CGJBX ICSKJ DSNSK SNSJK BNBMG WAVQZ FUYBJ UGSQN BGSSO JNSTC JLBXJ DSAQZ FQGWQ VBGEB GSGSQ NJDSB JDSNJ DSUZQ VSUKS NSSOZ SSWCG EVLDQ NWQGW EVBUU LKCJD QVVJD SQYYS QNQGZ SBXAM NGCUD SWEBV WJDSK SCEDJ BXJDS CGUSZ JKQUI SNLNS TQNFQ AVSQG WJQFC GEQVV JDCGE UCGJB ZBGUC WSNQJ CBGCZ BMVWD QNWVL AVQTS RMYCJ SNXBN DCUBY CGCBG NSUYS ZJCGE CJ" cipher = "KSVVW BGSJD SVSIS VXBMN YQUUK BNWCU ANMJS" f1 = frequency.frequency_count(cipher, 1) frequency.display_table(f1) # f2 = frequency.frequency_count(cipher, 2) # frequency.display_table(f2) # f3 = frequency.frequency_count(cipher, 3) # frequency.display_table(f3) # f4 = frequency.frequency_count(cipher, 4) # frequency.display_table(f4) key = { 'A': 'Q', 'B': 'A', 'C': 'Z', 'D': 'W', 'E': 'S', 'F': 'X', 'G': 'E', 'H': 'D', 'I': 'C', 'J': 'R', 'K': 'F', 'L': 'V', 'M': 'T', 'N': 'G', 'O': 'B', 'P': 'Y', 'Q': 'H', 'R': 'N', 'S': 'U', 'T': 'J', 'U': 'M', 'V': 'I', 'X': 'O', 'W': 'K', 'Y': 'L', 'Z': 'P' } print substitution.decrypt(key, cipher)
def decrypt(key, cipher): return substitution.decrypt(key, cipher)
def task3(): encrypted_data = read('c3_subst_c_all.png') decrypted_data = substitution.decrypt(encrypted_data) write('./imgs/task3_decrypted.png', decrypted_data)
def main(): ciphers = 'caesar (c), railfence (r), playfair (p)\ substitution (su), beale (b), straddle (st)' while True: cmd = input('encrypt, decrypt, or exit (en, de, ex)? ') # ENCRYPT while cmd == 'en': enc = input('select cipher: \n \ caesar (c), railfence (r), playfair (p) \n \ substitution (su), beale (b), straddle (st): \n') # caesar if enc == 'c': t = input('enter plain text: ') if t != '' and utils.check_latin(t): pass else: print('text can only consist of latin letters') break k = input('enter integer key: ') if utils.check_int(k): print(caesar.encrypt(t, int(k))) cmd = '' else: cmd = '' # railfence elif enc == 'r': t = input('enter plain text: ') k = input('enter integer key: ') if utils.check_int(k): if int(k) > 1: print(railfence.encrypt(t, int(k))) cmd = '' else: print('key must be at least 2') cmd = '' else: cmd = '' # playfair elif enc == 'p': t = input('enter plain text: ') if t != '' and utils.check_latin(t): pass else: print('text can only consist of latin letters') break k = input('enter string key: ') if k != '' and utils.check_latin(k): print(playfair.encrypt(t, k)) pass else: print('key can only consist of latin letters') break cmd = '' # substitution elif enc == 'su': t = input('enter plain text: ') if t != '' and utils.check_latin(t): pass else: print('text can only consist of latin letters') break k = input('enter string key: ') if k != '' and utils.check_latin(k): try: utils.check_full(k) except ValueError: print('key should bee full alphabet') break try: print(substitution.encrypt(t, k)) except ValueError: print('check errors for parameter issues') pass else: print('key can only consist of latin letters') break cmd = '' # beale elif enc == 'b': t = input('enter plain text: ') if t != '' and utils.check_latin(t): pass else: print('text can have letters + punctuation') break k = input('enter file name key: ') if k != '': pass s = input('enter an integer seed: ') if s != '': try: s = int(s) except ValueError: print('seed must be integer') break try: print(beale.encrypt(t, k, s)) except ValueError: print('make sure your file is correct') pass else: print('text cannot be empty') break cmd = '' # straddle elif enc == 'st': t = input('enter plain text: ') warn = 0 for x in t: if (t not in string.ascii_letters and t != '0' and t != '1' and t != ' '): warn += 1 if warn > 0: print('warning: text should include only letters, 0, or 1') if t != '': pass else: print('text cannot be empty') break alk = input('enter alpha key: ') if alk != '': pass else: print('alpha_key cannot be empty') break try: utils.check_full(alk) except ValueError: print('alpha_key must be full alphabet') break nk = input('enter 2 digit numeric key: ') if nk != '': pass if (len(nk) != 2 or not nk[0].isnumeric() or not nk[1].isnumeric() or '0' in nk): print('num_key must be a 2 digit int with unique digits') print('cannot contain 0') break adk = input('enter adding key:') if adk != '': try: print(straddle.encrypt(t, alk, nk, adk)) except ValueError: print('make sure your add key is an integer') pass else: print('input cannot be empty') break cmd = '' else: print('Please enter a valid input for a cipher.') # DECRYPT while cmd == 'de': dec = input('select cipher: \n \ caesar (c), railfence (r), playfair (p) \n \ substitution (su), beale (b), straddle (st): \n') # caesar if dec == 'c': t = input('enter cipher text: ') if t != '' and utils.check_latin(t): pass else: print('text can only consist of latin letters') break k = input('enter integer key: ') if utils.check_int(k): print(caesar.decrypt(t, int(k))) cmd = '' else: cmd = '' # railfence elif dec == 'r': t = input('enter cipher text: ') k = input('enter integer key: ') if utils.check_int(k): if int(k) > 1: print(railfence.decrypt(t, int(k))) cmd = '' else: print('key must be at least 2') cmd = '' else: cmd = '' # playfair elif dec == 'p': t = input('enter cipher text: ') if t != '' and utils.check_latin(t): pass else: print('text can only consist of latin letters') break k = input('enter string key: ') if k != '' and utils.check_latin(k): print(playfair.decrypt(t, k)) pass else: print('text can only consist of latin letters') break cmd = '' # substitution elif dec == 'su': t = input('enter cipher text: ') if t != '' and utils.check_latin(t): pass else: print('text can only consist of latin letters') break k = input('enter string key: ') if k != '' and utils.check_latin(k): try: utils.check_full(k) except ValueError: print('key should be full alphabet') break try: print(substitution.decrypt(t, k)) except ValueError: print('check errors for parameter issues') pass else: print('key can only consist of latin letters') break cmd = '' # beale elif dec == 'b': t = input('enter cipher text: ') for x in t: if x != ' ': if not x.isnumeric(): print('text should consist of int literals') break if t != '': pass else: print('warning: text should only consist of integers') break k = input('enter file name key: ') if k != '': try: print(beale.decrypt(t, k)) except ValueError: print('check that your text file is correct') pass else: print('text cannot be empty') break cmd = '' # straddle elif dec == 'st': t = input('enter cipher text: ') warn = 0 for x in t: if (t not in string.ascii_letters and t != '0' and t != '1' and t != ' '): warn += 1 if warn > 0: print('warning: text should include only letters, 0, or 1') if t != '': pass else: print('text cannot be empty') break alk = input('enter alpha key: ') if alk != '': pass else: print('alpha_key cannot be empty') break try: utils.check_full(alk) except ValueError: print('alpha_key must be full alphabet') break nk = input('enter 2 digit numeric key: ') if nk != '': pass if (len(nk) != 2 or not nk[0].isnumeric() or not nk[1].isnumeric() or '0' in nk): print('num_key must be a 2 digit int with unique digits') print('cannot contain 0') break adk = input('enter adding key:') if adk != '': try: print(straddle.decrypt(t, alk, nk, adk)) except ValueError: print('make sure your add key is an integer') pass else: print('text cannot be empty') break cmd = '' else: print('Please enter a valid input for a cipher.') if cmd == 'ex': print('exit') return False elif cmd == '' or cmd == 'en' or cmd == 'de': pass else: print('Please enter a valid input of en, de, or ex.')