A divided Supreme Court ruled Monday that police may take DNA samples when booking those arrested for serious crimes, narrowly upholding a Maryland law and opening the door to more widespread collection of DNA by law enforcement.

The court ruled 5 to 4 that government has a legitimate interest in collecting DNA from arrestees, just as it takes photographs and collects fingerprints. Rejecting the view that the practice constitutes an unlawful search, the majority said it was justified to establish the identity of the person in custody.

“DNA identification represents an important advance in the techniques used by law enforcement to serve legitimate police concerns for as long as there have been arrests,” Justice Anthony M. Kennedy wrote for the majority.

The decision will reinstate Alonzo Jay King Jr.’s conviction in a 2003 rape in Salisbury on Maryland’s Eastern Shore. He was connected to the crime after a DNA sample was taken following an unrelated 2009 arrest for assault.

Law enforcement has found DNA to be a powerful tool in solving cold cases, and the federal government and 28 states allow the practice.

As with other recent court decisions involving the Fourth Amendment’s “right of the people to be secure in their persons, ­houses, papers, and effects, against unreasonable searches and seizures,” the justices split in an unusual fashion.

The dissenters were three of the court’s liberals plus conservative Justice Antonin Scalia, who amplified his displeasure by reading a summary of his dissent from the bench.

“The court has cast aside a bedrock rule of our Fourth Amendment law: that the government may not search its citizens for evidence of crime unless there is a reasonable cause to believe that such evidence will be found,” Scalia said from the bench.

In his dissent, Scalia wrote that the majority’s attempts to justify the use of DNA as an identification tool “taxes the credulity of the credulous.” He added, “Make no mistake about it: As an entirely predictable consequence of today’s decision, your DNA can be taken and entered into a national DNA database if you are ever arrested, rightly or wrongly, and for whatever reason.”

Scalia was joined by Justices Ruth Bader Ginsburg, Sonia Sotomayor and Elena Kagan.

Kennedy wrote that the decision was more limited than that, noting that DNA can be taken only from those suspected of “serious” crimes. He said that police have a legitimate interest in identifying the person taken into custody and that the DNA samples could make sure that a dangerous criminal is not released on bail.

“By comparison to this substantial government interest and the unique effectiveness of DNA identification, the intrusion of a cheek swab to obtain a DNA sample is a minimal one,” Kennedy wrote. Justice Stephen G. Breyer — who most often votes with Ginsburg, Sotomayor and Kagan — joined the opinion, as did Chief Justice John G. Roberts Jr. and Justices Clarence Thomas and Samuel A. Alito Jr.

At the oral argument in the case in February, Alito called it “perhaps the most important criminal procedure case that this court has heard in decades.”

Maryland Attorney General Douglas F. Gansler (D) said he was happy the court agreed that “taking a DNA swab is no more invasive than taking someone’s fingerprints — and that DNA is an important tool in both identifying the guilty and exonerating the wrongly accused.”

But Steven R. Shapiro, legal director of the American Civil Liberties Union said the decision “creates a gaping new exception to the Fourth Amendment” and violates a long-established understanding that “police cannot search for evidence of a crime . . . without individualized suspicion.”

The jurisdictions that allow DNA samples have differing rules. In Virginia, for instance, law enforcement officials take DNA samples after making an arrest in violent felonies and burglaries, while officials in the District take samples only after a conviction, authorities said.

Still, all 50 states, the District and the Obama administration backed Maryland in defending its law.

Kennedy said Maryland’s law is limited. He noted that the sample is destroyed if the arrestee is not convicted and that the DNA tests did not violate the privacy of the person by revealing genetic traits or medical information.

Besides that, Kennedy said, DNA identification provides critical information about the potential danger posed by an arrestee, whether he should be eligible for bond or whether he would be likely to flee because he had committed a crime more serious than the one for which he was arrested.

But Scalia said those justifications did not apply to King. It took months for his DNA to be tested, and his sample was not compared to a database that would confirm his identity but to one with samples from cold cases in which the identity of the DNA was unknown.

“Solving unsolved crimes is a noble objective, but it occupies a lower place in the American pantheon of noble objectives than the protection of our people from suspicionless law-enforcement ­searches,” Scalia wrote.

He concluded with a nod to the Framers of the Constitution: “I doubt that the proud men who wrote the charter of our liberties would have been so eager to open their mouths for royal inspection.”

Gansler, in an interview after the ruling, acknowledged that “the real reason for the law is solving crime,” but he said the use of DNA was no different from “comparing fingerprints left at the scene of the crime” or a mug shot “to a photograph captured by a surveillance camera.”

The decision was evidence of how the court’s ideological differences blur on Fourth Amendment ­cases. Earlier this term, Scalia and Thomas joined Ginsburg, Sotomayor and Kagan to rule that bringing a police dog to a suspected drug dealer’s door without a warrant amounted to an unlawful search.

And Scalia joined Sotomayor’s broad ruling in another case that held police officers generally must try to get a warrant before forcing uncooperative drunken-driving suspects to submit to a blood test.

Scalia joked during a speech last fall in Wyoming that he “ought to be the pinup of the criminal defense bar.”

Monday’s decision came in Maryland v. King.