0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pythonでシーザー暗号を作成、復号する

Posted at
# substitute PLAIN the letter
PLAIN = "EBG KVVV vf n fvzcyr yrggre fhofgvghgvba pvcure gung ercynprf n yrggre jvgu gur yrggre KVVV yrggref nsgre vg va gur nycunorg. EBG KVVV vf na rknzcyr bs gur Pnrfne pvcure, qrirybcrq va napvrag Ebzr. Synt vf SYNTFjmtkOWFNZdjkkNH. Vafreg na haqrefpber vzzrqvngryl nsgre SYNT."

for i in range(26):
    KEY = i
    enc = ""

    for char in list(PLAIN):
        ASCII = ord(char)
        if (ASCII == 32):                   # if ASCII is SPC, make space.
            enc += " "
            continue
        if (122 >= ASCII and ASCII >= 97):  # if ASCII is lower
            num = ASCII - 97
            num = (num + KEY) % 26
            ASCII = num + 97
            enc += chr(ASCII)
        elif (90 >= ASCII and ASCII >= 65): # if ASCII is upper
            num = ASCII - 65
            num = (num + KEY) % 26
            ASCII = num + 65
            enc += chr(ASCII)
        else :                              # if ASCII is symbol
            enc += chr(ASCII)

    print(f"--------- Shifted {i} character ---------")
    print(enc)
    print("")

使いたい方はこのコードをコピーし、適当な名前を付けて保存してください。拡張子を「.py」にするのをお忘れなく(例、test.py)

そのあと、コードの一番上にあるPLAIN変数の中身を暗号にしたい文字や解読したい文字にしてください。(例、PLAIN = "hogehoge")

その状態で保存し、実行しします。

# python test.py

そうすると、1~25文字ずらした文字列を表示されます。(アルファベットは全部で26文字です。)

0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?