The men, ages 20 and 23, were arrested in March by vigilantes within their community in Banda Aceh, the capital of Aceh province.
They were subsequently found guilty of violating Aceh's strict Islamic laws and were sentenced to 85 lashes, according to Evendi, the head of Sharia law enforcement in Banda Aceh.
Khairil Jamal, the lead judge, said that "the court has proven that the defendants without doubt are legally guilty of committing sodomy."
He said, "No evidence was found to justify and forgive them. Therefore, they shall be punished accordingly."
Neighbors barged into an apartment with the men and filmed them naked with camera phones, according to rights groups.
The videos were shared widely in local media.
Evendi told CNN the public caning would be done "in one of the mosques in Banda Aceh."
LGBT activists and human rights groups have condemned the sentence.
"These young men are two Indonesians who wanted nothing more than to live their lives and have their privacy respected," Kyle Knight from Human Rights Watch
wrote about the case last week.
"Now they await a public flogging. A pluralist president like (Joko Widodo) should recognize the right thing to do here is to spare these two the rod."
Gay sex is legal everywhere else in Muslim-majority Indonesia, a nation generally known to be moderate and tolerant.
However, experts say Indonesia is becoming increasingly conservative, with large anti-LGBT protests in Jakarta
and passionate reactions to allegations of blasphemy commonplace in recent years.
The sentence comes on the heels of the imprisonment of outgoing Christian governor of Jakarta
, Basuki Tjahaja Purnama, commonly known as Ahok.
He was sentenced to two years in prison after being found guilty of blasphemy. He had been accused of insulting Islam during his re-election campaign.
The trial was seen as a test of Indonesia's religious tolerance, but the governor's election defeat and then his sentencing appeared to be a victory for Indonesia's conservative Muslims, who campaigned heavily against Ahok.
Aceh is the only Indonesian province governed by conservative Sharia law under which unmarried men and women are forbidden to spend time together anywhere in private.
To do so constitutes khalwat, directly translated as "seclusion" and punishable by public caning.
The law of khalwat does not apply to men, who can be alone in a room together, even without clothes.
However, Sharia punishes any same-sex acts harshly.