練習問題

a = 'abcdefghijklmnopqrstuvwxyz .,-'
s = 'qdq-gi.q-a ziatmxxitmdqibtqi-ustbi ri.qmoqrcxi.qbubu zir -ibtqi-qp-qaai ripmymsqkir -ibtqi-qy dmxi ri.cnxuoi rruoumxakir -ibtqiqzmobyqzbki-q.qmxi -imyqzpyqzbi rixmeaki -puzmzoqai -i-qscxmbu zaimzpir -i btq-iymbbq-a;iz -iatmxximzgi.q-a zinqiuzimzgiemgipuao-uyuzmbqpimsmuzabir -ia. za -uzsiacotimi.qbubu zj'
for i in range(len(a)):
    candidate = ''.join(';' if e==';' else a[(a.find(e) + i) % len(a)] for e in s)
    if 'person' in candidate:
        print(candidate)
        break

#初心者用

table_str = "abcdefghijklmnopqrstuvwxyz .,-abcdefghijklmnopqrstuvwxyz .,-"
encrypted = "qdq-gi.q-a ziatmxxitmdqibtqi-ustbi ri.qmoqrcxi.qbubu zir -ibtqi-qp-qaai ripmymsqkir -ibtqi-qy dmxi ri.cnxuoi rruoumxakir -ibtqiqzmobyqzbki-q.qmxi -imyqzpyqzbi rixmeaki -puzmzoqai -i-qscxmbu zaimzpir -i btq-iymbbq-a;iz -iatmxximzgi.q-a zinqiuzimzgiemgipuao-uyuzmbqpimsmuzabir -ia. za -uzsiacotimi.qbubu zj"

table = []
for c in table_str:
    table.append(c)
# table は ['a', 'b', ...] になった

needle_chars = ["p", "e", "r", "s", "o", "n"]
for stride in range(1, len(table)//2):
    # "person"をstride幅で暗号化
    needle_enc = ""
    for c1 in needle_chars:
        idx = table.index(c1)
        needle_enc += table[idx + stride]
        
    # 問題の暗号文が暗号化済"person"を含んでいれば勝ち
    if needle_enc in encrypted:
        decrypted = ""
        for c2 in encrypted:
            if c2 == ";":
                decrypted += ";"
            else:
                idx2 = table.index(c2)
                decrypted += table[idx2 - stride] # pythonはリスト番号がボチボチ負でもOK
        print(decrypted)
        break

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2023-02-23 (木) 23:33:35