์ƒˆ์†Œ์‹

Languages/Python

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

  • -
๋ฐ˜์‘ํ˜•

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


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

์—ฌ๊ธฐ์„œ ๋ฐ€์–ด๋‚ด๋Š” ๊ธ€์ž ์ˆ˜๋Š” ์•”ํ˜ธ๋ฅผ ๋ณด๋‚ด๋Š” ์‚ฌ๋žŒ๊ณผ ํ•จ๊ป˜ ์ •ํ•ด ๋” ์–ด๋ ค์šด ์•”ํ˜ธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ์นด์ด์‚ฌ๋ฅด ์•”ํ˜ธ๋Š” ์ˆœํ™˜์•”ํ˜ธ๋ผ๊ณ  ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ ์นด์ด์‚ฌ๋ฅด ์•”ํ˜ธ๋กœ 'RUSQHUVKBVEHQIIQIYDQJEH' ๋ผ๋Š” ์•”ํ˜ธ๋ฅผ ๋ฐ›์•˜์„ ๊ฒฝ์šฐ, ํ•ด๋…ํ•˜๋ฉด BECAREFULFORASSASINATOR, ์•”์‚ด์ž๋ฅผ ์กฐ์‹ฌํ•˜๋ผ๋Š” ๋œป์ด ๋œ๋‹ค. (์ถœ์ฒ˜ : ์œ„ํ‚ค๋ฐฑ๊ณผ)

์นด์ด์‚ฌ๋ฅด ์•”ํ˜ธ๋Š” ๋‹จ์ˆœํ•˜๊ณ  ๊ฐ„๋‹จํ•˜์—ฌ ์ผ๋ฐ˜์ธ๋„ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ฒ ์ž์˜ ๋นˆ๋„์™€ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋‹จ์–ด์™€ ํ˜•ํƒœ๋ฅผ ์ด์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

 

Caesar Cipher์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜

Caesar ์•”ํ˜ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

  • Caesar Cipher Technique๋Š” ๊ฐ„๋‹จํ•˜๊ณ  ์‰ฌ์šด ์•”ํ˜ธํ™” ๊ธฐ๋ฒ•์ด๋‹ค.

  • ๋‹จ์ˆœํ•œ ์œ ํ˜•์˜ ๋Œ€์ฒด ์•”ํ˜ธ์ด๋‹ค.

  • ์ผ๋ฐ˜ ํ…์ŠคํŠธ์˜ ๊ฐ ๋ฌธ์ž๋Š” ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์•„๋ž˜๋กœ ๊ณ ์ • ๋œ ์ˆซ์ž๊ฐ€์žˆ๋Š” ๋ฌธ์ž๋กœ ๋Œ€์ฒด๋œ๋‹ค.

 

๋‹ค์Œ ๋‹ค์ด์–ด๊ทธ๋žจ์€ Caesar ์•”ํ˜ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์˜ ์ž‘๋™์„ ์„ค๋ช…ํ•œ๋‹ค.

 

์„ค๋ช…

์ผ๋ฐ˜ ํ…์ŠคํŠธ ๋ฌธ์ž๋Š” ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์”ฉ ์ˆœํšŒํ•œ๋‹ค.

  • ์ฃผ์–ด์ง„ ์ผ๋ฐ˜ ํ…์ŠคํŠธ์˜ ๊ฐ ๋ฌธ์ž์— ๋Œ€ํ•ด ํ…์ŠคํŠธ์˜ ์•”ํ˜ธํ™” ๋ฐ ๋ณตํ˜ธํ™” ์ ˆ์ฐจ์— ๋”ฐ๋ผ ๊ทœ์น™์— ๋”ฐ๋ผ ์ฃผ์–ด์ง„ ๋ฌธ์ž๋ฅผ ๋ณ€ํ™˜ํ•œ๋‹ค.

  • ๋‹จ๊ณ„๋ฅผ ์ˆ˜ํ–‰ ํ•œ ํ›„ ์•”ํ˜ธ ํ…์ŠคํŠธ๋ผ๊ณ ํ•˜๋Š” ์ƒˆ ๋ฌธ์ž์—ด์ด ์ƒ์„ฑ๋จ.

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‹ค์Šต ์ „ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ํ•„์š”ํ•œ ๋ช‡ ๊ฐ€์ง€ ์ง€์‹์„ ์•Œ์•„๋ณด์ž.

 

ex) range์˜ ์ฐจ์ด

msg = 'hello world'    
size = len(msg)

for i in msg:
    print(i)
    
for i in range(size):
    print(i)

range๋’ค์—” str ํ˜•ํƒœ๊ฐ€ ์˜ฌ ์ˆ˜ ์—†์Œ

 

ex) find ํ•จ์ˆ˜

dic = 'abc'
index = dic.find('a') #return = 0
index = dic.find('b') #return = 1
index = dic.find('c') #return = 2
index = dic.find('x') #return = -1

print(index)

find ํ•จ์ˆ˜๋Š” ํ•ด๋‹น ๊ฐ’์ด ์กด์žฌ ํ•œ๋‹ค๋ฉด ๊ทธ ๊ฐ’์˜ ๋ฒˆํ˜ธ(index)๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์œ„ ๊ฒฐ๊ณผ๋Š” 'c'๋ฅผ ์ถœ๋ ฅํ•ด๋ณด๊ณ , 'b'๋ฅผ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ์˜ ๊ฒฐ๊ณผ์ด๋‹ค.

 

 

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„
def encrypt_caesar(msg,key):
    m_size = len(msg)
    ans = ''
    word = 'abcdefghijklmnopqrstuvwxyz' #๊ฐ„๋‹จํ•˜๊ฒŒ ์†Œ๋ฌธ์ž๋กœ๋งŒ ๊ตฌ์„ฑํ•˜์˜€์Œ(ํ‰๋ฌธ์ด ์†Œ๋ฌธ์ž๋ผ์„œ)
    for i in msg: #->hello world๋ฅผ ํ•œ ๊ธ€์ž์”ฉ ์ถœ๋ ฅ
        index = word.find(i) # word์ค‘ msg ๊ธ€์ž์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ๋‹ค๋ฉด index์— ๋„ฃ์–ด์ค€๋‹ค. index ์ฆ‰ ์ˆซ์ž ํ˜•ํƒœ๋กœ ๋“ค์–ด๊ฐ.
        #print(index) ->index ํ™•์ธ์šฉ
        if(index == -1): #ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด ์—†๋‹ค๋ฉด
            ans = ans + i # ๊ทธ ์—†๋Š” ๊ฐ’ ์ž์ฒด๋ฅผ ans์— ๋„ฃ๊ฒ ๋‹ค. -> ์ฆ‰ ํ‰๋ฌธ์—์„œ ๊ณต๋ฐฑ์— ํ•ด๋‹น
        else:
            index = index + key #key๊ฐ’ ๋งŒํผ Index๋ฅผ ์ด๋™์‹œํ‚ค๋Š” ๊ฒƒ.
            index = index % len(word) #index ๊ฐ’์ด ๋„˜์น  ์ผ์€ ์—†์ง€๋งŒ key๋ฅผ ๋”ํ•ด์ฃผ๋ฉด wordํฌ๊ธฐ๋ฅผ ๋ฒ—์–ด๋‚  ์ˆ˜ ์žˆ์Œ.
            ans = ans + word[index] #๋‹จ์–ด ์‚ฌ์ „ ์ค‘ ํ‚ค๋ฅผ ๋”ํ•ด์ค€ ์žฅ์†Œ์— ์žˆ๋Š” ์•ŒํŒŒ๋ฒณ์„ ans์— ๋„ฃ์–ด์ฃผ๋Š” 
        
    return ans
     
def decrypt_caesar(enc, key):
    e_size = len(enc)
    ans = ''
    word = 'abcdefghijklmnopqrstuvwxyz'
    
    for i in enc:
        index = word.find(i)
        if(index == -1):
            ans = ans + i #๊ทธ๋Œ€๋กœ ๋ฌธ์ž๋ฅผ ๋„ฃ์–ด์คŒ (๊ณต๋ฐฑ)
        else:
            index = index - key #๋‹ค์‹œ ๋Œ๋ฆฌ๋Š” ๊ฒƒ!
            index = index % len(word)
            ans = ans + word[index]
    
    return ans
    

def main():
    msg = 'hello world'
    key = 3 #key๋Š” ์ˆซ์žํ˜•ํƒœ
    enc = encrypt_caesar(msg, key)
    dec = decrypt_caesar(enc, key)
    print("์›๋ฌธ : ",msg)
    print("์•”ํ˜ธํ™” : ",enc)
    print("๋ณตํ˜ธํ™” : ",dec)

main()

 

์ฒ˜์Œ ๋Œ์•„๊ฐ€๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ’€์–ด์„œ ์„ค๋ช…ํ•˜์ž๋ฉด hello world์ค‘ ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž์ธ h๊ฐ€ i(๋ณ€์ˆ˜)๋กœ ๋“ค์–ด์˜จ๋‹ค.

h ๋Š” word๋‚ด์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์— index์— ๋“ค์–ด๊ฐ„๋‹ค. index = 7(a=0, b=1, c=2 ..) 
index ๊ฐ€ -1์ด ์•„๋‹ˆ๋ฏ€๋กœ, else ๋ฌธ์œผ๋กœ ์ด๋™
7 + key = 10 
10 = 10 % 25(์•ŒํŒŒ๋ฒณ ์ˆ˜) = 10
# ์ด ์ค„์ด ์žˆ๋Š” ์ด์œ ๋Š” index ๊ฐ€ 25์ธ๋ฐ ํ‚ค๊ฐ€ 3์ธ ๊ฒฝ์šฐ 25๋ฅผ ๋ฒ—์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์— 28 = 28 % 25 = 3 ์ด ๋˜๋ผ๊ณ  ์žˆ๋Š” ๊ธฐ๋Šฅ

๊ทธ๋ฆฌ๊ณ  ans ์— word[10]๋ฒˆ์งธ ์ฆ‰, k ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค. 

์ฆ‰ ans ์— ์ฒซ ๊ธ€์ž = h ์—์„œ ํ‚ค๊ฐ’(3) ์„ ์˜ฎ๊ธด "k"

 

๊ฒฐ๊ณผ(์•”ํ˜ธํ™” ๋ฌธ์ž์—ด์˜ ์ฒซ ๊ธ€์ž๊ฐ€ k ์ธ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.)

 

byte ๋‹จ์œ„๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„

์•ž์„œ bytearray์™€ encode, decode์˜ ๊ด€๊ณ„๋ฅผ ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ํ…Œ์ŠคํŠธ ํ•ด๋ณด์•˜๋‹ค.

ans = bytearray()
msg = b'hello world'
for i in msg:
    print(i)
    ans.append(i)
    print(ans)
#์ด๊ฑธ๋กœ ์•Œ ์ˆ˜ ์žˆ๋Š” ๊ฑด encode๋˜๋ฉด ๊ฐ’์„ ์•„์Šคํ‚ค๋กœ ๋นผ์คŒ, ๋‹ค์‹œ ์ด๊ฑธ ans ์— ๋„ฃ์œผ๋ฉด
#bytearray๋Š” ์ด๋ฅผ decodeํ•œ ๊ฒƒ ์ฒ˜๋Ÿผ ๋‹ค์‹œ ๋ฌธ์ž๋กœ ๋ฐ›์•„์คŒ.

# =============================================================================
# ์œ„ ์ฝ”๋“œ์˜ ๋Œ€ํ•œ ๊ฒฐ๊ณผ ๊ฐ’
# 104
# bytearray(b'h')
# 101
# bytearray(b'he')
# 108
# bytearray(b'hel')
# 108
# bytearray(b'hell')
# 111
# bytearray(b'hello')
# 32
# bytearray(b'hello ')
# 119
# bytearray(b'hello w')
# 111
# bytearray(b'hello wo')
# 114
# bytearray(b'hello wor')
# 108
# bytearray(b'hello worl')
# 100
# bytearray(b'hello world')
# =============================================================================

 

์œ„ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด  msg๋ฅผ encode()ํ•ด์„œ ๋ณด๋‚ด๋ฉด b'hello world' ์ฆ‰ ๋ฐ”์ดํŠธ ํ˜•ํƒœ๋กœ ๊ฐ€๊ฒŒ ๋˜๊ณ ,

์ด์˜ ๊ฐ’์„ ํ•˜๋‚˜ํ•˜๋‚˜ ๋ฝ‘์•„๋ณด๋ฉด asciiํ˜•ํƒœ๋กœ ๋˜์–ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

๋˜ํ•œ bytearray์•ˆ์— ascii๊ฐ’์„ ๋„ฃ์œผ๋ฉด byteํ˜•ํƒœ ์ฆ‰ ๋ฌธ์ž ํ˜•ํƒœ๋กœ ๋‚˜ํƒ€๋‚ด ์ค€๋‹ค๋Š” ๊ฒƒ๋„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ฅผ ํ† ๋Œ€๋กœ byteํ˜•ํƒœ ์•”ํ˜ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•ด๋ณด๋„๋ก ํ•˜์ž

def encrypt_caesar(msg,key):
    m_size = len(msg)
    ans = bytearray()
    word = 'abcdefghijklmnopqrstuvwxyz'
      
    for i in msg: #encode๋œ msg๋Š” ascii๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
        index = word.find(chr(i)) #chr(์ˆซ์ž) -> ๋ฌธ์ž ์ฆ‰, word์•ˆ์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž ์ฐพ๊ธฐ ์œ„ํ•ด ํ˜•๋ณ€ํ™˜ ํ•ด์ค€๊ฒƒ, i๊ฐ€ ํ˜„์žฌ ์ˆซ์ž์ด๊ธฐ ๋•Œ๋ฌธ
        if(index == -1):
            ans.append(i)
        else:
            index = index + key
            index = index % len(word)
            ans.append(ord(word[index])) #ord(๋ฌธ์ž) -> ์ˆซ์ž. ์ฆ‰ append ํ•ด์„œ ๊ฐ’ ๋„ฃ์–ด์ค„ ๋• ๋‹ค์‹œ ascii๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•ด์คŒ.
    return ans
     
def decrypt_caesar(enc, key):
    e_size = len(enc)
    ans = bytearray()
    word = 'abcdefghijklmnopqrstuvwxyz'
    
    for i in enc: 
        index = word.find(chr(i)) 
        if(index == -1):
            ans.append(i)
        else:
            index = index - key
            index = index % len(word)
            ans.append(ord(word[index])) 
    return ans

def main():
    msg = 'hello world'
    key = 3 #key๋Š” ์ˆซ์žํ˜•ํƒœ
    enc = encrypt_caesar(msg.encode(), key)
    dec = decrypt_caesar(enc, key)
    
    print("\n------------------------------------------")
    print("ํ‚ค : ",key)
    print("์›๋ฌธ : ",msg)
    
    print("์•”ํ˜ธ๋ฌธ : ",enc.decode())
    print("์™„์ „ํ•œ ๋ณตํ˜ธํ™” : ",dec.decode())
    
    print("\n(ํ˜•๋ณ€ํ™˜x)์•”ํ˜ธํ™” : ",enc)
    print("(ํ˜•๋ณ€ํ™˜x)๋ณตํ˜ธํ™” : ",dec)
    print("------------------------------------------")

main()

 

๊ฒฐ๊ณผ

 

Caesar Cipher ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•ดํ‚น

์•”ํ˜ธ๋ฌธ์€ ๋‹ค์–‘ํ•œ ๊ฐ€๋Šฅ์„ฑ์œผ๋กœ ํ•ดํ‚น ๋  ์ˆ˜ ์žˆ๋‹ค. 

์ด๋Ÿฌํ•œ ๊ฐ€๋Šฅ์„ฑ ์ค‘ ํ•˜๋‚˜๋Š” ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์•”ํ˜ธ ํ•ด๋… ํ‚ค๋ฅผ ์‹œ๋„ํ•˜๋Š” Brute Force Technique ์ด๋‹ค.

 

์ด ๊ธฐ์ˆ ์€ ๋งŽ์€ ๋…ธ๋ ฅ์ด ํ•„์š”ํ•˜์ง€ ์•Š์œผ๋ฉฐ ํ•ด์ปค์—๊ฒŒ๋Š” ๋น„๊ต์  ๊ฐ„๋‹จํ•˜๋‹ค. Caesar ์•”ํ˜ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ•ดํ‚นํ•˜๊ธฐ์œ„ํ•œ ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

message = 'GIEWIVrGMTLIVrHIQS' #encrypted message
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

for key in range(len(LETTERS)):
   translated = ''
   for symbol in message:
      if symbol in LETTERS:
         num = LETTERS.find(symbol)
         num = num - key
         if num < 0:
            num = num + len(LETTERS)
         translated = translated + LETTERS[num]
      else:
         translated = translated + symbol
print('Hacking key #%s: %s' % (key, translated))

#์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํ‚ค์˜ ๊ฐฏ์ˆ˜๋งŒํผ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๋‹ค ๊ตฌํ•ด์„œ ๊ฐ’์„ ์˜ˆ์ƒํ•˜์—ฌ ์ถœ๋ ฅ ์‹œ

 

์ด์ „ ์˜ˆ์—์„œ ์•”ํ˜ธํ™” ๋œ ์•”ํ˜ธ ํ…์ŠคํŠธ๋ฅผ ๊ณ ๋ คํ•˜๊ณ ,

๊ทธ๋Ÿฐ ๋‹ค์Œ ํ‚ค๋กœ ๊ฐ€๋Šฅํ•œ ํ•ดํ‚น ๋ฐฉ๋ฒ•๊ณผ ๋ฌด์ฐจ๋ณ„ ๋Œ€์ž… ๊ณต๊ฒฉ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•œ ์ถœ๋ ฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

https://www.tutorialspoint.com/cryptography_with_python/cryptography_with_python_caesar_cipher.htm

๋ฐ˜์‘ํ˜•
Contents

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

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