Addedin logic to ensure that file reencrypts if something is invalid before the end.