High Court Judge Mathew Emukule ruled in Mombasa, Kenya, on June 16 that anal probes are a legal way to determine someone's sexual orientation.
"I find no violation of human dignity, right to privacy and right to freedom of the petitioners," Emukule said during the ruling, according to the Associated Press.
The petitioners in the case were two men who asked the court to stop forced anal exams and HIV testing of those accused of homosexuality.
The men were arrested in February 2015 for allegedly having gay sex. If the men are convicted, they could go to jail for up to 14 years.
Emukule also ruled that the men should have had their lawyers get a court order to stop the anal intrusions by law enforcement.
"I sat in court holding my chin in disbelief," Eric Gitari, the executive director of the Kenyan National Gay and Lesbian Human Rights Commission, said.
"It's so painful when we are trying to encourage the gay community to go to court to affirm their rights; the courts are instead affirming violation of their rights," Gitari added.
Gitari said the men would appeal the ruling.
U.S. President Barack Obama was warned in July 2015 not to mention LGBT rights during his visit to Kenya by the country's Christian leadership, noted The Telegraph.
"Homosexuality is against the plan of God, God did not create man and woman so that men would marry men and women marry women," William Ruto, Kenya’s deputy president, said at the time.
"We have heard that in the U.S. they have allowed gay relations and other dirty things. "I want to say as a Christian leader that we will defend our country Kenya, we will stand for our faith and our country."
Justin Muturi, the speaker of Kenya’s National Assembly, said Obama would be prevented from speaking about gay rights if he dared to do so.
"We will demonstrate firmness against such obtrusions," Muturi said. "As an individual and a Christian, I am opposed to homosexuality and cannot condone gay practices."