The scientists analysed coral samples from the Great Barrier Reef in Australia. Photograph: Australian Institute For Marine Science/PA

A secret from the sea could lead to a pill that prevents sunburn within five years, say scientists.

British researchers have uncovered the unique way coral shields itself against harmful ultraviolet (UV) rays.

They believe the discovery could pave the way for a sunscreen revolution with a tablet that protects both skin and eyes.

Scientists are close to producing a synthetic anti-sun compound based on those found in coral.

If the research progresses as hoped, the first sunscreen pill could become a reality in five years.

Such a product may have to be on prescription only to prevent people overdosing and harming their health.

Some skin reaction to UV is vital for the production of vitamin D. Too much sun protection can lead to vitamin D deficiency, resulting in weak bones.

Coral is an animal that only survives because of the algae living within it.

The mutually dependent relationship between the two organisms is the key to coral sun protection.

Dr Paul Long, who is heading the three-year project at King's College London, said: "What we have found is that the algae living within the coral makes a compound that we think is transported to the coral, which then modifies it into a sunscreen for the benefit of both the coral and the algae.

"Not only does this protect them both from UV damage, but we have seen that fish that feed on the coral also benefit from this sunscreen protection, so it is clearly passed up the food chain.

"This led us to believe that if we can determine how this compound is created and passed on, we could biosynthetically develop it in a laboratory to create a sunscreen for human use, perhaps in the form of a tablet, which would work in a similar way.

"We are very close to being able to reproduce this compound in the lab, and if all goes well we would expect to test it within the next two years."

Long led a team that analysed coral samples from Australia's Great Barrier Reef. By collecting coral in the dark on night dives and then exposing it to the sun, the scientists were able to see the sunscreen-manufacturing mechanism at work.

They believe coral might make more than 20 sun-protection compounds, but are focusing on the structure of just one. This will provide the "scaffold" for a laboratory made synthetic version.

Early tests will be conducted on human skin samples obtained from cosmetic surgeons, said Long.

The ultimate aim is to produce a pill that provides sun protection for the whole body.

"There would have to be a lot of toxicology tests done first but I imagine a sunscreen tablet might be developed in five years or so. Nothing like it exists at the moment."

The work is being funded by the taxpayer through the Biotechnology and Biological Sciences Research Council.

Another application of the research could lead to sun-tolerant crops that boost world food supplies.

Plants already possess the same genetic pathway discovered in coral algae, said Long.

Adding the extra genes from coral might make it possible to grow temperate crops, such as wheat and potatoes, that can thrive in the tropics.

Long added: "If we do this in crop plants that have been bred in temperate climates for high yield, but that at present would not grow in the tropics because of high exposure to sunlight, this could be a way of providing a sustainable nutrient-rich food source, particularly in need for [developing] world economies."