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 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() # * Choose a method if self.METHOD == '1': dec = Bem.CesarD(bytesArray[0],self.KEY) if self.METHOD == '2': dec = Bem.MonoD(bytesArray[0],self.KEY) if self.METHOD == '3': dec = Bem.DispD(bytesArray[0],self.KEY) # * --------------- pN = Bem.XOR(dec,self.IV) i = 1 logProcess = '----- log D -----\n\n' plainText = '' cN_aux = '' for cN in bytesArray: logProcess += ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") if i == 1: plainText += pN + ' ' cN_aux = cN logProcess += ("\nC{} -> {}").format(i,cN) # * Choose a method if self.METHOD == '1': logProcess += "\n ces -------> {}".format(dec) if self.METHOD == '2': logProcess += "\n mon -------> {}".format(dec) if self.METHOD == '3': logProcess += "\n dis -------> {}".format(dec) # * --------------- logProcess += ("\nK -> {} xor -------> P{} -> {} = {}").format(self.KEY,i,pN,chr(int(pN,2))) logProcess += "\n IV -> {}\n".format(self.IV) else: # * Choose a method if self.METHOD == '1': dec = Bem.CesarD(cN,self.KEY) if self.METHOD == '2': dec = Bem.MonoD(cN,self.KEY) if self.METHOD == '3': dec = Bem.DispD(cN,self.KEY) # * --------------- pN = Bem.XOR(dec,cN_aux) plainText += pN + ' ' logProcess += ("\nC{} -> {}").format(i,cN) # * Choose a method if self.METHOD == '1': logProcess += "\n ces -------> {}".format(dec) if self.METHOD == '2': logProcess += "\n mon -------> {}".format(dec) if self.METHOD == '3': logProcess += "\n dis -------> {}".format(dec) # * --------------- logProcess += ("\nK -> {} xor -------> P{} -> {} = {}").format(self.KEY,i,pN,chr(int(pN,2))) logProcess += "\n C{} -> {}\n".format((i-1),cN_aux) cN_aux = 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 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()