본문 바로가기
Cryptography

[Square CTF] 카이사르 암호 암복호화

by L3m0n S0ju 2021. 8. 12.

# -*- coding: utf-8 -*-

 

catalog1 = "abcdefghijklmnopqrstuvwxyz"

catalog2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

 

def encrypt(data, key):

    rst = ""

    for i in range(len(data)):

        idx1 = catalog1.find( data[i] )

        idx2 = catalog2.find( data[i] )

        if idx1 != -1:

            rst += catalog1[ (idx1+key) % len(catalog1) ]

        elif idx2 != -1:

            rst += catalog2[ (idx2+key) % len(catalog2) ]

        else: rst += data[k]

            return rst

 

def decrypt(data, key):

    rst = ""

    for k in range(len(data)):

        idx1 = catalog1.find( data[k] )

        idx2 = catalog2.find( data[k] )

        if idx1 != -1:

            rst += catalog1[ (idx1-key) % len(catalog1) ]

        elif idx2 != -1:

            rst += catalog2[ (idx2-key) % len(catalog2) ]

        else: rst += data[k]

            return rst

 

def main():

    data = "Kyv ufdvjkzt trk (Wvczj jzcmvjkizj trklj fi Wvczj trklj) zj r jdrcc, kpgztrccp wliip, triezmfiflj drddrc. Kyvp riv fwkve trccvu yfljv trkj nyve bvgk rj zeuffi gvkj fi jzdgcp trkj nyve kyviv zj ef evvu kf uzjkzexlzjy kyvd wifd fkyvi wvczuj reu wvczevj. Trkj riv fwkve mrclvu sp yldrej wfi tfdgrezfejyzg reu wfi kyvzi rszczkp kf ylek mvidze. Kyviv riv dfiv kyre 70 trk sivvuj, kyflxy uzwwvivek rjjftzrkzfej giftcrzd uzwwvivek eldsvij rttfiuzex kf kyvzi jkreuriuj. Kyv wcrx zj kyv gyirjv nzky urjyvj: wcrx nyrk zj r ufdvjkzt trk."

 

    key = 17

    # rst = encrypt(data, key)

    rst = decrypt(data, key)

    print(rst)

 

if __name__ == "__main__":

    main()

'Cryptography' 카테고리의 다른 글

[HackCTF] Classic Cipher -2  (0) 2021.09.11
[HackCTF] Classic Cipher -1  (0) 2021.09.11
[HackCTF] Smooth CipherText  (0) 2021.09.11
[HackCTF] Great Binary  (0) 2021.09.10
[Square CTF] The General's Cat  (0) 2021.08.12

댓글