-
[정보보호개론] Substitution Cipher (치환 암호 / 카이사르 암호) 해독 코드Study/Python3 2021. 3. 22. 19:18반응형
파이썬으로 구현했습니다. 문제는 다음과 같습니다.
Decrypt the message encrypted using a simple substitution cipher
주어진 문자열
'APS ZU BMS THAAMT KB SOP CHAAPJ MQ LPUWHKX. K UHJ SM JMZ SMLHJ VJ QXKPBLU -- UM PCPB SOMZDO TP QHEP SOP LKQQKEZASKPU MQ SMLHJ HBL SMVMXXMT, K USKAA OHCP H LXPHV. KS KU H LXPHV LPPWAJ XMMSPL KB SOP HVPXKEHB LXPHV. K OHCP H LXPHV SOHS MBP LHJ SOKU BHSKMB TKAA XKUP ZW HBL AKCP MZS SOP SXZP VPHBKBD MQ KSU EXPPL: "TP OMAL SOPUP SXZSOU SM IP UPAQ-PCKLPBS, SOHS HAA VPB HXP EXPHSPL PGZHA." K OHCP H LXPHV SOHS MBP LHJ MB SOP XPL OKAAU MQ DPMXDKH SOP UMBU MQ QMXVPX UAHCPU HBL SOP UMBU MQ QMXVPX UAHCP MTBPXU TKAA IP HIAP SM UKS LMTB SMDPSOPX HS SOP SHIAP MQ IXMSOPXOMML. K OHCP H LXPHV SOHS MBP LHJ PCPB SOP USHSP MQ VKUUKUUKWWK, H USHSP UTPASPXKBD TKSO SOP OPHS MQ KBFZUSKEP, UTPASPXKBD TKSO SOP OPHS MQ MWWXPUUKMB, TKAA IP SXHBUQMXVPL KBSM HB MHUKU MQ QXPPLMV HBL FZUSKEP. K OHCP H LXPHV SOHS VJ QMZX AKSSAP EOKALXPB TKAA MBP LHJ AKCP KB H BHSKMB TOPXP SOPJ TKAA BMS IP FZLDPL IJ SOP EMAMX MQ SOPKX URKB IZS IJ SOP EMBSPBS MQ SOPKX EOHXHESPX. K OHCP H LXPHV SMLHJ. K OHCP H LXPHV SOHS MBP LHJ LMTB KB HAHIHVH, TKSO KSU CKEKMZU XHEKUSU, TKSO KSU DMCPXBMX OHCKBD OKU AKWU LXKWWKBD TKSO SOP TMXLU MQ KBSPXWMUKSKMB HBL BZAAKQKEHSKMB -- MBP LHJ XKDOS SOPXP KB HAHIHVH AKSSAP IAHER IMJU HBL IAHER DKXAU TKAA IP HIAP SM FMKB OHBLU TKSO AKSSAP TOKSP IMJU HBL TOKSP DKXAU HU UKUSPXU HBL IXMSOPXU. K OHCP H LXPHV SMLHJ. K OHCP H LXPHV SOHS MBP LHJ PCPXJ CHAAPJ UOHAA IP PNHASPL, HBL PCPXJ OKAA HBL VMZBSHKB UOHAA IP VHLP AMT, SOP XMZDO WAHEPU TKAA IP VHLP WAHKB, HBL SOP EXMMRPL WAHEPU TKAA IP VHLP USXHKDOS, HBL SOP DAMXJ MQ SOP AMXL UOHAA IP XPCPHAPL HBL HAA QAPUO UOHAA UPP KS
SMDPSOPX.힌트 (알파벳 사용 빈도수 테이블)
코드 (Python)
def printAll(): # 출력부 print("[Current Text]") print(encryptedString) print("[Frequency Count Table]") for i in range(26): print(alphabet[i] + "\t", end="") print("") for i in range(26): print(str(alphabetCounter[i]) + "\t", end="") print("\n[Key Table]") for i in range(26): print(alphabet[i] + "->" + changedAlphabet[i] + "\t", end="") if i%13==12: print("") print("") def countAll(): for i in list_str: if ord(i) > 64 and ord(i) < 91: alphabetCounter[ord(i) - 65] += 1 def clearAll(): for i in range(26): alphabetCounter[i]=0 def swapAB(): print("[Swap]") a=input("A: ") b=input("B: ") if ord(a)<65 or ord(a)>90: a=a.upper() if ord(b)<65 or ord(b)>90: b=b.upper() index=0 for i in list_str: if i==a: list_str[index]=b if i==b: list_str[index]=a index+=1 returnString="" for i in list_str: returnString = returnString + i print("swap is complete") return returnString def updateKeyMap(): index = 0 for i in original_list_str: if list_str[index]!=i: changedAlphabet[ord(i)-65]=list_str[index] index += 1 encryptedString = 'APS ZU BMS THAAMT KB SOP CHAAPJ MQ LPUWHKX. K UHJ SM JMZ SMLHJ VJ QXKPBLU -- UM PCPB SOMZDO TP QHEP SOP LKQQKEZASKPU MQ SMLHJ HBL SMVMXXMT, K USKAA OHCP H LXPHV. KS KU H LXPHV LPPWAJ XMMSPL KB SOP HVPXKEHB LXPHV. K OHCP H LXPHV SOHS MBP LHJ SOKU BHSKMB TKAA XKUP ZW HBL AKCP MZS SOP SXZP VPHBKBD MQ KSU EXPPL: "TP OMAL SOPUP SXZSOU SM IP UPAQ-PCKLPBS, SOHS HAA VPB HXP EXPHSPL PGZHA." K OHCP H LXPHV SOHS MBP LHJ MB SOP XPL OKAAU MQ DPMXDKH SOP UMBU MQ QMXVPX UAHCPU HBL SOP UMBU MQ QMXVPX UAHCP MTBPXU TKAA IP HIAP SM UKS LMTB SMDPSOPX HS SOP SHIAP MQ IXMSOPXOMML. K OHCP H LXPHV SOHS MBP LHJ PCPB SOP USHSP MQ VKUUKUUKWWK, H USHSP UTPASPXKBD TKSO SOP OPHS MQ KBFZUSKEP, UTPASPXKBD TKSO SOP OPHS MQ MWWXPUUKMB, TKAA IP SXHBUQMXVPL KBSM HB MHUKU MQ QXPPLMV HBL FZUSKEP. K OHCP H LXPHV SOHS VJ QMZX AKSSAP EOKALXPB TKAA MBP LHJ AKCP KB H BHSKMB TOPXP SOPJ TKAA BMS IP FZLDPL IJ SOP EMAMX MQ SOPKX URKB IZS IJ SOP EMBSPBS MQ SOPKX EOHXHESPX. K OHCP H LXPHV SMLHJ. K OHCP H LXPHV SOHS MBP LHJ LMTB KB HAHIHVH, TKSO KSU CKEKMZU XHEKUSU, TKSO KSU DMCPXBMX OHCKBD OKU AKWU LXKWWKBD TKSO SOP TMXLU MQ KBSPXWMUKSKMB HBL BZAAKQKEHSKMB -- MBP LHJ XKDOS SOPXP KB HAHIHVH AKSSAP IAHER IMJU HBL IAHER DKXAU TKAA IP HIAP SM FMKB OHBLU TKSO AKSSAP TOKSP IMJU HBL TOKSP DKXAU HU UKUSPXU HBL IXMSOPXU. K OHCP H LXPHV SMLHJ. K OHCP H LXPHV SOHS MBP LHJ PCPXJ CHAAPJ UOHAA IP PNHASPL, HBL PCPXJ OKAA HBL VMZBSHKB UOHAA IP VHLP AMT, SOP XMZDO WAHEPU TKAA IP VHLP WAHKB, HBL SOP EXMMRPL WAHEPU TKAA IP VHLP USXHKDOS, HBL SOP DAMXJ MQ SOP AMXL UOHAA IP XPCPHAPL HBL HAA QAPUO UOHAA UPP KS SMDPSOPX.' list_str = list(encryptedString) original_list_str = list(encryptedString) alphabet=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] alphabetCounter=[] changedAlphabet=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] for i in range(26): alphabetCounter.append(0) while True: clearAll() # 매번 새로 countAll() #카운트 해서 printAll() #결과를 보여줌 encryptedString=swapAB() #swap updateKeyMap()
실행
해독 결과
[Current Text]
LET US NOT WALLOW IN THE VALLEY OF DESPAIR. I SAY TO YOU TODAY MY FRIENDS -- SO EVEN THOUGH WE FACE THE DIFFICULTIES OF TODAY AND TOMORROW, I STILL HAVE A DREAM. IT IS A DREAM DEEPLY ROOTED IN THE AMERICAN DREAM. I HAVE A DREAM THAT ONE DAY THIS NATION WILL RISE UP AND LIVE OUT THE TRUE MEANING OF ITS CREED: "WE HOLD THESE TRUTHS TO BE SELF-EVIDENT, THAT ALL MEN ARE CREATED EQUAL." I HAVE A DREAM THAT ONE DAY ON THE RED HILLS OF GEORGIA THE SONS OF FORMER SLAVES AND THE SONS OF FORMER SLAVE OWNERS WILL BE ABLE TO SIT DOWN TOGETHER AT THE TABLE OF BROTHERHOOD. I HAVE A DREAM THAT ONE DAY EVEN THE STATE OF MISSISSIPPI, A STATE SWELTERING WITH THE HEAT OF INJUSTICE, SWELTERING WITH THE HEAT OF OPPRESSION, WILL BE TRANSFORMED INTO AN OASIS OF FREEDOM AND JUSTICE. I HAVE A DREAM THAT MY FOUR LITTLE CHILDREN WILL ONE DAY LIVE IN A NATION WHERE THEY WILL NOT BE JUDGED BY THE COLOR OF THEIR SKIN BUT BY THE CONTENT OF THEIR CHARACTER. I HAVE A DREAM TODAY. I HAVE A DREAM THAT ONE DAY DOWN IN ALABAMA, WITH ITS VICIOUS RACISTS, WITH ITS GOVERNOR HAVING HIS LIPS DRIPPING WITH THE WORDS OF INTERPOSITION AND NULLIFICATION -- ONE DAY RIGHT THERE IN ALABAMA LITTLE BLACK BOYS AND BLACK GIRLS WILL BE ABLE TO JOIN HANDS WITH LITTLE WHITE BOYS AND WHITE GIRLS AS SISTERS AND BROTHERS. I HAVE A DREAM TODAY. I HAVE A DREAM THAT ONE DAY EVERY VALLEY SHALL BE EXALTED, AND EVERY HILL AND MOUNTAIN SHALL BE MADE LOW, THE ROUGH PLACES WILL BE MADE PLAIN, AND THE CROOKED PLACES WILL BE MADE STRAIGHT, AND THE GLORY OF THE LORD SHALL BE REVEALED AND ALL FLESH SHALL SEE IT TOGETHER. 반응형'Study > Python3' 카테고리의 다른 글
[Python3] 파이썬으로 지뢰찾기(Mine Sweeper) 만들기 (0) 2021.06.21