Li Jiaqi had seemed like a harmless old man, generous to his neighbors and fond of long walks in city parks.

Yet Li, 59, reportedly turned out to be a superspy who headed Taiwan's espionage network here. He was accused of spending thousands of dollars to obtain communist secrets for the rival regime. He adopted a disguise, passed coded messages in matchboxes and sent more than 100 intelligence reports written in invisible ink.

Although Li was arrested in April, his case heightened an official bout of spy mania that reflects a conservative challenge to Deng Xiaoping's five-year-old open-door policy.

Deng and his ruling moderate faction have seized the initiative for now with a forceful counterespionage campaign. The crackdown has netted 18 "enemy agents" in recent weeks and led to a reorganization of the nation's security apparatus to improve China's spy-catching capability. A new KGB-style Ministry of State Security has been formed to "forcefully exercise the functions of dictatorship" against foreign spies, according to its chief.

"Since China adopted the policy of opening to the outside world, intelligence agencies or secret services of some foreign countries have been stepping up their activities to spy out our state secrets and sending special agents for subversive and destructive purposes," said State Security Minister Ling Yun.

Primary attention has been directed at Taiwan operatives since April, but authorities also snared an alleged Soviet spy in the Northeast and convicted a Hong Kong newspaper editor on charges of passing secrets to the United States. In May, Communist Party leader Hu Yaobang said about 200 Chinese accused of spying for the Soviet Union were arrested last year.

Although the crackdown has an antiforeign character, diplomats view it as a move by Deng to protect his "great leap outward" by assuaging the xenophobic fears of old-line forces in the military and party.

"If the open door is to be a longstanding policy, it cannot be seen as an open invitation to foreign agents," said an Asian envoy.

Fear of foreign snooping in China is as old as the high walls surrounding Peking's Forbidden City, which have shielded rulers from inquisitive observers since European missionaries settled here in the 17th century.

Mao Tse-tung kept away foreigners for the first three decades of communist rule by practicing a revolutionary foreign policy that excluded much of the world.

Mao's modernizing successors, however, broke the isolation in 1978 by relaxing travel restrictions for overseas Chinese--including those with Taiwan connections--and by allowing thousands of journalists, students, business executives and diplomats to live in Chinese cities.

Foreigners still are required to live in special compounds and to deal with layers of bureaucracy to do official business, but the opening has permitted unprecedented access to a once closed society.

To Deng, the open door is necessary if China is to attract foreign capital and know-how for its modernizatioon. To his conservative opponents, however, it lowers the nation's defenses against foreign spies who now have the opportunity to compromise Chinese with consumer goods and cash. Although the issue has been a hot topic in internal party debates as far back as 1980, conservatives mounted their first public challenge 13 months ago with a commentary in Liberation Army Daily.

"We should be soberly aware," it said, "that foreign intelligence organizations and enemy spies are making every effort to disguise themselves as legal visitors to steal our secrets."

Deng moved to contain the backlash by stepping up counterespionage activities last year. Official media started giving unusually detailed accounts of police raids on clandestine radio stations, of "spy boats" lurking off China's coast and of Taiwan hit squads out to blow up mainland bridges and assassinate communist officials.

Li Jiaqi's case in April signaled an intensified phase of the crackdown, kicking off arrests in Tianjin, Shanghai and Liaoning provinces. All were charged with supplying secrets in return for cash from Taiwan agents in Hong Kong and Tokyo.

Of more lasting significance, however, was the establishment of the new State Security Ministry, approved by the parliament this month and given a charter to root out spies. The new agency appears to be modeled after the Soviet Union's KGB secret police, controlling domestic surveillance of subversives and counterintelligence activities, but not routine police work. It is unclear whether the ministry will undertake espionage abroad in the fashion of the KGB.

Little is known of China's intelligence activities in foreign countries. According to informed sources, the Army's general political department gathers military data while the party's investigation department collects political information, chiefly by infiltrating other espionage circles.

Counterespionage within China previously had been the responsibility of a shadowy state organization called the Political Security Bureau, which monitored foreigners' activities and their contacts with local residents, the sources said. It worked alongside the party's investigation department, which is said to have agents in every Chinese workplace as well as in neighborhoods, communes and foreign embassies.

Before the recent reorganization, the counterespionage bureau fell administratively under the Ministry of Public Security, a hydra-headed body that also had been responsible for criminal cases, traffic regulation, fire fighting and penal institutions.

The parliament made the bureau a separate ministry, leaving routine law enforcement in the hands of a streamlined Public Security Ministry.

Diplomats said this gives less political coloration to China's police, who no longer will be responsible for ideological control. But the restructuring also creates a new security organization with extraordinary powers that could become an influential force in Chinese politics as the KGB is in Kremlin affairs.

Chinese leaders did not give the new ministry control over quite as many aspects of public security as the KGB holds, however.

The People's Armed Police, which includes border patrols and government and foreign embassy guards, remains under control of the Ministry of Public Security.