One of the country’s most notorious serial killers hatched two secret plots to escape from his maximum-security lockdown so he could continue his bloodthirsty California rampage and go down in a blaze of glory, The Post has learned nine months after his death due to natural causes.

In the first plot, Richard Ramirez — whose nickname, “the Night Stalker,” was forever seared into the public’s consciousness during his frenzied 1980s murder spree — tried to conceal a handcuff key inside his rectum at San Quentin State Prison in 1993, official sources said.

The second occurred five years later, when Ramirez received a bizarre letter from one of his lovesick groupies that alluded to helping him bust out of prison, sources said.

But San Quentin authorities caught wind of the potentially deadly plots and quickly squashed them.

“From the moment Ramirez came to San Quentin, it was in his mind to escape,” said one source close to the matter. “If he went free, he would have killed a lot more people before the cops cut him down. That’s how he wanted it.”

The source added, “Being incarcerated went against everything Ramirez was about. He hated being cooped up in a cell, but he valued his freedom to roam the streets and kill.”

In 1984, the self-professed satanist launched a campaign of murder and mayhem that paralyzed Los Angeles and San Francisco.

When he wasn’t shooting, butchering and bludgeoning his many victims, he was raping, sodomizing and torturing them.

He would steal cars to go on hunting expeditions and break into homes in the middle of the night while the occupants were asleep.

Ramirez was finally apprehended in 1985 by a mob of East LA residents who recognized him from his mugshot plastered in the media.

The Texas-born drifter was convicted and sentenced to death for 13 murders, five attempted murders, 11 sexual assaults and 14 burglaries. He was suspected of several more slayings but never charged with them.

In June 2013, he died from complications arising from blood cancer. As The Post previously reported, he had turned a hideous shade of green just prior to his death in a hospital.

The first escape plot was foiled on Sept. 21, 1993, when Ramirez was escorted back to San Quentin after appearing in a San Francisco court on a murder rap.

As a correction officer used a wand to scan Ramirez’s body, the metal detector went off near his buttocks.

An X-ray later revealed that Ramirez had stuffed a handcuff key inside his rectum along with a ballpoint pen, a syringe and, strangely, a sticker that read, “I Luv chocolate.”

Ramirez told prison officials he needed the items for “self-defense.”

But the source, who spoke only on the condition of anonymity, said Ramirez was going to feign an illness so he could be taken to a hospital off prison grounds with only two cops keeping an eye on him.

“Two cops is a lot better than 200 at the prison,” the source noted. “He was then going to use the key to unlock the cuffs and go after the two cops.”

With the element of surprise on his side and his animalistic tendencies, Ramirez could have killed both officers, the source insisted.

“He would have then stolen a car and started to do his thing again,” the source said.

Ramirez’s second escape plan was hinted at in a 1998 letter sent to him by an obsessed fan two years after the savage killer had tied the knot with another woman during a wedding ceremony on Death Row.

In the June 8 letter, she stated, “Ya know why women have done such crazy things like helping or masterminding prison escapes ‘cuz they are driven to temporary insanity — insatiable desire made them crazy.”

Prison officials discovered the letter on June 18, 1998, while reviewing incoming mail. They noted that the groupie had visited Ramirez on the same day that she penned the letter and promptly cut off her access to him.

Neither Ramirez nor his pen pal was ever charged in the second case, apparently due to a lack of evidence.

Ramirez was charged by San Quentin officials with an escape attempt for the first incident. He pleaded guilty and lost 10 days of yard privileges.