A secret code used by the Spanish king Ferdinand II of Aragon 500 years ago has been cracked by Spain’s intelligence agency.

The coding system contained over 200 special characters and was used by the king to communicate with his military commander, Gonzalo Fernández de Córdoba.

No one had been able to decipher the letters for centuries, as the substitution table the king used to create the code had not been preserved.

However, when the Army Museum in Toledo displayed the letters in 2015, experts from the Spanish National Intelligence Center (CNI) were asked for help to crack the code.

The museum provided the CNI with two letters written in 1502 and 1506.

At the foot of one letter de Córdoba, known as “The Great Captain”, had deciphered a few sentences, allowing the experts an insight into the code.

Within the letters the CNI specialists found orders, threats and instructions for military engagements.

The discovery was described by Spanish national daily newspaper ABC as “one of the great mysteries in the history of Spain”.

In total, 88 different symbols and 237 “combined letters” were used to create the ”Great Captain code”.

Encrypted letters were a common system in Italy at the time Ferdinand II wrote his, but the experts who broke the code described this one as “very well thought out”.

It is a precursor of the so-called “Vigenère cipher”, but an advanced form that was not widely used until the 17th century.

Ferdinand II was the king behind the final defeat of the Moors in Spain in 1492, as well as the sponsor of Christopher Columbus on his discovery of the New World.

The decoded letters dated from the time of the second French invasion of Naples, which the Spanish controlled at the time.

They therefore represent a key moment in Spanish history, when Spain and France were fighting for dominance in the Mediterranean region.

Historian José Enrique Ruiz-Domènec, a specialist in de Córdoba, told ABC this was a highly important finding that showed the Spanish king in a new light.