About a year ago I was asked to take part in a careers event for maths students at the University of Oxford. I sat through presentations about banking and accountancy and felt at ease: my job can't compete on wages, but it's varied, fast-paced and hugely rewarding. Surely some of the bright young things in the audience would ignore the lure of the fat pay cheque and speak to me at the end? But just before my turn, a new presenter took the floor. He was from the UK Government Communications Headquarters (GCHQ). He worked as a codebreaker and was incredibly cool.

Mathematicians are in high demand at GCHQ, he said. The work is really interesting and it pays well. Where else could you say that your mathematical skills were being put to use to help protect the nation? You'll be serving Queen and country from a very comfortable office in Cheltenham.

Powerful stuff. To make it even more enticing, he pointed out that codebreakers weren't allowed to take work home and were strongly discouraged from staying later than 5pm. I started to have doubts about my own chosen career.

To remind myself that teaching really is the best job in the world, I decided that I could have a go at being a codebreaker myself and I'd get my year 8 class to join in. Initially I just used some resources from the Nrich website. I particularly like their transposition cipher because it helps cement pupils' understanding of factors and multiples. It was a fun lesson to do, but there was scope to develop it more.

This year, the opportunity came to do it again as a department wide cross-curricular project and I leaped at the chance. A colleague in the history department, Ross Bowrage, suggested putting the whole project in the context of World War II, getting our students to imagine they were in Bletchley Park deciphering messages to help the war effort.

Ross wrote a series of messages in English, which gave the students facts about the battle of the Atlantic and the fight to keep control of the supplies being shipped into Britain. I then set to work coding the messages using five different techniques: ceasar shift, pigpen cipher, transposition cipher, mono-alphabetic substitution and the Vigenère Square. After painstakingly coding some of the messages myself, I discovered Simon Singh's excellent code website called the Black Chamber which does the job for you and I sent yet another prayer of thanks to the god of the internet.

As my year 8 class walked through the door for their lesson, I told them they were no longer in 2013. The year was now 1940 and they had just entered Hut 8: Station X. Entering into the spirit of the thing, I gave myself the title of first officer King of the Women's Royal Naval Service and showed the students their brief. I told them about the dangerous journeys that supply ships had to make across the Atlantic and gravely informed them that their mathematical skills were required to help protect the ships. The students were enthralled. I resisted the urge to ask them to salute.

We started with the most basic code, the ceasar shift code, where the cipher alphabet is written under the normal plain text alphabet, but translated across by a given number of letters. For example, a shift of plus three would move all the letters three places along, so A becomes D and B becomes E.

Next up was the pigpen cipher, which uses symbols to replace each letter. This is a nice pictoral code that lower-attaining students generally enjoy working on.

After students had mastered these techniques, I asked for their attention again. "Urgent message from codebreaking HQ," I said. "The German army is using a new codebreaking technique. All personnel must be re-trained."

The students immediately put their pens down and listened carefully as I discussed how the transposition cipher worked by writing the message into a grid and then re-writing it without the grid, but reading down the page instead of across the page. The students quickly realised that they needed to work out the factor pairs of the total number of characters in any message and try out different sized grids.

Were any of the year 8 codebreakers ready to take on this new challenge? You bet they were. Hands shot up and I gave out the next set of coded messages to tackle.

Up and down the maths corridor, my colleagues were delivering lessons using the same resources and generally having a lot of fun. Students and teachers alike really embraced the scenario and enjoyed the opportunity to do something a bit different.

Codebreaking is not on the national curriculum and it does not need to be there. But we do need the freedom, and time, to do something different occasionally. We need the opportunity to engage our students, to inspire them and to have a bit of fun.

I'll end with the message one of my students wrote at the end of the lesson: "We have enjoyed learning about codes and World War II. We learned that mathematicians and musicians were good at cracking codes because they are good at finding patterns. We learned new words like enigma and U-boat. We learned that lots of women worked in Bletchley Park. And we learned that you need a lot of resilience to be a codebreaker."

A pretty worthwhile lesson then.

Charlotte teaches mathematics at Didcot Girls' Comprehensive School in Oxfordshire. She believes teaching and learning should be a creative process and is interested in developing project-based lessons that put the mathematics from the national curriculum into context. She recently began blogging on the subject.

This content is brought to you by Guardian Professional. To get articles direct to your inbox, and to access thousands of free resources, sign up to the Guardian Teacher Network here. Looking for your next role? See our Guardian jobs for schools site for thousands of the latest teaching, leadership and support jobs