Hamlet’s treatment of Rosencrantz and Guildenstern demonstrates that he feels resentment toward them for betraying him.
Hamlet was once friends with Rosencrantz and Guildenstern. Yet at some point they turned on him, at least in his mind. They were spies, not friends. Their role was to spy on him for Claudius, and while some people might consider that par for the course in a royal castle, it does make it harsh because they used to be his friends. While they probably felt that loyalty to the king and queen was paramount, he felt that they had betrayed his trust. This is clear in his reaction.
There's letters seal'd; and my two schoolfellows,
Whom I will trust as I will adders fang'd … (Act 3, Scene 4)
Hamlet does not really hide the fact that he does not care for the two. He makes his feelings known pretty clearly.
That I can keep your counsel, and not mine own.
Besides, to be demanded of a sponge, what replication should
be made by the son of a king? (Act 4, Scene 2)
They seem a little confused by this, but calling them “adders” and then a “sponge” makes it fairly obvious to us, the reader or audience, where Hamlet is coming from.
It is not hard to believe why Hamlet feels this way. While the argument can be made that Rosencrantz and Guildenstern may not know what was in the letter that ordered Hamlet’s death, they were the messengers. They were still the instruments of his death. Hamlet decided to do something about it, and rearranged things so that he ordered Rosencrantz and Guildenstern’s deaths instead. Hamlet did not feel much remorse at this, because after all, they were about to do the same thing to him. Should Hamlet have killed them? Maybe not, but it was kill or be killed.
Hamlet does not trust Rosencrantz and Guildenstern. That is why he breaks open the letter they are carrying to England. When he sees that Claudius is asking the English to execute him, he realizes he is in a tight spot. He does not blame Rosencrantz and Guildenstern for this. They do not know the contents of the letter. At this point he thinks he is on his way to England. He has no idea that a pirate ship will attack the ship he is on and that he will be returned to Denmark for ransom. He has to write a substitute letter, which he seals with his ring. He decides to order the immediate executions of both Rosencrantz and Guildenstern--but this is out of necessity. He wants to shut them up as soon as they arrive in England. Otherwise they would have a lot to tell the English about Hamlet. For one thing, they would say that Hamlet was insane. They would tell the English authorities that Hamlet murdered Polonius and that King Claudius was sending him away because he feared for his own life. They could describe some of Hamlet's mad antics, such as his thinking that Polonius was only hiding and that they were all playing a game like hide-and-seek. And they could bring up other incidents in which Hamlet appeared to be mad. They could say, quite truthfully, that both King Claudius and Queen Gertrude both believed that Hamlet was insane. What would be the result? The English would decide to delay the executions and keep Hamlet in custody until they could send an envoy to Denmark to find out what was expected of them. Hamlet would be treated with courtesy and given comfortable quarters, but he would be a prisoner. Hamlet did not especially want to be "harsh" with his former friends, but he wanted to save his own life. If the English sent an envoy to Claudius, that man would soon come back with word that Claudius wanted Hamlet executed and had actually written them a letter expressing that request. Hamlet doesn't hate his former friends. He doesn't trust them, and he doesn't care much about them. He probably wouldn't have ordered them executed in his forged letter if he hadn't considered it necessary to save his own life. Here is what Hamlet tells Horatio about them in Act 5, Scene 2:
So Guildenstern and Rosencrantz go to't.
Why, man, they did make love to their employment.
They are not near my conscience; their defeat
Does by their own insinuation grow.
'Tis dangerous when the baser nature comes
Between the pass and fell incensed points
Of mighty opposites.
Rosencrantz and Guildenstern are what we moderns would call collateral damage. When Hamlet says, "...their defeat Does by their own insinuation grow," he means that they brought their deaths on themselves by their interference. Hamlet is only angry at Claudius. Once he discovers that Claudius had ordered the English to kill him, he becomes more determined and more cruel. He is a changed man. There will be no more vacillating and procrastinating. He intends to kill Claudius immediately. Shakespeare invented the business with the pirate ship to bring Hamlet back to Denmark quickly. The playwright didn't want to go through a lot of pointless explanation of how Hamlet went to England, how Rosencrantz and Guildenstern were "put to sudden death, Not shriving-time allowed," how Hamlet got out of England as quickly as possible, either going across to France and returning to Denmark overland, or hiring a ship to take him directly to Denmark, and so forth. Hamlet's character change has nothing to do with Rosencrantz and Guildenstern, but is entirely due to his discovery that King Claudius intends to kill him. He knows he had better kill Claudius before Claudius hears what happened to Rosencrantz and Guildenstern.
It must be shortly known to him from England
What is the issue of the business there.
It shall be short. The interim is mine,
And a man's life's no more than to say "one."
Hamlet is feeling betrayed and takes it out on the people near him. One of the times where this is very important is when Hamlet is going to England and sees the letter from the King and changes the name to Rosencrantz and Guildenstern. Most likely R & G didn't even know that Hamlet's name was in the letter, and because of Hamlet, it resulted in their death.