“I would give anything to go back and change this,” he said. “Anything.”

Mr. Hansen’s lawyers did not immediately respond to requests for comment on Tuesday night.

Image Ron Rockwell Hansen said in court Tuesday that “there simply are no words to accurately and fully express the depth of regret I have for my decisions and actions.” Credit... Salt Lake County Sheriff's Office, via Associated Press

John W. Huber, the United States attorney in Utah, said in a statement Tuesday that Mr. Hansen’s case was an example of how the “Chinese government continues to attempt to identify and recruit current and former members of the United States intelligence community.”

In June 2018, Kevin Mallory of Leesburg, Va., a former C.I.A. case officer, was found guilty of espionage and lying to the F.B.I. after a Chinese intelligence agent reached out to him on LinkedIn in 2017. He was sentenced to 20 years in prison.

In May, Jerry Chun Shing Lee, another former C.I.A. officer, pleaded guilty to a charge of conspiring with Chinese intelligence agents. He is scheduled to be sentenced in October in Alexandria, Va.

“These cases show the breadth of the Chinese government’s espionage efforts and the threat they pose to our national security,” John C. Demers, the assistant attorney general for national security, said.

Foreign operatives often use social media platforms for espionage recruitment. LinkedIn, a site mainly used for professional networking, is a prime hunting ground for Chinese spies, Western counterintelligence officials said.

Mr. Hansen was arrested in Seattle in June 2018 before he boarded a flight to China with secret military information, the authorities said. He was indicted later that month on 15 counts by a federal grand jury. Fourteen counts were dropped as part of his plea agreement in March.