The new legislation increases restrictions in a controversial 2011 law on the misuse of a computer. Museveni signed the bill on Thursday, according to a presidential spokesman's statement.
Passed by the legislature in September, the bill was brought by a lawmaker who said those who hide behind computers to hurt others should be punished, arguing that the “enjoyment of the right to privacy is being affected by the abuse of online and social media platforms through the sharing of unsolicited, false, malicious, hateful and unwarranted information.”
The bill proposes jail terms of up to 10 years in some cases, including for offenses related to the transmission of information about a person without their consent as well as the sharing or intercepting of information without authorization.
Opponents of the law say it will stifle freedom of expression in a country where many of Museveni's opponents, for years unable to stage street protests, often raise their concerns on Twitter and other online sites.
Others say it will kill investigative journalism.
The law is "a blow to online civil liberties in Uganda," according to analysis by a watchdog group Collaboration on International ICT Policy for East and Southern Africa (CIPESA).
The Committee to Protect Journalists is among groups that urged Museveni to veto the bill, noting its potential to undermine press freedom.
“Ugandan legislators have taken the wrong turn in attempting to make an already problematic law even worse. If this bill becomes law, it will only add to the arsenal that authorities use to target critical commentators and punish independent media,” the group's Muthoki Mumo said in a statement after lawmakers passed the bill.
Museveni, 78, has held power in this East African country since 1986 and won his current term last year.
Although Museveni is popular among some Ugandans who praise him for restoring relative peace and economic stability, many of his opponents often describe his rule as authoritarian.