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()