MEXICO CITY — The U.S. government is expanding a program to capture the biometric data of tens of thousands of Central Americans and other migrants arrested in Mexico, gaining unprecedented access to Mexican immigration jails to identify criminals, gang members and potential terrorists long before they reach the U.S. border.

Operating in detention facilities in southern Mexico and here in the capital, Department of Homeland Security officials have installed scores of screening terminals to collect migrants’ fingerprints, ocular scans and other identifying features, including tattoos and scars.

President Trump recently blasted Mexico as doing “very little, if not nothing” to stop the flow of people across Mexican territory en route to the United States. While he later softened his tone, Trump has not acknowledged that Mexican authorities have, over the past several years, allowed the United States a wider view into the identities and backgrounds of those often headed for the border. U.S. authorities see this partnership as a potential model for other countries, and they are in talks with Central American nations to adopt similar measures.

The information gathered is immediately forwarded to DHS and other U.S. law enforcement and intelligence databases, alerting American officials if an individual in Mexican custody is a convicted criminal or in a category known as “Special Interest Aliens,” which includes potential extremists, according to current and former U.S. officials who described the program on the condition of anonymity.

“These are bilateral programs that build Mexican capacity in a way that benefits our security,” said an official from the State Department’s Bureau of International Narcotics and Law Enforcement Affairs, which has budgeted $75 million to install the scanning equipment at immigration jails across Mexico and deliver mobile terminals that can transmit biometric data from almost anywhere.

Paid for through the $2.5 billion Merida security assistance program launched by President George W. Bush in 2008, the data-collecting effort requires the kind of U.S. access to Mexican facilities that would have been unthinkable a decade ago. It has been largely kept quiet by Mexican authorities, who risk public backlash over suspicions of American government technology and the perception that Washington interferes in the country’s affairs.

The two nations’ security cooperation came under fresh strain this week when Trump accused Mexico of allowing an unimpeded flow of Central Americans through Mexican territory, citing a caravan of more than 1,000 migrants that was traveling north through Mexico. When caravan organizers decided to end the journey in Mexico City, rather than head to the border, Trump thanked Mexico for its “strong immigration laws” and “willingness to use them.”

Related Trump vents about immigration in tweetstorm

Arrests by U.S. agents along the U.S.-Mexico border topped 50,000 in March, the highest one-month total since Trump took office. Over the past two decades, the character of illegal immigration has changed dramatically, as the number of Mexicans apprehended crossing illegally has plunged from more than 1.6 million in 2000 to 130,000 last year. Central Americans accounted for more than half of those arrested by U.S. border agents last year, and Mexico is now a transit country as well as a destination for Central Americans.

The surge of Central American families and children coming into Texas to seek asylum became a crisis for the Obama administration in 2014. Under U.S. pressure, Mexican authorities made it more difficult for Central Americans to pass freely to the United States. Mexican immigration officials set up new checkpoints near the Guatemalan border and caught and deported far more people.

Last year, the Mexican government detained 95,000 migrants, the vast majority from Central America’s gang-plagued Northern Triangle — Guatemala, Honduras and El Salvador. The three nations have some of the highest homicide rates in the world.

“I think we would have been in a significantly worse position in McAllen [Tex.] and the Rio Grande Valley if it wasn’t for the work that Mexico was doing on their southern border,” Gil Kerlikowske, who served as commissioner of U.S. Customs and Border Protection for part of President Barack Obama’s tenure, said in an interview.

“Mexico’s our ally. Mexico’s our friend,” he added. “The last thing we should be doing is criticizing them.”

The biometrics program took on new urgency during the upswing in Central American families flooding into the United States.

William Brownfield, who was head of the Bureau of International Narcotics and Law Enforcement Affairs when he retired last September after 38 years in the Foreign Service, said that the biometric program had been in development since 2012, starting with the software and other technology and that the two countries agreed to implement it at a bilateral security meeting in 2014.

“Part of the reason the Mexican government was interested is that it brought them value by giving them a window into who was coming into Mexico, as well as those simply en route to the United States,” Brownfield said in an interview. “And from the U.S. side, we accepted and understood that it is a lot easier, cheaper and more efficient to manage migrant flows from Central America to the U.S. at Mexico’s much smaller southern border than at our longer and more complicated [U.S.-Mexico] border.”

A spokesman for Mexico’s Interior Ministry said he had no information about the program. Spokesmen for the National Institute of Migration and the Foreign Ministry declined to comment.

Gustavo Mohar, a former top Mexican intelligence and immigration official, described the biometric program as “part of the bilateral package and the immigration policy of Mexico to prevent criminals or bad people from passing through Mexico and going to the United States.”

“This biometric interchange is a preventative measure to identify people of risk; it’s not that we’re letting pass gang members, rapists, everything that man said,” Mohar added, referring to Trump. “It’s frankly an open lie, what he’s saying.”

Because of the biometric data program, Mexican authorities have transferred dozens of criminal fugitives and suspects wanted in the United States to the border in the past year, handing them over to U.S. marshals or CBP officers. It was unclear whether any of the individuals had known or suspected terrorist ties, but U.S. military and State Department officials have long warned Congress that extremists could try to hire people smugglers to sneak them across the border.

The biometric program does not collect information on Mexican citizens, but over the past 13 months, it has gathered data on more than 30,000 migrants at immigration detention facilities in Tapachula, in southern Mexico, and at Iztapalapa, in Mexico City, according to figures obtained by The Washington Post.

The program is expanding this month into detention centers in Tijuana, Mexicali and Reynosa, according to U.S. officials, who said the goal is to have the capability to screen every migrant taken into custody in Mexico.

Of 21,000 migrants who were scanned last year, 5,500 had previously been arrested for crossing the U.S. border illegally, the figures show. Of those, 24 were identified as “alien smugglers,” and eight were known gang members.

The Department of Homeland Security has an officer in Mexico working full time inside the country’s detention facilities and seven other DHS “mentors” whose tasks include biometric equipment training and installation.

In addition, the United States is funding telecommunications towers at 10 Mexican navy bases along the Mexico-Guatemala border, as a way to improve coordination in remote jungle areas.

DHS officials in Washington referred inquiries to the U.S. Embassy in Mexico. State Department officials declined to confirm details about the program, citing law enforcement sensitivities.

Late Friday, the administration announced two moves intended to address Trump’s calls for a tougher posture on illegal immigration. In one, the president issued a memo directing an end to the practice he has branded “catch and release,” which allows some migrants apprehended while entering the United States illegally to leave detention facilities while their court dates are pending. Separately, Defense Secretary Jim Mattis signed an order authorizing the deployment of up to 4,000 National Guard troops to back up Border Patrol agents along the Mexico border.

The angry criticism from Trump over the past week about Mexico’s approach to handling migration increases the strain on cooperation between the countries and behind-the-scenes programs such as the biometrics work.

After Trump’s order Wednesday sending National Guard troops to the U.S. southern border, Mexican President Enrique Peña Nieto said in a national address that Trump should not use Mexico to score domestic political points and that Mexico would not negotiate with the United States at the expense of its sovereignty and dignity.

Angry Mexican senators this week also approved a nonbinding resolution calling on Peña Nieto to break off cooperation with the United States on security and migration issues. Laura Rojas, the head of the Senate’s Foreign Relations Committee, said in support of the resolution that Trump’s behavior has been “permanently and systematically disrespectful and insulting,” and based on “prejudices and misinformation,” while making “frequent use of threats and blackmail.”

Such talk is unnerving to the U.S. diplomats who have worked for decades to overcome mistrust in Mexico and persuade authorities here to allow American officials to work alongside them despite the risk of public backlash.

Although U.S. officials would like Mexican authorities to stiffen immigration enforcement along Mexico’s southern border, they say Mexico has been a vigilant and crucial partner in guarding against potential Islamic extremists who might be looking for a backdoor into the United States.

U.S. officials said they have had preliminary discussions with governments in Central America about expanding the biometric program there, giving nations across the region the ability to track gang members and other border-crossing criminals. “As we deal with the reality of political campaigns and elections and the inevitable rhetoric they produce, let us remember we are cooperating with Mexico because it is in our national interest,” Brownfield said. “I would regret, as a retired official, if we allowed such rhetoric to undercut or end cooperation that is delivering real value to Mexico and real value to the United States.”

Send questions/comments to the editors.