2017年12月24日 星期日

b010: 編碼破解

b010: 編碼破解


'''
在第二次世界大戰中,德軍的通訊編碼被美國破解,
以致於機密被美國竊聽而慘敗。
假設現在有一種簡易的文字編碼規則如下:
將訊息每個字母往後推兩位再傳出去,
例如 A→C、B→D,而後面的 Y→A、Z→B,
所有的訊息都是大寫字母。現在給你編過碼的文字,
請你解讀回原本的訊息。

輸入說明:
輸入一個字串,代表要破解的文字編碼。
輸出說明:
請輸出解碼後的文字。

範例輸入: 
輸入1:
UVQR
輸入2:
FWVA

範例輸出 :
輸出1:
STOP
輸出2:
DUTY
'''
# convert a character to a integer in Python
#  chr(97) 'a'
#  ord('a') 97

#How to convert list to string [duplicate]
# list1 = ['1', '2', '3']
# str1 = ''.join(list1)

#Convert string to list
# string = '[1,2,3]'
# string = string[1:-1].split(',')

"""
convert a string to a list

strX3=[]
for char in strX2:
            strX3+= char
print(strX3)
"""
import string
#Python判斷字符串是否純英文(純ASCII碼字符)
def judge_pure_english(keyword):
            return keyword.isalpha()

#=================================
print("ASCII", string.ascii_uppercase[:26])
strX1="1"
while judge_pure_english(strX1) != True:
            strX1=str(input("輸入一個字串,代表要破解的文字編碼 > "))
            strX2=strX1.upper()

strX3=list(strX2)
print(strX3)

for i in range (0,len(strX3)):
            
            if strX3[i]=='B' :
                        strX3[i]='Z'
            elif strX3[i]=='A' :
                        strX3[i]='Y'
            else:             
                        strX3[i]=str(chr(ord(strX3[i])-2))

print(strX3)

string_of_lists=""
for i in strX3:
    string_of_lists += str(i)

print(string_of_lists)

            
         


====================== RESTART: F:/Python_APSC/b010.py ======================
ASCII ABCDEFGHIJKLMNOPQRSTUVWXYZ
輸入一個字串,代表要破解的文字編碼 > abcdefghijklmnopqrstuuvwxyz
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'U', 'V', 'W', 'X', 'Y', 'Z']
['Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'S', 'T', 'U', 'V', 'W', 'X']
YZABCDEFGHIJKLMNOPQRSSTUVWX
>>>
====================== RESTART: F:/Python_APSC/b010.py ======================
ASCII ABCDEFGHIJKLMNOPQRSTUVWXYZ
輸入一個字串,代表要破解的文字編碼 > UVQR
['U', 'V', 'Q', 'R']
['S', 'T', 'O', 'P']
STOP
>>>
====================== RESTART: F:/Python_APSC/b010.py ======================
ASCII ABCDEFGHIJKLMNOPQRSTUVWXYZ
輸入一個字串,代表要破解的文字編碼 > FWVA
['F', 'W', 'V', 'A']
['D', 'U', 'T', 'Y']
DUTY
>>>

沒有留言:

張貼留言

2024年4月24日 星期三 Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --2

 2024年4月24日 星期三 Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --2 AngularJS 實例 <!DOCTYPE html> <html> <head> &...