Both stories turn on the importance of the exact text of a bizarre legal document, and both involve Portia.
In the casket story, the legal document is Portia's father's will, which stipulated that each of her suitors must choose one of three mysterious caskets (small chests). If the suitor chooses the one that turns out to have Portia's picture inside, he is free to marry her.
Each casket (gold, silver, or lead) also comes with a riddle, presumably also written by Portia's father. The riddles are very ambiguous, e.g., "Who chooseth me shall get as much as he deserves." Their meaning could be construed very differently depending the reader's expectations. This gives us a glimpse into Portia's father's mind, and his possible reasons for writing his will the way he did. He wanted Portia's suitors to be sorted out by their interpretations of the challenge. The one with the best character would choose the right casket.
It is up to Portia to explain the rules of the challenge to each suitor. Then, though she knows which is the right casket, she has to keep her mouth shut. She must refrain from misleading suitors she doesn't like, and from giving a hint to the suitor she really does like (Bassanio). This is a very hard task, and the fact that she is able to do it shows how great a respect she has for her father and for the letter of his will (though she does not really like the will). It also shows her willingness not to show off, to keep her knowledge hidden.
Unlike the will and the riddles, the bond that Shylock draws up with Antonio is not carefully designed. It is drawn up in haste. Antonio thinks it a joke and foolishly signs it. Shylock is in a flurry of secret excitement because here is his chance to get even with Antonio, and like any con man he is eager to get a signature before the victim changes his mind. Thus they end up with the bond saying that if Antonio does not pay on time, Shylock is entitled to "a pound of flesh" from Antonio.
Despite being poorly thought out, the bond is legal, just like Portia's father's will. When Antonio is not able to pay on time, Shylock demands what he is entitled to in "my bond," as he calls it. Antonio's friends offer to pay the amount owed, or even two or three times its value, but Shylock will accept only a pound of flesh, and the bond does not say that the pound of flesh may be waived in exchange for repayment. Everyone, even the judge, is distressed by the situation.
Enter Portia, disguised as the young lawyer Balthasar. In her argument before the court, she holds up the legality of the bond. We cannot cast it aside, she says, because it would go down for a precedent, and soon everyone would feel free to break their contracts, all over Venice. In doing this, she earns the trust of Shylock and of the judge. But Portia does not uphold the bond simply because it will win her the game. She has great respect for the letter of the law.
But Portia also knows, from her experience with the caskets and probably from her training under her father before his death, that documents when examined closely do not always say what we thought they said. So she takes the bond very literally, and finds that it says nothing about blood. Therefore it is not legal for Shylock to shed any of Antonio's blood. Thus she turns the words of the bond back on Shylock, just like the words of the riddles were turned back on her unlucky suitors.
Notice also that in her role as Balthasar, Portia has to use again her skills of keeping her mouth shut and not showing her knowledge. She does not show who she is. She does not immediately blurt out her winning argument, but leads up to it slowly and even allows Shylock to prepare to cut into Antonio's flesh. Afterward, when Bassanio thanks her, she still does not blurt out who she is. She has learned to live and die by the law, to be cool under pressure, and to keep her own council. That is why she is such an amazing heroine.