2020. 1. 30. 18:25ㆍ카테고리 없음
s = "apple."
se = ""
i = 1
print(len(s))
while len(s)-i+1:
se += s[len(s)-i]
i += 1
print(se)
////////////////////////////////////////////////////////////
plaintext = 'apple'
ciphertext = ''
i = len(plaintext)
while i > 0:
ciphertext = ciphertext + plaintext[i - 1]
i -= 1
decrypted = ''
i = len(ciphertext)
while i > 0:
decrypted = decrypted + ciphertext[i - 1]
i = i - 1
print('Plaintext', plaintext)
print('Piphertext', ciphertext)
print('Plaintext', decrypted)
////////////////////////////////////////////////////////////////
plaintext = 'apple'
ciphertext = ''
i = len(plaintext)
while i:
ciphertext = ciphertext + plaintext[i - 1]
i -= 1
decrypted = ''
i = len(ciphertext)
while i:
decrypted = decrypted + ciphertext[i - 1]
i = i - 1
print('Plaintext', plaintext)
print('Ciphertext', ciphertext)
print('Plaintext', decrypted)
//////////////////////////////////////////////////////
plaintext = 'Three can keep a secret, if two of them are dead.'
ciphertext = ''
for i in range(1, len(plaintext) + 1):
ciphertext = ciphertext + plaintext[-i]
print("Plaintext :", plaintext)
print("Ciphertext :", ciphertext)
decrypted = ''
for i in range(1, len(ciphertext) + 1):
decrypted += ciphertext[-i]
print("Plaintext :", decrypted)
////////////////////////////////////////////////////////
plaintext = 'Three can keep a secret, if two of them are dead.'
translated = []
for i in plaintext:
translated.append(i)
# string(스트링)을 list(리스트)로 바꿈
# translated = [i for i in plaintext]
# string(스트링)을 list(리스트)로 바꿈
translated.reverse()
ciphertext = ''.join(translated)
# 다시 string(스트링)으로 변환한다.
print('Plaintext :', plaintext)
print('Ciphertext :', ciphertext)
translated = [i for i in ciphertext]
translated.reverse()
decrypted = ''.join(translated)
print('Plaintext :', decrypted)
////////////////////////////////////////////////
#!/usr/bin/python3
def reverse_translated(msg):
# input : str(msg)
# output : str(trans)
# function :
# * msg 문자열을 입력으로 받아서 문자열을 뒤집고,
# * trans를 return
translated = [i for i in msg]
translated.reverse()
trans = ''.join(translated)
return trans
def main():
plaintext = 'Three can keep a secret, if two of them are dead.'
ciphertext = reverse_translated(plaintext)
decrypted = reverse_translated(ciphertext)
print('Plaintext :', plaintext)
print('Ciphertext :', ciphertext)
print('Plaintext :', decrypted)
if __name__ == '__main__':
main()
# 파이썬만 설치되어 있으면 window, MAC OS에서도 기동가능
//////////////////////////////////////////////////////
# key : 'A'
# value : 0
# encbook = {'A': 0, 'B': 1, ...}
# encbook[key] = value
encbook = {}
# decbook = {0: 'A', '1': B, ...}
decbook = {}
for i in range(0, 26):
key = chr(65 + i)
value = i
encbook[key] = value
for i in encbook:
key = encbook[i]
value = i
decbook[key] = value
plaintext = 'nice to meet you'
key = 5
ciphertext = ''
input_msg = plaintext.upper()
for i in input_msg:
if i in encbook:
translated = (encbook[i] + key) % 26
ciphertext += decbook[translated]
else:
ciphertext += i
key = -5
input_msg = ciphertext
decrypted = ''
for i in input_msg:
if i in encbook:
translated = (encbook[i] + key) % 26
decrypted += decbook[translated]
else:
decrypted += i
print('Plaintext :', plaintext)
print('Ciphertext :', ciphertext)
print('Decrypted :', decrypted.lower())
///////////////////////////////////
def makebook():
# input : N/A(Not available)
# output : dict(encbook), dict(decbook)
# function : 암호화 북(encbook)과 복호화 북(decbook)을 만들어서 return
encbook = {}
decbook = {}
for i in range(0, 26):
key = chr(65 + i)
value = i
encbook[key] = value
for i in encbook:
key = encbook[i]
value = i
decbook[key] = value
return encbook, decbook
def translate(msg, key, enc, dec):
# input :
# * str(msg) : 입력 메시지
# * int(key) : 암호화 키
# * dict(enc) : encbook
# * dict(dec) : decbook
# output : str(translated) : 암호화 또는 복호화된 문자열
# function : 입력 메시지를 받아서 암호화/복호화하고 그 메시지를 출력한다.
translated = ''
for i in msg:
if i in enc:
tmp = (enc[i] + key) % 26
translated += dec[tmp]
else:
translated += i
return translated
plaintext = 'nice to meet you'
myenc, mydec = makebook()
mykey = 5
input_msg = plaintext.upper()
encrypted = translate(input_msg, mykey, myenc, mydec)
mykey = -5
input_msg = encrypted
decrypted = translate(input_msg, mykey, myenc, mydec)
print('Plaintext :', plaintext)
print('Ciphertext :', encrypted)
print('Decrypted :', decrypted.lower())
/////////////////////////////////////////////////////
def substitution(kt, msg):
# input :
# * dict(kt) : 암호화/복호화 키 테이블
# * str(msg) : 평문/암호문 문자열
# output : str(translated) : 복호화/암호화 된 문자열
# function :
# * 암호화 키 테이블을 가지고 입력 평문 문자열을 return
# * 복호화 키 테이블을 가지고 출력 암호문 문자열을 return
results = ''
for i in msg:
if i in kt:
results += kt[i]
else:
results += i
return results
def makebook(kt):
# input : dict(kt)
# output : dict(reverse_kt)
# function : Keytable을 입력 받아 key와 value의 위치를 바꾼 dict를 return
"""
reverse_kt = {}
for i, j in kt.items():
reverse_kt[j] = i
"""
reverse_kt = [j, i for i, j in kt.items()]
return reverse_kt
def main():
keytable = {'h': 'A', 'e': 'K', 'l': 'O', 'o': 'B', 'w': 'L', 'r': 'Z', 'd': 'Y'}
mymsg = "hello world"
encrypted = substitution(keytable, mymsg)
print("Original :", mymsg)
print("Encrypted :", encrypted)
keytable2 = makebook(keytable)
decrypted = substitution(keytable2, encrypted)
print("Decrypted :", decrypted)
if __name__ == "__main__":
main()
///////////////////////////////////////////////////////////////////////