언어/python

암호 ver2

파아랑새 2016. 2. 15. 04:37
#///////////////////////////////
# 2016 02 11
# Kim Jun Hyeon
# Lower case
# ord('a') => 97
# ord('z') => 122
# Upper case
# ord('A') => 65
# ord('Z') => 90
# ord('') => 44032
# ord('') => 55203
#///////////////////////////////
import random

alphabat_type_of_List = [] #empty
alphabat_type_of_String = '' #empty
# a, b, c, d, e, f, ..., z -----------------------------------------
for number_alphabat_Lower_case in range(97, 123): # 97, 98, ... ,122
alphabat_type_of_List.append(chr(number_alphabat_Lower_case))

# ' '
alphabat_type_of_List.append(' ')
# ------------------------------------------------------------------

# A, B, C, D, E, F, ..., Z -----------------------------------------
for number_alphabat_Upper_case in range(65, 91): # 65, 66, ... ,90
alphabat_type_of_List.append(chr(number_alphabat_Upper_case))
# ------------------------------------------------------------------

#, , ... , --------------------------------------------------
for number_hangul in range(44032, 55203):
alphabat_type_of_List.append(chr(number_hangul))
# ------------------------------------------------------------------

# (test) print(alphabat_type_of_List)
random.shuffle(alphabat_type_of_List)
# (test) print(alphabat_type_of_List)

alphabat_type_of_String = ''.join(alphabat_type_of_List)
boundary_range = len(alphabat_type_of_String)

print(alphabat_type_of_String)
print("len(alphabat_type_of_String) is {0:d}"\
.format( len(alphabat_type_of_String) ))
class CRYPTO:
def __init__(self):
self.ceasar_key = 0 #---------> ceasar
self.plain_text = ""
self.cipher_text = ""
self.plain_text_decrypt = ""
self.vigenere_key = "" #---------> vigenere

def plain_text_write(self):
self.plain_text = input("[1] plain_text input >>> ")
# Ceasar crypto -------------------------------------------------------------
def key_random_ceasar(self):
self.ceasar_key = random.randint(1, 11222) #1, 2, 3, 4, 5, ..., 51

def encrypt_ceasar(self):
for plain_char in self.plain_text:
if plain_char in alphabat_type_of_String:
encrypt_ing = (alphabat_type_of_String.find(plain_char) + self.ceasar_key)%11223
self.cipher_text += alphabat_type_of_List[(encrypt_ing)]
else:
self.cipher_text += plain_char

def decrypt_ceasar(self):
for cipher_char in self.cipher_text:
if cipher_char in alphabat_type_of_String:
decrypt_ing = (alphabat_type_of_String.find(cipher_char) - self.ceasar_key)%11223
self.plain_text_decrypt += alphabat_type_of_List[(decrypt_ing)]
else:
self.plain_text_decrypt += cipher_char
# ----------------------------------------------------------------------------

# Vigenere crypto-------------------------------------------------------------
def vigenere_random_key(self):
for i in range(0, random.randint(1, len(self.plain_text))):
self.vigenere_key += alphabat_type_of_String[random.randint(0, len(alphabat_type_of_String)-1)]

def encrypt_vigenere(self):
index = 0
for char_ascii in self.plain_text:
position_int = (alphabat_type_of_String.find(char_ascii) + alphabat_type_of_String.find(self.vigenere_key[index]))%boundary_range
self.cipher_text = self.cipher_text + alphabat_type_of_String[position_int]
index = (index +1)%2

def decrypt_vigenere(self):
index = 0
for char_ascii in self.cipher_text:
position_int = (alphabat_type_of_String.find(char_ascii) - alphabat_type_of_String.find(self.vigenere_key[index]))%boundary_range
self.plain_text_decrypt = self.plain_text_decrypt + alphabat_type_of_String[position_int]
index = (index +1)%2
# ----------------------------------------------------------------------------
def main():
'''
cea_test = CRYPTO()
cea_test.plain_text_write()
cea_test.key_random_ceasar()
cea_test.encrypt_ceasar()
cea_test.decrypt_ceasar()
print("plain_text is " + cea_test.plain_text)
print("Random Key is {0:d}".format(cea_test.ceasar_key))
print(cea_test.cipher_text)
print(cea_test.plain_text_decrypt)
'''
vigenere = CRYPTO()
vigenere.plain_text_write()
vigenere.vigenere_random_key()
print(vigenere.vigenere_key)
vigenere.encrypt_vigenere()
print(vigenere.cipher_text)
vigenere.decrypt_vigenere()
print(vigenere.plain_text_decrypt)
if __name__ == "__main__":
main()