More than 28,000 prosecutions brought by two rail firms for alleged fare evasions have been quashed.