WASHINGTON — A former State Department diplomatic security officer and military contractor was charged with conducting espionage for China after F.B.I. agents found top-secret documents and apparently incriminating messages on a communications device he brought back from Shanghai, the Justice Department said on Thursday.

The man, Kevin Patrick Mallory, 60, of Leesburg, Va., made his initial appearance in Federal District Court in Alexandria, the department said. He is also charged with lying to federal investigators.

“The conduct alleged in this complaint is serious, and these charges should send a message to anyone who would consider violating the public’s trust and compromising our national security by disclosing classified information,” Dana J. Boente, the acting assistant attorney general for national security and United States attorney for the Eastern District of Virginia, said in a statement.

The Run-Up

The podcast that makes sense of the most delirious stretch of the 2016 campaign.

Geremy C. Kamens, a federal public defender identified on the docket as representing Mr. Mallory, did not respond to an email inquiry.

After a stint as a State Department agent from 1987 to 1990, Mr. Mallory worked for the military, defense contractors, and “various government agencies” before leaving government in 2012, according to a complaint filed by an F.B.I. agent.

In April, it said, Mr. Mallory was returning from a trip to Shanghai when a Customs and Border Protection search of his carry-on luggage revealed that he was bringing $16,500 in undeclared cash into the United States.

In a subsequent F.B.I. interview, it said, Mr. Mallory said he had met an unnamed person at Shanghai think tank that the United States government believes acts as a cutout for the Chinese intelligence service

But Mr. Mallory told the F.B.I. he believed that the person and his boss, whom Mr. Mallory also met, might be Chinese intelligence agents, the complaint said, and he showed the bureau a communications device the person had given him.

A subsequent search of the device, the complaint said, recovered several classified documents and messages in which Mr. Mallory had discussed removing classification markings from documents he was transmitting.

“Your object is to gain information, and my object is to be paid for,” he is said to have written in a message sent on May 5.

The complaint did not identify the subject of the documents or which government agency they came from.