[์ํธ ํ๋กํ ์ฝ] ์์ ์ํธ ์ฌ์ | ์์ ์ํธ
์นด์ด์ฌ๋ฅด(์์ )์ํธ
์ด์ ์๊ฐ์ ์ฝ๋๋ฅผ ์ง๋ดค์ง๋ง, ๋ค๋ฅธ ๋ฐฉ๋ฒ๋ ์ฌ๋ฌ๊ฐ๊ฐ ์์ด ์๊ณ ๋ฆฌ์ฆ์ ๊ตฌํํด ๋ณด๋ ค๊ณ ํฉ๋๋ค!
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()
๊ฒฐ๊ณผ