Long-distance quantum key distribution with imperfect devices