import random
class HiLL:
def __init__(self):
self.MatrixKey = [[1, 1], [0, 1]]
'''
1 2
0 1 1 mod (26) == 1 27
'''
self.ReverseKey = [[27, -1], [0, 1]]
'''
27 1
0 1
'''
self.PlainText_str = ""
self.CipherText_str = ""
self.DecryptText_str = ""
self.FieldList = []
self.TempLength = 0

def FiledSetting(self):
self.FieldList.extend([chr(Ascii) for Ascii in range(ord('A'), ord('Z')+1)])

def DeterminantTest(self):
D1 = self.MatrixKey[0][0] * self.MatrixKey[1][1] -\
self.MatrixKey[0][1] * self.MatrixKey[1][0]

D2 = self.ReverseKey[0][0] * self.ReverseKey[1][1] - \
self.ReverseKey[0][1] * self.ReverseKey[1][0]


if (D1*D2)%len(self.FieldList) == 1:
print ("Test OK ... !!!")
else:
print ("Ooopse .... !!!")

def WritePlainText(self):
self.PlainText_str = input("PlainText : ")
if (len(self.PlainText_str)%2 != 0):
self.TempLength = int(len(self.PlainText_str)%2)
for i in range(0, self.TempLength):
RandomIndx = random.randint(0, len(self.FieldList))
self.PlainText_str = self.PlainText_str + self.FieldList[RandomIndx]
print ("plainText is {}".format(self.PlainText_str))

def EncrytHiLL(self):
L = []
for i in range(0, len(self.PlainText_str), 2):
L.append(self.PlainText_str[i:i+2])
v1 = self.FieldList.index(L[0][0])
v2 = self.FieldList.index(L[0][1])
"""
i = 0 => [0:2]
i = 1 => [2:4]
"""
R1 = ((self.MatrixKey[0][0] * v1) + (self.MatrixKey[0][1] * v2))%len(self.FieldList)
R2 = ((self.MatrixKey[1][0] * v1) + (self.MatrixKey[1][1] * v2))%len(self.FieldList)
Ch1 = self.FieldList[R1]
Ch2 = self.FieldList[R2]

self.CipherText_str += Ch1
self.CipherText_str += Ch2
L = []
print ("self.CipherText_str => {cipher}".format(cipher = self.CipherText_str))

def DecryptHiLL(self):
L = []
for i in range(0, len(self.CipherText_str), 2):
L.append(self.CipherText_str[i:i+2])
v1 = self.FieldList.index(L[0][0])
v2 = self.FieldList.index(L[0][1])
"""
i = 0 => [0:2]
i = 1 => [2:4]
"""
R1 = ((self.ReverseKey[0][0] * v1) + (self.ReverseKey[0][1] * v2))%len(self.FieldList)
R2 = ((self.ReverseKey[1][0] * v1) + (self.ReverseKey[1][1] * v2))%len(self.FieldList)
Ch1 = self.FieldList[R1]
Ch2 = self.FieldList[R2]

self.DecryptText_str += Ch1
self.DecryptText_str += Ch2
L = []
print ("self.CipherText_str => {Decrypt}".format(Decrypt = self.DecryptText_str))

def main():
H = HiLL() # Instance create2
H.FiledSetting()
H.DeterminantTest()
H.WritePlainText()
H.EncrytHiLL()
H.DecryptHiLL()
if __name__ == "__main__":
main()


'언어 > python' 카테고리의 다른 글

Base64 encoding _code  (0) 2017.08.07
네트워크 패킷 날리는 코드  (0) 2017.07.30
사용할 코드  (0) 2017.04.09
행렬 더하기  (0) 2017.04.07
gcd , lcm  (0) 2017.02.08