A century and a half after Confederate officer James Malbone wrote his Civil War diary partly in code, a couple of Yankees have figured out why he took the precaution: He liked to gossip.

Sprinkled amid entries on camp recipes and casualties are encrypted passages in which Malbone dishes on such juicy topics as a fellow soldier who got caught in bed with another man’s wife.

Malbone also writes about meeting the wife of Confederate President Jefferson Davis and describes her looks in an apparent echo of rumors at the time that she may have been of mixed race.

“That’s pretty shocking,” said Kent D. Boklan, the Queens College computer science professor and former National Security Agency cryptographer who deciphered Malbone’s code with little difficulty. “It’s a military diary and you expect military information, but you don’t expect the first lady of the Confederacy to make an appearance in this diary.”

Davis’ wife was a well-educated woman for her time, and as a result, was the target of “all kind of gossipy innuendos from the ladies” in Richmond, Virginia, the Confederate capital, according to Sam Craghead of the Museum of the Confederacy in Richmond.

Malbone, a lieutenant with the 6th Virginia Infantry Regiment, was severely wounded in the arm at the Battle of Chancellorsville in 1863. Assigned to light duty behind the lines, he used a leather-bound pocket diary to jot down his thoughts and even a poem.

Many of the entries were in a code he devised himself, consisting of a variety of symbols, including punctuation marks and a dollar sign, that corresponded to letters of the alphabet.

Other entries — names of deserters, costs of supplies — were written in plain text because the diary would have been submitted to his superiors so they could copy the information for their official records, according to Jim Gandy, librarian at the New York State Military Museum.

Gandy said the journal probably came into the possession of a New York soldier at the end of the war and wound up in the state’s vast collection. It is the only Confederate diary in the museum. There is no record there of Malbone’s ultimate fate.

It wasn’t until 2012 that a museum volunteer discovered the diary was written partly in code. The museum contacted Boklan, who had broken Union and Confederate codes used in other documents, and he completed the deciphering after working on it for a week in January.

“Technically, this is not very hard to break,” Boklan said. “There were some odd things. With a little bit of work and patience everything worked out.”