As defined in Linda and Roger Flavell’s book Dictionary of Idioms and Their Origins, a “bad egg” is an idiom that means an untrustworthy person, and a “good egg” is an idiom that means a dependable person (page 112).
“I think Peter’s tactics are sometimes wrong, but I thought what he did against Mugabe was incredibly brave. And he’s doing good work in a world where most people are too timid. So, yes, I think Peter Tatchell is a good egg.” – Elton John, quoted in the Sunday Telegraph, 9 September 2001
The theories around the origin of this idiom are lengthy and uncertain. The jist of it is this: it is essentially impossible to tell from looking at the shell or exterior of an egg whether it is fresh or not. But, once the egg is cracked open, you know immediately whether or not it is good or bad, as is the case with people. One’s outward appearance won’t reveal or expose the content of one’s character.
As far as when the idiom started appearing, it seems as though the term “bad egg” became popular in the mid-nineteenth century in school settings.