์ƒˆ์†Œ์‹

Languages/Python

[์•”ํ˜ธ ํ”„๋กœํ† ์ฝœ] ์‹œ์ €์•”ํ˜ธ ์‚ฌ์ „ | ์‹œ์ €์•”ํ˜ธ

  • -
๋ฐ˜์‘ํ˜•

https://www.itworld.co.kr/tags/2784/%EC%95%94%ED%98%B8%ED%99%94/132494

 


์นด์ด์‚ฌ๋ฅด(์‹œ์ €)์•”ํ˜ธ

์ด์ „ ์‹œ๊ฐ„์— ์ฝ”๋“œ๋ฅผ ์งœ๋ดค์ง€๋งŒ, ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•๋„ ์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ ์žˆ์–ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•ด ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!

lucete1230-cyberpolice.tistory.com/436?category=876734

 

[์•”ํ˜ธ ํ”„๋กœํ† ์ฝœ] ์‹œ์ €์•”ํ˜ธ | Caesar Cipher | Caesar | ์นด์ด์‚ฌ๋ฅด ์•”ํ˜ธ

์นด์ด์‚ฌ๋ฅด(์‹œ์ €)์•”ํ˜ธ ์นด์ด์‚ฌ๋ฅด ์•”ํ˜ธ๋Š” ์•”ํ˜ธํ™”ํ•˜๊ณ ์ž ํ•˜๋Š” ๋‚ด์šฉ์„ ์•ŒํŒŒ๋ฒณ๋ณ„๋กœ ์ผ์ •ํ•œ ๊ฑฐ๋ฆฌ๋งŒํผ ๋ฐ€์–ด์„œ ๋‹ค๋ฅธ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์น˜ํ™˜ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 3๊ธ€์ž์”ฉ ๋ฐ€์–ด๋‚ด๋Š” ์นด์ด์‚ฌ๋ฅด ์•”ํ˜ธ๋กœ 'COME TO R

lucete1230-cyberpolice.tistory.com

 

 

์œ„ ๋ฐฉ๋ฒ•์€ ์‚ฌ์ „์„ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด์„œ ์ •์˜ ํ›„ ์‚ฌ์šฉํ•˜์˜€๊ณ  ์•„๋ž˜๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ์‹œ์ €์•”ํ˜ธ๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

100% ์™„๋ฒฝํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„ํ•˜์ง€๋Š” ์•Š์•˜์ง€๋งŒ, ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•๋„ ์žˆ๊ตฌ๋‚˜ ์ฐธ๊ณ ๋งŒ ํ•˜์…”์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

ex) ascii์ฝ”๋“œ ์ด์šฉํ•˜๊ธฐ.

def encrypt_ceasar(msg, key):
    msg = msg.lower() #๋Œ€๋ฌธ์ž -> ์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝ
    ans = bytearray()
    dic = "" # a,b,c,d..์ •์˜ํ•˜์ง€ ์•Š๊ณ , ๋น„์›Œ๋†“์Œ.
    for i in range(97,123): #์•„์Šคํ‚ค ์ฝ”๋“œ๋กœ a~z๊นŒ์ง€ ๋„ฃ์–ด์คŒ.
        dic += chr(i)
    for m in msg:
        index = dic.find(chr(m))
        if(index == -1):
            ans.append(m)
        else:
            index = index + key
            index = index % len(dic)
            ans.append(ord(dic[index]))
    return ans


def decrypt_ceasar(enc, key):
    enc = enc.upper()
    ans = bytearray()
    dic = ''
    for i in range(65,91): #ascii ๋Œ€๋ฌธ์ž A-Z
        dic += chr(i)
    
    
    for n in enc:
        index = dic.find(chr(n))
        if(index == -1):
            ans.append(n) #๊ณต๋ฐฑ์ด๋‚˜ ํŠน์ˆ˜๋ฌธ์ž ์ฒ˜๋ฆฌ
        else:
            index = index - key #๋ณตํ˜ธํ™” ํ•  ๋•Œ ํ‚ค ์ž๋ฆฌ ๋งŒํผ ๋‹ค์‹œ ๋นผ์คŒ
            index = index % len(dic) #์•ŒํŒŒ๋ฒณ ๊ฐœ์ˆ˜๋กœ ๋‚˜๋ˆ ์„œ ์ตœ๋Œ€ ์ˆ˜๊ฐ€ 26์ด ๋˜๋„๋ก.
            ans.append(ord(dic[index]))
    return ans;


def main():
    msg='This is Apple'
    key =2
    enc = encrypt_ceasar(msg.encode(),key)
    dec = decrypt_ceasar(enc,key)
    
    print("์›๋ฌธ >> ",msg)
    print("์•”ํ˜ธํ™” >> ",enc.decode())
    print("๋ณตํ˜ธํ™”(๋ฐ”์ด๋„ˆ๋ฆฌ) >> ",dec)
    print("๋ณตํ˜ธํ™”(์›๋ฌธ) >> ",dec.decode())

main()

 

ex2) ascii๋ฅผ ์ด์šฉํ•ด์„œ ์ผ์ • ๊ฐ’์„ ๋นผ๊ณ  ๋”ํ•ด์คŒ.

์ด ๋ฐฉ๋ฒ•์€ ๋จผ์ € ๊ณผ์ •์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์˜ˆ์ œ๋ฅผ ๋จผ์ € ์งœ๋ณด์•˜๋‹ค.

def main():

    msg = b"I Love you!! I am happy"
    key = 3
    msg = msg.lower()
    ans = bytearray()
    
    for m in msg:
        
        if(ord('a')<= m <=ord('z')):#ascii์ฝ”๋“œ ์ด์šฉ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๋น„๊ต
            print("m >>",m)
            index = m - ord('a') # ํ‰๋ฌธ์€ ์•„์Šคํ‚ค ์ฝ”๋“œ ๊ฐ’์œผ๋กœ ๋“ค์–ด๊ฐ€ ์žˆ๊ณ , ์ด๋ฅผ a๋งŒํผ (97)์„ ๋นผ์คŒ
            # ์‚ฌ์‹ค ์œ„ ๊ณผ์ •์€ key๊ฐ’๊ณผ 26์œผ๋กœ ๋‚˜๋ˆ„๋Š” ๊ฐ’์„ ํ™•์‹คํžˆ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ๋„ฃ์–ด์ฃผ๋Š” ๊ณผ์ •์ด๋ฏ€๋กœ ํŠน์ •ํ•œ ์ผ์ • ๊ฐ’์„ ๋นผ์ฃผ์–ด๋„ ๊ฒฐ๊ณผ๋Š” ๊ฐ™๋‹ค.
            print("1 >> ",index)
            index = index + key #key๋งŒํผ ๋”ํ•ด์คŒ.
            print("2 >>", index)
            index = index % 26 #์•ŒํŒŒ๋ฒณ(26๊ธ€์ž)์„ ๋„˜์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•จ.
            print("3 >>",index)
            index = index + ord('a')# ๋บ€ ๊ฐ’์„ ๋‹ค์‹œ ๋”ํ•ด์ฃผ๋Š” ๊ณผ์ •.
            print("4 >>",index,"\n")
            ans.append(index)

        else:
            ans.append(m)
    print("์›๋ฌธ",msg)
    print("์•”ํ˜ธ๋ฌธ",ans)

main()

 

๊ฒฐ๊ณผ

 

์ „์ฒด ์ฝ”๋“œ

def myenc(msg,key):
    msg = msg.lower()
    ans = bytearray()
    
    for m in msg:
        if(ord('a')<= m <=ord('z')):
            index = m - ord('a')
            index = index + key
            index = index % 26
            index = index + ord('a')
            
            ans.append(index)
                
#             t = m + key
#             ans.append(t)
        else:
            ans.append(m)

    return ans  
    

def mydec(msg, key):
    ans = bytearray()
    
    for m in msg:
        if(ord('a')<= m <=ord('z')):
            index = m -ord('a')
            index = index - key
            index = index % 26
            index = index + ord('a')
            
            ans.append(index)
            
        else:
            ans.append(m)

    return ans


def main():
    msg = "I Love you!! I am happy"
    key = 14
    
    msg_enc = myenc(msg.encode(),key)
    msg_dec = mydec(msg_enc,key)
    
    print("์›๋ฌธ : ",msg)
    print("์•”ํ˜ธํ™” : ",msg_enc)
    print("๋ณตํ˜ธํ™” : ",msg_dec)
    print("๋ณตํ˜ธํ™”(decode) : ",msg_dec.decode())
main()

 

๊ฒฐ๊ณผ

๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.