WASHINGTON — The police may take DNA samples from people arrested in connection with serious crimes, the Supreme Court ruled on Monday in a 5-to-4 decision.

The federal government and 28 states authorize the practice, and law enforcement officials say it is a valuable tool for investigating unsolved crimes. But the court said the testing was justified by a different reason: to identify the suspect in custody.

“When officers make an arrest supported by probable cause to hold for a serious offense and they bring the suspect to the station to be detained in custody,” Justice Anthony M. Kennedy wrote for the majority, “taking and analyzing a cheek swab of the arrestee’s DNA is, like fingerprinting and photographing, a legitimate police booking procedure that is reasonable under the Fourth Amendment.”

Justice Antonin Scalia summarized his dissent from the bench, a rare move signaling deep disagreement. He accused the majority of an unsuccessful sleight of hand, one that “taxes the credulity of the credulous.” The point of DNA testing as it is actually practiced, he said, is to solve cold cases, not to identify the suspect in custody.