언어/python
시저, 대치, 비즈네르
파아랑새
2015. 11. 21. 16:45
#-*-coding: utf-8 -*-
'''
(1)작성자:김준현
'''
import random
''' ---------- SETTING ---------- '''
TEMP_CEA_LIST=[]
AlphaBat_String=''
TEMP_SUB_LIST=[]
SUB_String=''
for symbol in range(97, 123):
'''97, 98, 99, 100, ..., 122'''
TEMP_CEA_LIST.append(chr(symbol))
TEMP_SUB_LIST.append(chr(symbol))
while(True):
random.shuffle(TEMP_SUB_LIST)
if TEMP_SUB_LIST != TEMP_CEA_LIST:
break
AlphaBat_String = ''.join(TEMP_CEA_LIST)
SUB_String = ''.join(TEMP_SUB_LIST)
'''---------------------------------'''
'''--------- ceasar cipher ---------'''
class ceasar:
def __init__(self, p, k, c, pp):
''' p: 평서문, k: 비밀키(1~25), c: 암호문, pp:해독평문 '''
self.p = p
self.k = k
self.c = c
self.decryption_P = pp
def plainText_encrypt(self):
for charactor in self.p:
if charactor in AlphaBat_String:
index = AlphaBat_String.find(charactor) + self.k
if index > len(AlphaBat_String)-1:
index = index % len(AlphaBat_String)
self.c = self.c + AlphaBat_String[index]
else:
'''charactor not in AlphaBat_String'''
self.c = self.c +charactor
return self.c
def cipherText_decrypt(self):
for charactor in self.c:
if charactor in AlphaBat_String:
index = AlphaBat_String.find(charactor) - self.k
if index < 0:
index = index + len(AlphaBat_String)
self.decryption_P = self.decryption_P + AlphaBat_String[index]
else:
'''charactor not in AlphaBat_String'''
self.decryption_P = self.decryption_P + charactor
return self.decryption_P
'''--------- substitution cipher ---------'''
class substitutionCipher:
def __init__(self, p, c, pp):
self.p = p
self.c = c
self.decryption_P = pp
def plainText_encrypt(self):
for charactor in self.p:
if charactor in AlphaBat_String:
index = AlphaBat_String.find(charactor)
self.c = self.c + SUB_String[index]
else:
'''charactor not in AlphaBat_String'''
self.c = self.c +charactor
return self.c
def cipherText_decrypt(self):
for charactor in self.c:
if charactor in SUB_String:
index = SUB_String.find(charactor)
self.decryption_P = self.decryption_P + AlphaBat_String[index]
else:
'''charactor not in AlphaBat_String'''
self.decryption_P = self.decryption_P +charactor
return self.decryption_P
'''--------- vigenere cipher ---------'''
class vigenere:
def __init__(self, p, k, c, pp):
''' p: 평서문, k: 비밀키(1~25), c: 암호문, pp:해독평문 '''
self.p = p
self.k = k
self.c = c
self.decryption_P = pp
#cipherText = vigenere(plaintext, secretKey,cipherText,Result_plaintext).plainText_encrypt()
def plainText_encrypt(self):
i=0
for charactor in self.p:
if charactor in AlphaBat_String:
index = AlphaBat_String.find(charactor) + AlphaBat_String.find(self.k[i])
if index > len(AlphaBat_String)-1:
index = index % len(AlphaBat_String)
self.c += AlphaBat_String[index]
else:
self.c += charactor
i+=1
if i >len(self.k)-1:
i = 0
return self.c
def cipherText_decrypt(self):
i=0
for charactor in self.c:
if charactor in AlphaBat_String:
index = AlphaBat_String.find(charactor) - AlphaBat_String.find(self.k[i])
if index <0 :
index = index + len(AlphaBat_String)
self.decryption_P += AlphaBat_String[index]
else:
self.decryption_P += charactor
i+=1
if i >len(self.k)-1:
i = 0
return self.decryption_P
def main():
plaintext = input("plaintext is ")
plaintext = plaintext.lower()
cipherText = ''
Result_plaintext = ''
print("(1): ceasar cipher ")
print("(2): substitution cipher")
print("(3): vigenere cipher ")
while(True):
result = int(input("result: "))
#ceasar cipher
if result == 1:
secretKey = random.randint(1, 25)
cipherText = ceasar(plaintext, secretKey,cipherText,Result_plaintext).plainText_encrypt()
print("시저암호법: 평문[{0:s}] -> 암호문[{1:s}] ". format(plaintext,cipherText))
d = ceasar(plaintext, secretKey,cipherText,Result_plaintext).cipherText_decrypt()
print("시저암호법: 암호문[{0:s}] -> 평문[{1:s}] ". format(cipherText,d))
break
#substitution cipher
elif result == 2:
cipherText = substitutionCipher(plaintext,cipherText,Result_plaintext).plainText_encrypt()
print("대치암호법: 평문[{0:s}] -> 암호문[{1:s}] ". format(plaintext,cipherText))
d = substitutionCipher(plaintext,cipherText,Result_plaintext).cipherText_decrypt()
print("대치암호법: 암호문[{0:s}] -> 평문[{1:s}] ". format(cipherText,d))
break
#vigenere cipher
elif result == 3:
secretKey_SIZE = random.randint(1, len(plaintext))
secretKey = vigenere_secret_key_setting(secretKey_SIZE)
cipherText = vigenere(plaintext, secretKey,cipherText,Result_plaintext).plainText_encrypt()
print("비즈네르암호법: 평문[{0:s}] -> 암호문[{1:s}] ". format(plaintext,cipherText))
d = vigenere(plaintext, secretKey,cipherText,Result_plaintext).cipherText_decrypt()
print("비즈네르암호법: 암호문[{0:s}] -> 평문[{1:s}] ". format(cipherText,d))
break
else:
print("잘못입력했어")
def vigenere_secret_key_setting(secretKey_SIZE):
size = 0
tmp_key=''
while(size!=secretKey_SIZE-1):
tmp_key += AlphaBat_String[random.randint(0, len(AlphaBat_String)-1)]
size +=1
return tmp_key
if __name__=="__main__":
main()