카이사르 암호 관련 질문
조회수 927회
다음은 카이사르 암호로 작성된 5개의 수수께끼 문항들입니다. 먼저 카이사르 암호를 해독할 수 있는 함수를 사용하여 각 수수께끼 문항들이 몇 글자가 밀렸는지 찾아낸 뒤, 원래 의미하는 것이 무엇인지 해석하고 문항에 맞는 알맞은 답안을 생각하여 카이사르 암호로 표현하세요.
- 일단 문제는 안올릴께요
- 그리고 각 수수께끼 문항들은 다 달라요.
여기에서 오류는
- 첫번째는 영어로 입력했는데 문자 코드(*,!,') 이렇게 나오는 거하고
- 두번째는 GU 두 글자를 입력했는데 암호화,복호화 되는 곳에서 그냥 첫번째 글자만 나오는 것입니다.
- 또 둘이 동시에 하면 암호화 만 1글자만 나오고 복호화는 아에 수가 안 나옵니다.
코드는
def x(code, shift): #복구화하기
result = ""
a=97
z=122
for i in code:
if i==" ":
result +=1
else:
change = ord(i)-shift
result += chr(change)
return result
def er(code,shift):
result = ""
m=97
e=122
code=code.lower()
for i in code:
if i==" ":
result +=1
else:
change = ord(i)-shift
return result
d = input("Input :").lower()
s = int(input("shift number:"))
p=x(d,s)
t=er(d,s)
print('암호화 :'+p)
print('복호화 :'+t)
입니다.
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
1 답변
-
def caesar_encrypt(code, shift): result = "" a = 97 z = 122 for i in code: if i == " ": #result += 1 #문제 조건이 어떻게 되는지 모르겠지만 #띄어쓰기는 그대로 출력한다고 가정 result += " " else: #change = ord(i)-shift change = ord(i) + shift #시저암호는 기본적으로 암호화시 +하여 치환 if change > z: change = change - z + a - 1 #ascii code 표로 a~z 이상 값으로 넘어갈경우 #다시 a로 돌아가야함. result += chr(change) return result #이거 기존에 return 문이 for문 안에 있어서 하나만 암호화된거임. def caesar_decrypt(code,shift): result = "" a = 97 z = 122 code=code.lower() for i in code: if i == " ": #result += 1 result += " " else: change = ord(i) - shift if change < a: change = change + z - a + 1 result += chr(change) #이거 누락됨 return result d = input("Input :").lower() s = int(input("shift number:")) p = caesar_encrypt(d,s) print('암호화 :'+p) t = caesar_decrypt(p,s) print('복호화 :'+t)
댓글 입력