언어/python

암호 ver4.1

파아랑새 2016. 2. 16. 21:10
#///////////////////////////////
# 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
import affine
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) # ---------------------> 11224

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
self.affine_Multi_Encrypt_Key = 0 #-------------> affine_Multi_Encrypt_Key
self.affine_Multi_Decrypt_Key = 0 #-------------> affine_Multi_Decrypt_Key
self.affine_Add_Key = 0 #-----------------------> affine_Add_Key
self.one_Time_Pad_Key = "" #--------------------> one_Time_Pad_Key

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, 11223) #1, 2, 3, 4, 5, ..., 11223

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)%11224
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)%11224
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)%len(self.vigenere_key)

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)%len(self.vigenere_key)
# ----------------------------------------------------------------------------

# affine crypto---------------------------------------------------------------
def affine_random_key(self):
index = random.randint(0,5279) #total:5280, 0,1,2,3,4,...,5279
self.affine_Multi_Encrypt_Key = affine.affine_encrypt_key_List[index]
self.affine_Multi_Decrypt_Key = affine.affine_decrypt_key_List[index]
self.affine_Add_Key = random.randint(1, 11223) # 1, 2, 3,..., 11223

def encrypt_affine(self):
index = 0
for char_ascii in self.plain_text:
index = (alphabat_type_of_String.find(char_ascii)*self.affine_Multi_Encrypt_Key)%boundary_range
index = (index + self.affine_Add_Key)%boundary_range
self.cipher_text += alphabat_type_of_String[index]

def decrypt_affine(self):
index = 0
for char_ascii in self.cipher_text:
index = (alphabat_type_of_String.find(char_ascii) - self.affine_Add_Key)%boundary_range
index = (index * self.affine_Multi_Decrypt_Key)%boundary_range
self.plain_text_decrypt += alphabat_type_of_String[index]
# ----------------------------------------------------------------------------

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

def encrypt_OTP(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.one_Time_Pad_Key[index]))%boundary_range
self.cipher_text = self.cipher_text + alphabat_type_of_String[position_int]
index = (index +1)%len(self.one_Time_Pad_Key)

def decrypt_OTP(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.one_Time_Pad_Key[index]))%boundary_range
self.plain_text_decrypt = self.plain_text_decrypt + alphabat_type_of_String[position_int]
index = (index +1)%len(self.one_Time_Pad_Key)
# ----------------------------------------------------------------------------
def main():
choice = random.randint(1, 4)
if(choice == 1): #ceasar
print("ceasar")
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)
elif(choice == 2): #vigenere
print("vigenere")
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)
elif(choice == 3): #affine
print("affine")
affine_ = CRYPTO()
affine_.plain_text_write()
affine_.affine_random_key()
print(affine_.affine_Multi_Encrypt_Key)
print(affine_.affine_Multi_Decrypt_Key)
affine_.encrypt_affine()
print(affine_.cipher_text)
affine_.decrypt_affine()
print(affine_.plain_text_decrypt)
else: #otp
print("one time pad")
otp = CRYPTO()
otp.plain_text_write()
otp.OTP_random_key()
print(otp.one_Time_Pad_Key)
otp.encrypt_OTP()
print(otp.cipher_text)
otp.decrypt_OTP()
print(otp.plain_text_decrypt)

if __name__ == "__main__":
main()