Why use cryptography? If I understand the problem statement correctly, there's a simpler solution. When a prisoner wants to go to sleep, they signal and a guard walks over and renders them unconscious, presumably using drugs. Since we know that nobody would go to sleep outside of jail, you can figure out who is who by counting the number of times they've been sedated.
(This is vulnerable to troubles telling who is who at the start, but so is any knowledge-based method. This is also vulnerable to people falling asleep outside, but so is any knowledge based method. It's also fairly dangerous, given that most drugs capable of rendering somebody unconscious are dangerous; however, giving guards some training and then handwaving away or saying the society isn't concerned by the (minimal) danger sounds reasonable. It assumes certain things about going to sleep and drugs that may not be true in this universe, but it at least sounds reasonable- and this is fiction.)
Sedatives would cause physical sleep, and the reason people share bodies in this world is because having your body be asleep will cause your soul to be eaten by insubstantial demons. Sleeping-while-someone-else-pilots-your-body is safe in large part because it cuts off interventions regarding your soul from outside sources - demons, drugs, magic, etc.
Also, this method relies on cooperative criminals, not just cooperative cohabitors-with-criminals. The criminal has an incentive to make being in jail really inconvenient for their cohabitor - by, for instan...
If it's worth saying, but not worth its own post, even in Discussion, it goes here.