Throughout the Midrash and Gemara, Bil’am is viewed as one of the most wicked people in all of Tanach. Although Balak is also a Rasha, we don't usually put Balak on the same level as Bil’am. However, under a close examination of the text, it seems that Balak is the one who is the instigator of the attempt to curse Bnei Yisrael. The difference between the two lies in the fact that Bil’am is able to learn from his mistakes, unlike Balak.
From the beginning, Balak's idea of cursing Bnei Yisrael was unjustified. Up to that point, Bnei Yisrael had attacked other nations only out of self-defense or Hashem's commandment to do so. Moav, whose king was Balak, was therefore not justified in being fearful of an attack from them. Furthermore, the Pasuk (BeMidbar 22:3) states that both Balak and his nation were fearful of Bnei Yisrael. However, a king should not be scared with his people during a time of danger, but should try to strengthen them. Because of his fear, Balak seeks Bil’am's help and says about him, “Asher Tevareich Mevorach VaAsher Ta’or Yu’ar,” “Whomever you bless is blessed and whomever you curse is accursed” (BeMidbar 22:6). This demonstrates that Balak thought Bil’am was the one who can choose who is blessed and who cursed, which is a denial of Hashem's involvement in the matter. In Bil’am's response, he corrects Balak by saying that Hashem is the one who decides whether or not his curse will be effective. In spite of this, Balak asks Bil’am again and offers more money because he thinks that Bil’am's original rejection was just a ploy to extract more money. Clearly Balak placed no significance in Hashem's opinion on the matter, and felt that Bil’am was in charge. Following this encounter, Bil’am consults Hashem on the matter and is permitted to go to Balak as long as he follows exactly what Hashem instructs him to do.
On the way to greet Balak, Bil’am is delayed by an angel of Hashem and warned again, but does not understand what the angel is trying to tell him until later. Therefore, when he later tries to curse Bnei Yisrael, the Torah states (23:5), “VaYasem Hashem Davar BeFi Vil’am” “And Hashem put an utterance in Bil’am’s mouth.” This event implicitly rebukes Balak and Bil’am; Hashem teaches them that He is the one who gives curses and blessings, not a human being. Pasuk 16 records that Hashem was once again forcibly putting words into Bil’am’s mouth during his second blessing. This, just like by the first blessing, shows us that Hashem is the master of all curses and blessings. By the third blessing, however, Bil’am begins to understand Hashem's message, as the Pasuk states (24:1), “VaYar Bil’am Ki Tov BeEinei Hashem LeVareich Et Yisrael” “And Bil’am saw that it was good in Hashem’s eyes to bless Israel.” He realizes that he should not have been trying to curse a nation that Hashem has already blessed, and so he gave this third blessing with good intentions. Because of Bil’am's Teshuvah, Hashem does not, “Put an utterance in Bil’am's mouth,” as He did every other time. Instead, Pasuk 2 tells us, “VaTehi Alav Ru’ach Elokim,” “And the spirit of God was upon him,” which is a phrase reserved for the prophets of Bnei Yisrael. And so, after seeing Hashem's agreement, Bil’am prefaces his blessing with the words (Pasuk 3), “UNe’um HaGever Shetum HaAyin,” “The words of the man with the open eye.” Bil’am is referred to as the man with the open eye, because now he knows the truth, that Hashem’s blessings and curses are exalted over everyone. Therefore, Bil’am can only bless Bnei Yisrael (Pasuk 4), “while fallen,” in other words, humbled. Furthermore, this third blessing is a pure blessing which contains no rebuke against Bil’am, and mentions that (Pasuk 9), “Mevarechecha Varuch VeOrerecha Arur,” “Those who bless you are blessed and those who curse you are accursed.” This is ironic because it is precisely the power which Balak had attributed to Bil’am before, and what Bil’am himself had previously known. Balak fails to accept this and banishes Bil’am, but Bil’am tries to advise Balak with a prophecy affected by his own accord. This prophecy of Bil’am shows Balak that Hashem now permits him to prophesy freely. Therefore, we see that Balak may very well have been the most wicked person in this story. Bil’am, unlike Balak, was able to learn, to a certain extent, that Bnei Yisrael is a special nation, untouchable by the curses of other nations as long as Hashem is on their side.