A Silicon Valley executive spent countless hours executing an elaborate scheme to cheat local Target stores out of Lego collectors blocks, prosecutors said Tuesday.

Thomas Langenbach, a vice president at the software company SAP Labs in Palo Alto, crafted fake bar codes and pasted them over the real thing on Lego packages in Target stores, Santa Clara County prosecutors said. The fakes gave Langenbach a steep discount, prosecutors said.

After purchasing the Legos, Langenbach allegedly sold them for a profit on eBay. At least some of the Legos were valuable collectors items featuring “Star Wars” characters, prosecutors said.

Hundreds of unopened Lego boxes were found in Langenbach’s San Carlos home, authorities said.

Prosecutors said the thefts were puzzling, because the scam was extremely labor-intensive and not particularly lucrative.

“He was going back to the same store, to different stores, then had to package them up, post them on eBay and send them out,” said Cindy Hendrickson, a supervising deputy district attorney in Santa Clara County.

EBay told prosecutors that 2,100 items from Langenbach’s account had been sold since April 2011, for a total of $30,000.

“You assume he paid a percentage of that as well,” Hendrickson said. “For someone in his position, this isn’t someone trying to live on that. There has to be an obsession.”

Langenbach was detained May 8 at a Target in Mountain View by security guards who had been watching him on store visits for about a month, Hendrickson said.

He had allegedly put three fake bar codes on items during his visit that day, bought one of the boxes and put the other two back on the shelf. Langenbach also had 32 fake bar codes in his car, Hendrickson said.

Prosecutors charged him Monday with four felony counts of second-degree burglary in connection with thefts at four Target stores in Santa Clara County. Hendrickson said authorities suspect Langenbach of pulling the same scam at other Target stores in the Bay Area.

Langenbach’s attorney, Thomas Greenberg, declined to comment. SAP Labs spokesman Jim Dever also would not comment about the case, but said Langenbach has worked for the company since 1988 and is still an employee.

Langenbach appeared in court Tuesday but did not enter a plea. His next court date is June 20.