진짜 이곳저곳 다 물어봐도 아는 사람이 없어서 여기까지 왔어요 ㅠㅠ 오빠들 힘좀 보태줘..
플레이 페어를 만들고 있는데 묶는 알파벳 2개를 직접 입력하게끔 만들었거든요??
근데 뭘 넣어도 적용이 안되고 지 멋대로 묶는거 같아요 ㅠㅠ
도와주세요 능력자님들
first,second = input("묶을 두 알파벳을 입력하세요. ex) A B : ").split()
# 암호테이블 만들기
# 두 글자가 반복되는 경우 X 추가
def LHYunknownX(plainText):
for s in range(0, len(plainText) + 1, 2):
if s < len(plainText) - 1:
if plainText[s] == plainText[s + 1]:
plainText = plainText[:s + 1] + 'X' + plainText[s + 1:]
# 전체 문자가 홀수이면 X를 추가하여 일반 텍스트를 짝수로 만듭니다.
if len(plainText) % 2 != 0:
plainText = plainText[:] + 'X'
return plainText
# 처음에 모든 값이 0인 5X5 행렬을 만듭니다
def LHYmatrix(key):
matrix_5x5 = [[0 for i in range(5)] for j in range(5)]
LHYKeyArr = []
for c in key:
if c not in LHYKeyArr:
if c == second:
LHYKeyArr.append(first)
else:
LHYKeyArr.append(c)
is_I_exist = first in LHYKeyArr
# A-Z의 ASCII 값은 65에서 90 사이이지만 범위의 두 번째 매개변수는 해당 값을 제외하므로 65에서 91을 사용합니다.
for i in range(65, 91):
if chr(i) not in LHYKeyArr:
if i == first and not is_I_exist:
LHYKeyArr.append(first)
is_I_exist = True
elif i == first or i == second and is_I_exist:
pass
else:
LHYKeyArr.append(chr(i))
# LHYKeyArr을 matrix_5x5에 매핑합니다.
index = 0
for i in range(0, 5):
for j in range(0, 5):
matrix_5x5[i][j] = LHYKeyArr[index]
index += 1
return matrix_5x5
def LHYcharacter(char, LHYKeyMatrix):
indexOfChar = []
if char == second:
char = first
for i, j in enumerate(LHYKeyMatrix):
# 다시 enumerate는 첫 번째 반복에서 다음과 같은 객체를 반환합니다.
for k, l in enumerate(j):
if char == l:
indexOfChar.append(i) # 5X5 행렬의 1차원 추가 => 즉, indexOfChar = [i]]
indexOfChar.append(k) # 5X5 행렬의 2차원 추가 => 즉, indexOfChar = [i,k]
return indexOfChar
def LHYencryption(plainText, key):
cipherText = []
# 1. 키 매트릭스 생성
keyMatrix = LHYmatrix(key)
# 2. 플레이페어 암호 규칙에 따라 암호화
i = 0
while i < len(plainText):
# keyMatrix에서 두 개의 그룹화된 문자 인덱스 계산
n1 = LHYcharacter(plainText[i], keyMatrix)
n2 = LHYcharacter(plainText[i + 1], keyMatrix)
# 2.2 같은 열이면 아래 행을 살펴보세요.
# 형식은 [행, 열]입니다.
# 이제 아래 행을 보려면 => 두 항목 모두에서 행을 늘립니다.
# (n1[0]+1,n1[1]) => (3+1,1) => (4,1)
# (n2[0]+1,n2[1]) => (4+1,1) => (5,1)
# 하지만 우리 행렬에는 0에서 4개의 인덱스만 있습니다.
# 값을 0에서 4로 제한하려면 %5를 수행합니다.
# 즉.,
# (n1[0]+1% 5,n1[1])
# (n2[0]+1% 5,n2[1])
if n1[1] == n2[1]:
i1 = (n1[0] + 1) % 5
j1 = n1[1]
i2 = (n2[0] + 1) % 5
j2 = n2[1]
cipherText.append(keyMatrix[i1][j1])
cipherText.append(keyMatrix[i2][j2])
cipherText.append(" ")
elif n1[0] == n2[0]:
i1 = n1[0]
j1 = (n1[1] + 1) % 5
i2 = n2[0]
j2 = (n2[1] + 1) % 5
cipherText.append(keyMatrix[i1][j1])
cipherText.append(keyMatrix[i2][j2])
cipherText.append(" ")
# 직사각형을 만드는 경우
# [4,3] [1,2] => [4,2] [3,1]
# 두 값의 열 교환
else:
i1 = n1[0]
j1 = n1[1]
i2 = n2[0]
j2 = n2[1]
cipherText.append(keyMatrix[i1][j2])
cipherText.append(keyMatrix[i2][j1])
cipherText.append(" ")
i += 2
return cipherText
def main():
# 사용자 입력 키(5x5 문자 매트릭스를 만들기 위해) 및 일반 텍스트(암호화될 메시지) 가져오기
key = input("키값 : ").replace(" ", "").upper()
plainText = input("평문 : ").replace(" ", "").upper()
LHYconvertedText = LHYunknownX(plainText)
LHYcipherText = " ".join(LHYencryption(LHYconvertedText, key))
print(LHYcipherText)
if __name__ == "__main__":
main()
KDScipherText = " ".join(KDSencryption(KDSconvertedText, key))
print(KDScipherText)
if __name__ == "__main__":
main()
댓글 0