def Decrypt_File(filename,keyFile): try: DecryptionFile = open(filename, 'r') except IOError: raise IOError #keyFile = pickle.load(DecryptionFile) key = Core.convert_block(keyFile) msg = pickle.load(DecryptionFile) IVFile = msg[0:BLOCK_SIZE] line = msg[BLOCK_SIZE:] IV = Core.convert_block(list(IVFile)) DecryptedText = Core.remove_nulls(Decrypt_all_blocks(line, key, IV)) DecryptionFile = open(filename, 'wb') DecryptionFile.write((DecryptedText+"\n")) DecryptionFile.close()
def Encrypt_File(filename,keyText=None): try: EncryptionFile = open(filename, 'rb') except IOError: raise IOError fileContent = EncryptionFile.read() EncryptionFile.close() key = [] if keyText==None: key = Core.keyGenerator() else: key = Core.convert_block(list(keyText)) IV, EncryptedText = Encrypt_all_blocks(fileContent,key) tempiv = ''.join(Core.convert_to_chars(IV)) Final = tempiv+EncryptedText #for i,line in enumerate(fileContent): #IV.append(None) # EncryptedText.append(None) #IV[i], EncryptedText[i] = Encrypt_all_blocks(line[:-1], key) #Final.append([tempiv,EncryptedText[i]]) keyFile = ''.join(Core.convert_to_chars(key)) EncryptionFile = open(filename, 'w') #pickle.dump(keyFile, EncryptionFile) pickle.dump(Final,EncryptionFile) EncryptionFile.close() return keyFile