์ƒˆ์†Œ์‹

Languages/Python

[์•”ํ˜ธ ํ”„๋กœํ† ์ฝœ] ๊ฐ„๋‹จํ•œ ์•”ํ˜ธํ™”,๋ณตํ˜ธํ™” | Reverse Cipher | ์—ญ ์•”ํ˜ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • -
๋ฐ˜์‘ํ˜•

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


 

์‹œ์ž‘์— ์•ž์„œ ๊ฐ„๋‹จํ•œ ํŒŒ์ด์ฌ ๋ฌธ๋ฒ•์„ ์ตํžˆ๊ธฐ ์œ„ํ•ด ๋ช‡๊ฐ€์ง€ ์˜ˆ์ œ๋ฅผ ์‹ค์Šตํ•˜์ž!

 

ex1) ๋‘ ์ˆ˜ ์ž…๋ ฅ๋ฐ›๊ณ  ์ถœ๋ ฅํ•˜๊ธฐ :

num_list = list(map(int, input("์ˆซ์ž ๋‘๊ฐœ ์ž…๋ ฅ : ").split()))
print(max(num_list))

 

ex) ์ฃผ์„ ์ฒ˜๋ฆฌ & pass

ํŒŒ์ด์ฌ์—์„œ๋Š” #์„ ์ฃผ์„์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

pass๋Š” ๋‚ด์šฉ์ด ์—†๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ์‹œ ์—๋Ÿฌ๊ฐ€ ๋‚˜์ง€ ์•Š๋„๋ก ์ž๋ฆฌ๋ฅผ ์žก์•„์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

 

 

์—ญ ์•”ํ˜ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜

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

  • Reverse Cipher๋Š” ์ผ๋ฐ˜ ํ…์ŠคํŠธ ๋ฌธ์ž์—ด์„ ๋ฐ˜์ „ํ•˜๋Š” ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์•”ํ˜ธ ํ…์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

  • ์•”ํ˜ธํ™” ๋ฐ ๋ณตํ˜ธํ™” ๊ณผ์ •์€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

  • ์•”ํ˜ธ ํ…์ŠคํŠธ๋ฅผ ํ•ด๋…ํ•˜๋ ค๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ์ผ๋ฐ˜ ํ…์ŠคํŠธ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด ์•”ํ˜ธ ํ…์ŠคํŠธ๋ฅผ ๋ฐ˜์ „ํ•˜๋ฉด๋ฉ๋‹ˆ๋‹ค.

์•ฝ์ 

์—ญ ์•”ํ˜ธ์˜ ์ฃผ์š” ๋‹จ์ ์€ ๋งค์šฐ ์•ฝํ•˜๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•ด์ปค๋Š” ์›๋ณธ ๋ฉ”์‹œ์ง€๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด ์•”ํ˜ธ ํ…์ŠคํŠธ๋ฅผ ์‰ฝ๊ฒŒ ํ•ด๋… ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์—ญ ์•”ํ˜ธ๋Š” ๋ณด์•ˆ ํ†ต์‹  ์ฑ„๋„์„ ์œ ์ง€ํ•˜๊ธฐ์œ„ํ•œ ์ข‹์€ ์˜ต์…˜์œผ๋กœ ๊ฐ„์ฃผ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

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

 

 

์‹ค์Šต ์ „ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ƒ๊ฐ

 

ans๋Š” ๋นˆ ๋ฌธ์ž์—ด์ด๋‹ค. ์—ฌ๊ธฐ์— msg๋ฌธ์ž ์ค‘ msize(11)-i(0)-1 ์„ ํ•ด์ฃผ๋ฉด ๋์—์„œ๋ถ€ํ„ฐ ๋ฌธ์ž๋ฅผ ans ๋ฌธ์ž์—ด์— ๋„ฃ์–ด์ฃผ๊ฒŒ ๋œ๋‹ค.

 

์ฆ‰ hello world ์—์„œ ์ œ์ผ ๋งˆ์ง€๋ง‰ ๋ฒˆ์งธ ๊ธ€์ž์ธ d ๊ฐ€ ans์— 0๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ๋“ค์–ด๊ฐ€๊ฒŒ๋œ๋‹ค.

-1์„ ํ•ด์ฃผ๋Š” ์ด์œ ๋Š” index = 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

i ๋ฅผ ๋นผ์ฃผ๋Š” ์ด์œ ๋Š” ์ž๋ฆฌ์ˆ˜๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ๋‹ค์Œ ์ž๋ฆฌ๋กœ ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

ans๋ฅผ ๋”ํ•ด์ฃผ๋Š” ์ด์œ ๋Š” ๋“ค์–ด๊ฐ„ ๊ธ€์ž(์ €์žฅ๋œ ๊ธ€์ž)๋ฅผ ์ด์–ด ๋ถ™์ด๊ธฐ ์œ„ํ•จ์ด๋‹ค.

 

ex) ans ์˜ 0๋ฒˆ์งธ ๋ฐฉ์— ๋“ค์–ด๊ฐˆ ๊ธ€์ž = msg[11-0-1] = msg[10] ์ฆ‰  "d"๊ฐ€  ๋œ๋‹ค.

ans์˜ 1๋ฒˆ์งธ ๋ฐฉ์— ๋“ค์–ด๊ฐˆ ๊ธ€์ž = msg[11-1-1] = msg[9] = "l" ์ด๋Ÿฐ์‹์œผ๋กœ ๊ฑฐ๊พธ๋กœ ๋„๋Š” ๊ฒƒ์ด๋‹ค.

def main():
    msg = 'hello world'
    m_size = len(msg)
    ans = '' #๋นˆ ๋ฌธ์ž์—ด
    for i in range(m_size): #i=0 ~ m_size(11)๊นŒ์ง€
        ans = ans+msg[m_size -i -1]
    print("reverse : "+ans)
        

main()

๊ฒฐ๊ณผ 

 

์•”ํ˜ธํ™” | ๋ณตํ˜ธํ™” ์‹ค์Šต
def encrypt_r(msg):
    m_size = len(msg)
    ans = ''
    for i in range(m_size):
        ans = ans+msg[m_size-i-1]    
    
    return ans



def main():
    msg = 'hello world' #์›๋ฌธ
    enc = encrypt_r(msg) #์•”ํ˜ธ๋ฌธ
    dec = encrypt_r(enc) #๋ณตํ˜ธํ™” -> ๋‹ค์‹œ ์›๋ฌธ
    
    print("์›๋ฌธ : "+msg)
    print("์•”ํ˜ธ๋ฌธ : "+enc)
    print("๋ณตํ˜ธํ™” : "+dec)
    

main()

 

๊ฒฐ๊ณผ

 

reverse์•”ํ˜ธ์˜ ํŠน์ง•์€ ์•”ํ˜ธํ™”์™€ ๋ณตํ˜ธํ™”๊ฐ€ ๋™์ผํ•˜๋‹ค.

๊ฑฐ๊พธ๋กœ ๋Œ๋ฆฐ ๊ฐ’์„ ๋‹ค์‹œ ๋Œ๋ฆฌ๋ฉด ์›๋ฌธ์ด ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

byte ๋‹จ์œ„ ์•”ํ˜ธ ์‹ค์Šต

๋ณดํ†ต ์•”ํ˜ธ๋Š” ๋‹ค ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๊ตฌํ˜„์ด ๋˜์–ด์žˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๋ฐ”๊พธ์–ด ์‹ค์Šต์„ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

์ด๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ช‡๊ฐ€์ง€๋ฅผ ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž

def main():
    msg = b'hello wolrd'
    msg1 = 'hello world'
    
    print('๋ฐ”์ดํŠธ ๋‹จ์œ„ : ',msg)
    print("๊ทธ๋ƒฅ ๋ฌธ์ž์—ด : "+msg1)
    
    print(msg1.encode())
    print(msg.decode())
    

main()
b = byte ๋กœ ๋ฐ”๊ฟ”์คŒ
encode = byte๋‹จ์œ„๋กœ ๋ฐ”๊ฟ”์คŒ
decode = ๋‹ค์‹œ ๋ฌธ์ž์—ด๋กœ ๋Œ๋ฆผ
bytearray = byte๋ฅผ ๋‹ด๋Š” ๋ฐฐ์—ด

 

๊ฒฐ๊ณผ

 

type ํ™•์ธ
def main():
    msg = b'hello wolrd'
    msg1 = 'hello world'
    msg2 = bytearray()
    print('๋ฐ”์ดํŠธ ๋‹จ์œ„ : ',type(msg))
    print("๊ทธ๋ƒฅ ๋ฌธ์ž์—ด : ",type(msg1))
    print("๋ฐ”์ดํŠธ array : ",type(msg2))
    print("๋ฌธ์ž์—ด- > encode() :", type(msg1.encode()))
    print("byte -> decode() : ", type(msg.decode()))
    

main()

 

๊ฒฐ๊ณผ

 

bytearray()์˜ size๋ฅผ ๊ธฐ์žฌํ•˜๋Š” ๊ฒฝ์šฐ
# bytearray()์˜ size ๋ฅผ ๊ธฐ์žฌํ•˜๋Š” ๊ฒฝ์šฐ
def encrypt_r(msg):
    m_size = len(msg)
    ans = bytearray(m_size) 
    
    for i in range(m_size):
        ans[i] = msg[m_size-i-1]
    return ans
    
    


def main():
    msg = b'hello wolrd'
    enc = encrypt_r(msg)
    dec = encrypt_r(enc)
    
    print("์›๋ฌธ(byte) : ",msg)
    print("์•”ํ˜ธํ™” : ",enc)
    print("๋ณตํ˜ธํ™”(byte) : ",dec)
    print("๋ณตํ˜ธํ™”(str) : ",dec.decode())

main()

 

๊ฒฐ๊ณผ

 

bytearray()์˜ size๋ฅผ ๊ธฐ์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
# bytearray()์˜ size ๋ฅผ ๊ธฐ์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
def encrypt_r(msg):
    m_size = len(msg)
    ans = bytearray() #๋ฐฐ์—ด์ด๋‹ˆ .append๋กœ ๋ถ™์ด๊ธฐ ๊ฐ€๋Šฅ 
    for i in range(m_size):
        ans.append(msg[m_size-i-1])
    return ans
    
    


def main():
    msg = b'hello wolrd'
    enc = encrypt_r(msg)
    dec = encrypt_r(enc)
    
    print("์›๋ฌธ(byte) : ",msg)
    print("์•”ํ˜ธํ™” : ",enc)
    print("๋ณตํ˜ธํ™”(byte) : ",dec)
    print("๋ณตํ˜ธํ™”(str) : ",dec.decode())

main()

 

๊ฒฐ๊ณผ

 

๋ฐ˜์‘ํ˜•
Contents

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

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