Former News of the World reporter Clive Goodman told the court he learned to protect his contacts after former editor Piers Morgan made a "shameful" decision to expose a former royal source in the 1990s.
Royal aide Ken Stronach had wanted the NotW to help him get a book deal for his life story. Instead, it was decided to do a story exposing him.
Mr Stronach was arrested, threatened with charges of theft of royal property and eventually released without charge and dismissed, he said.
Goodman said he was also "pulled up by the royal protection squad and threatened" with aiding and abetting. He said: "It was very, very nasty.
"What we did then was pretty discreditable. It was a pretty shameful thing to do."
Goodman said the incident taught him to protect his sources, even from colleagues, and secondly that it is "a dangerous world out there".
More top news
Pensioner's photo evidence being used in the hunt for two men captured on a stolen bike in an incident that saw him suffer a heart attack.
Footage filmed at an industrial estate in Stourbridge has emerged showing suspected migrants from Calais fleeing a lorry.
A baby sloth named Edward after his impressive Edward Scissorhands-like claws is being hand-reared with the help of a special teddy bear.