def decrypt(self): plainTextClean = mf.readFile('cipherText.txt','b') try: bytearray(int(x, 2) for x in plainTextClean.split()) except: print(f'ERROR: It is not a binary string.') exit() bytesArray = plainTextClean.split() i = 1 plainText = '' IVK = '' logProcess = '----- log D -----\n\n' for cN in bytesArray: if i == 1: # * Choose a method if self.METHOD == '1': xor = Bem.CesarE(self.IV,self.KEY) if self.METHOD == '2': xor = Bem.MonoE(self.IV,self.KEY) if self.METHOD == '3': xor = Bem.DispE(self.IV,self.KEY) # * --------------- IVK = xor pN = Bem.XOR(IVK,cN) plainText += pN + ' ' logProcess += ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"\ "\nIV -> {}").format(self.IV) # * Choose a method if self.METHOD == '1': logProcess += "\n ces -------> {}".format(xor) if self.METHOD == '2': logProcess += "\n mon -------> {}".format(xor) if self.METHOD == '3': logProcess += "\n dis -------> {}".format(xor) # * --------------- logProcess += ("\nK -> {} xor -------> P{} -> {} = {}"\ "\n C{} -> {}\n".format(self.KEY,i,pN,chr(int(pN,2)),i,cN)) else: logProcess += ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"\ "\nIV^K -> {}").format(IVK) # * Choose a method if self.METHOD == '1': xor = Bem.CesarE(IVK,self.KEY) if self.METHOD == '2': xor = Bem.MonoE(IVK,self.KEY) if self.METHOD == '3': xor = Bem.DispE(IVK,self.KEY) # * --------------- IVK = xor pN = Bem.XOR(xor,cN) plainText += pN + ' ' # * Choose a method if self.METHOD == '1': logProcess += "\n ces -------> {}".format(xor) if self.METHOD == '2': logProcess += "\n mon -------> {}".format(xor) if self.METHOD == '3': logProcess += "\n dis -------> {}".format(xor) # * --------------- logProcess += ("\nK -> {} xor -------> P{} -> {} = {}"\ "\n C{} -> {}\n".format(self.KEY,i,pN,chr(int(pN,2)),i,cN)) i += 1 finalText = ''.join([chr(int(b, 2)) for b in plainText.split()]) mf.createFile('decryptText.txt',finalText) mf.createFile('log.txt',logProcess+'\n\nRESULT:\n'+finalText) print('\nCOMPLETED PROCESS\n') exit()
def encrypt(self): try: octetos = bytearray(self.plainText, 'utf8') except: print(f'ERRORf: File error.') exit() bytesArray = (' '.join(f'{x:b}'.rjust(8, '0') for x in octetos)).split() # * Choose a method if self.METHOD == '1': xor = Bem.CesarE(self.IV,self.KEY) if self.METHOD == '2': xor = Bem.MonoE(self.IV,self.KEY) if self.METHOD == '3': xor = Bem.DispE(self.IV,self.KEY) # * --------------- cN = Bem.XOR(xor,bytesArray[0]) i = 0 plainText = '' logProcess = '----- log -----\n\n' for ba in bytesArray: if i == 0: plainText += cN + ' ' logProcess += ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"\ "\nIV -> {}").format(self.IV) # * Choose a method if self.METHOD == '1': logProcess += "\n ces -------> {}".format(xor) if self.METHOD == '2': logProcess += "\n mon -------> {}".format(xor) if self.METHOD == '3': logProcess += "\n dis -------> {}".format(xor) # * --------------- logProcess += ("\nK -> {} xor -------> C{} -> {}"\ "\n P{} -> {}\n".format(self.KEY,i,cN,i,ba)) else: # * Choose a method if self.METHOD == '1': xor = Bem.CesarE(cN,self.KEY) if self.METHOD == '2': xor = Bem.MonoE(cN,self.KEY) if self.METHOD == '3': xor = Bem.DispE(cN,self.KEY) # * --------------- logProcess += ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"\ "\nC{} -> {}").format((i-1),cN) cN = Bem.XOR(xor,ba) plainText += cN + ' ' # * Choose a method if self.METHOD == '1': logProcess += "\n ces -------> {}".format(xor) if self.METHOD == '2': logProcess += "\n mon -------> {}".format(xor) if self.METHOD == '3': logProcess += "\n dis -------> {}".format(xor) # * --------------- logProcess += ("\nK -> {} xor -------> C{} -> {}"\ "\n P{} -> {}\n".format(self.KEY,i,cN,i,ba)) i += 1 mf.createFile('log.txt',logProcess+'\n\nRESULT:\n'+plainText) mf.createFile('cipherText.txt',plainText) print('\nCOMPLETED PROCESS\n') exit()
def encrypt(self): try: octetos = bytearray(self.plainText, 'utf8') except: print(f'ERROR: Check the path of the file.') exit() bytesArray = (' '.join(f'{x:b}'.rjust(8, '0') for x in octetos)).split() xor = Bem.XOR(bytesArray[0],self.IV) # * Choose a method if self.METHOD == '1': cN = Bem.CesarE(xor,self.KEY) if self.METHOD == '2': cN = Bem.MonoE(xor,self.KEY) if self.METHOD == '3': cN = Bem.DispE(xor,self.KEY) # * --------------- i = 1 logProcess = '----- log -----\n\n' plainText = '' for P in bytesArray: logProcess += "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" if i == 1: plainText += cN+' ' logProcess += ("\nP{} -> {}"\ "\n xor -------> {}").format(i,P,xor) # * Choose a method if self.METHOD == '1': logProcess += "\nIV -> {} ces -------> C{} -> {}".format(self.IV,i,cN) if self.METHOD == '2': logProcess += "\nIV -> {} mon -------> C{} -> {}".format(self.IV,i,cN) if self.METHOD == '3': logProcess += "\nIV -> {} dis -------> C{} -> {}".format(self.IV,i,cN) # * --------------- logProcess += "\n K-> {}\n".format(self.KEY) else: xor = Bem.XOR(P,cN) logProcess += ("\nP{} -> {}"\ "\n xor -------> {}").format(i,P,xor) # * Choose a method if self.METHOD == '1': logProcess += "\nC{} -> {} ces -------> C{} -> {}".format((i-1),cN,i,Bem.CesarE(xor,self.KEY)) if self.METHOD == '2': logProcess += "\nC{} -> {} mon -------> C{} -> {}".format((i-1),cN,i,Bem.MonoE(xor,self.KEY)) if self.METHOD == '3': logProcess += "\nC{} -> {} dis -------> C{} -> {}".format((i-1),cN,i,Bem.DispE(xor,self.KEY)) # * --------------- logProcess += "\n K-> {}\n".format(self.KEY) # * Choose a method if self.METHOD == '1': cN = Bem.CesarE(xor,self.KEY) if self.METHOD == '2': cN = Bem.MonoE(xor,self.KEY) if self.METHOD == '3': cN = Bem.DispE(xor,self.KEY) # * --------------- plainText += cN + ' ' i += 1 mf.createFile('log.txt',logProcess+'\n\nRESULT:\n'+plainText) mf.createFile('cipherText.txt',plainText) print('\nCOMPLETED PROCESS\n') exit()
def encrypt(self, KEY): try: # * Check the key if self.Metodo == '1': bytearray(int(KEY, 2)) if len(KEY) != 8: print(f'ERROR: The key is not an 8 bit binary number.') exit() elif self.Metodo == '2': pass elif self.Metodo == '3': pass # * ------------- octetos = bytearray(self.plainText, 'utf8') except: print(f'ERROR: The key is not an 8 bit binary number.') else: bytesArray = (' '.join(f'{x:b}'.rjust(8, '0') for x in octetos)).split() cipherText = '' logProcess = '----- log -----\n' i = 0 for ba in bytesArray: i += 1 # * Choose a method if self.Metodo == '1': cc = Bem.CesarE(ba, self.LlaveM) cipherText += cc + ' ' logProcess += ('\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'\ '\nP{} -> {}'\ '\n cesar ------> C{} -> {}'\ '\nK -> {}').format(i,ba,i,cc,KEY) if self.Metodo == '2': cc = Bem.MonoE(ba, self.LlaveM) cipherText += cc + ' ' logProcess += ('\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'\ '\nP{} -> {}'\ '\n mono ------> C{} -> {}'\ '\nK -> {}').format(i,ba,i,cc,KEY) if self.Metodo == '3': cc = Bem.DispE(ba, self.LlaveM) cipherText += cc + ' ' logProcess += ('\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'\ '\nP{} -> {}'\ '\n disp ------> C{} -> {}'\ '\nK -> {}').format(i,ba,i,cc,KEY) # * --------------- mf.createFile('cipherText.txt', cipherText) mf.createFile('log.txt', logProcess + '\n\nRESULT:\n' + cipherText) print('\nCOMPLETED PROCESS\n') exit()
def decrypt(self): plainTextClean = mf.readFile('cipherText.txt', 'b') try: bytearray(int(x, 2) for x in plainTextClean.split()) except: print(f'ERROR: File error.') exit() bytesArray = plainTextClean.split() i = 1 T = self.TN plainText = '' logProcess = '----- log D -----\n\n' for cN in bytesArray: # * Choose a method if self.METHOD == '1': xor = Bem.CesarE(T, self.KEY) if self.METHOD == '2': xor = Bem.MonoE(T, self.KEY) if self.METHOD == '3': xor = Bem.DispE(T, self.KEY) # * --------------- pN = Bem.XOR(cN, xor) plainText += pN + ' ' logProcess += ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"\ "\nT{} -> {} = {}").format(i,T,int(T,2)) # * Choose a method if self.METHOD == '1': logProcess += "\n ces -------> {}".format(xor) if self.METHOD == '2': logProcess += "\n mon -------> {}".format(xor) if self.METHOD == '3': logProcess += "\n dis -------> {}".format(xor) # * --------------- logProcess += ("\nK -> {} xor -------> P{} -> {} = {}"\ "\n C{} -> {}\n").format(self.KEY,i,pN,chr(int(pN,2)),i,cN) TD = (int(T, 2)) + 1 TA = TD % 256 T = '{0:b}'.format(TA).rjust(8, '0') i += 1 finalText = ''.join([chr(int(b, 2)) for b in plainText.split()]) mf.createFile('decryptText.txt', finalText) mf.createFile('log.txt', logProcess + '\n\nRESULT:\n' + finalText) print('\nCOMPLETED PROCESS\n') exit()
def encrypt(self): try: octetos = bytearray(self.plainText, 'utf8') except: print(f'ERROR: File error.') exit() bytesArray = (' '.join(f'{x:b}'.rjust(8, '0') for x in octetos)).split() i = 1 plainText = '' T = self.TN logProcess = '----- log -----\n\n' for ba in bytesArray: # * Choose a method if self.METHOD == '1': xor = Bem.CesarE(T, self.KEY) if self.METHOD == '2': xor = Bem.MonoE(T, self.KEY) if self.METHOD == '3': xor = Bem.DispE(T, self.KEY) # * --------------- cN = Bem.XOR(xor, ba) plainText += cN + ' ' logProcess += ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"\ "\nT{} -> {} = {}").format(i,T,int(T,2)) # * Choose a method if self.METHOD == '1': logProcess += "\n ces -------> {}".format(xor) if self.METHOD == '2': logProcess += "\n mon -------> {}".format(xor) if self.METHOD == '3': logProcess += "\n dis -------> {}".format(xor) # * --------------- logProcess += ("\nK -> {} xor -------> C{} -> {}"\ "\n P{} -> {}\n").format(self.KEY,i,cN,i,ba) TD = (int(T, 2)) + 1 TA = TD % 256 T = '{0:b}'.format(TA).rjust(8, '0') i += 1 mf.createFile('cipherText.txt', plainText) mf.createFile('log.txt', logProcess + '\n\nRESULT:\n' + plainText) print('\nCOMPLETED PROCESS\n') exit()