In 1951, two policemen, Nock and Staehl, investigate mathematician Alan Turing after an apparent break-in at his home. Turing's suspicious behaviour and lack of war records triggers Nock's suspicion that he might be a Soviet spy. During his interrogation by Nock, Turing tells of his time working at Bletchley Park.
When Britain declares war on Germany in 1939, Turing travels to Bletchley Park, where, under the direction of Commander Alastair Denniston, he joins the cryptography team of Hugh Alexander, John Cairncross, Peter Hilton, Keith Furman, and Charles Richards. The team are trying to break the ciphers created by the Enigma machine, which the Nazis use to provide security for their radio messages.
Turing is difficult to work with and considers his colleagues inferior. He remembers 1927, when Turing was unhappy and bullied at boarding school. He developed a friendship with Christopher Morcom, who sparks an interest in cryptography, and eventually Turing developed romantic feelings for him. Before Turing could confess his love, Morcom dies from bovine tuberculosis.
He works alone to design a machine to decipher Enigma. After Denniston refuses to fund construction of the machine, Turing writes to Prime Minister Winston Churchill, who puts Turing in charge of the team and funds the machine. Turing fires Furman and Richards and places a difficult crossword in newspapers to find replacements. Joan Clarke, a Cambridge graduate, and Jack Good pass Turing's test, but Clarke's parents will not allow her to work with male cryptographers. Turing arranges for her to live and work with the female clerks who intercept the messages and shares his plans with her.
Turing's machine, which he names Christopher, is constructed but Turing cannot determine the Enigma settings before the Germans reset the encryption each day. Denniston orders it destroyed and Turing fired, but the other cryptographers threaten to leave if Turing is dismissed. After Clarke plans to leave on the wishes of her parents, Turing proposes marriage, which she accepts. During their reception, Turing confirms his homosexuality to Cairncross, who warns him to keep it secret. After overhearing a conversation with a clerk about messages she receives, Turing has an epiphany, realising he can program the machine to decode words he already knows exist in certain messages—such as the word "weather" (as the morning messages always include a weather forecast) and the phrase "Heil Hitler." After he recalibrates the machine, it quickly decodes a message and the cryptographers celebrate; however, Turing realises they cannot act on every decoded message or the Germans will realise Enigma has been broken.
Turing discovers that Cairncross is a Soviet spy. When Turing confronts him, Cairncross argues that the Soviets are allies working for the same goals and threatens to disclose Turing's homosexuality if Cairncross's role as an agent is revealed. When MI6 agent Stewart Menzies appears to threaten Clarke, Turing reveals that Cairncross is a spy. Menzies reveals that he knew this already and planted Cairncross among them in order to leak messages to the Soviets for British benefit. Fearing for her safety, Turing tells Clarke to leave Bletchley Park, revealing that he is homosexual and lying about never having cared for her. They break up, but she remains at Bletchley. After the war, Menzies tells the cryptographers to destroy their work and that they can never see one another again or share what they have done.
In the 1950s Turing is convicted of indecency and, in lieu of a jail sentence, undergoes chemical castration so he can continue his work. Clarke visits him in his home and witnesses his physical and mental deterioration. They reconcile as she reminds him that his work saved millions of lives.
In the end, the group is seen burning the documents, and a caption reveals Alan Turing committed suicide when he was 41 years old.