/// <summary> /// Encrypts or decrypts a string with the given key (rotor positions) and formats /// the output according to the settings /// </summary> /// <param name="rotor1Pos">Position of rotor 1 (fastest)</param> /// <param name="rotor2Pos">Position of rotor 2 (middle)</param> /// <param name="rotor3Pos">Position of rotor 3 (slowest)</param> /// <param name="rotor4Pos">Position of rotor 4 (extra rotor for M4)</param> /// <param name="text">The text for en/decryption. This string may contain /// arbitrary characters, which will be dealt with according to the settings given</param> /// <returns>The encrypted/decrypted string</returns> private string FormattedEncrypt(int rotor1Pos, int rotor2Pos, int rotor3Pos, int rotor4Pos, string text) { String input = preFormatInput(text); enigmaPresentationFrame.ChangeStatus(isrunning, enigmaPresentationFrame.EnigmaPresentation.IsVisible); if (Presentation.IsVisible && enigmaPresentationFrame.EnigmaPresentation.PresentationDisabled.DisabledBoolProperty) { String output = core.Encrypt(rotor1Pos, rotor2Pos, rotor3Pos, rotor4Pos, input); enigmaPresentationFrame.EnigmaPresentation.output = output; if (enigmaPresentationFrame.EnigmaPresentation.checkReady()) { enigmaPresentationFrame.EnigmaPresentation.setinput(input); } else { LogMessage("Presentation Error!", NotificationLevel.Error); } //myPresentation.playClick(null, EventArgs.Empty); //return postFormatOutput(output); return(""); } else { return(postFormatOutput(core.Encrypt(rotor1Pos, rotor2Pos, rotor3Pos, rotor4Pos, input))); } }
/// <summary> /// This method performs a trial encryption with the given rotor positions (i.e. the key) /// If the trial encryption results in a better result as before encountered, the current settings will /// remembered in analysisCandidates-List. /// </summary> /// <param name="rotor1Pos">Integer value for rotor 1 position (values range from 0 to 25)</param> /// <param name="rotor2Pos">Integer value for rotor 2 position (values range from 0 to 25)</param> /// <param name="rotor3Pos">Integer value for rotor 3 position (values range from 0 to 25)</param> /// <param name="text">The ciphertext</param> /// <returns>The result of encrypting/decrypting the ciphertext with the given key</returns> private string checkKey(int rotor1Pos, int rotor2Pos, int rotor3Pos, string text) { string result = core.Encrypt(rotor1Pos, rotor2Pos, rotor3Pos, 0, text); double newScore = calculateScore(result, settings.KeySearchMethod); if (analysisCandidates.Count >= maxAnalysisEntries) { // List is full, check if we need to remove one if (newScore > analysisCandidates[0].Score) { double currentMax = analysisCandidates[analysisCandidates.Count - 1].Score; analysisConfigSettings csetting = new analysisConfigSettings(); csetting.Score = newScore; csetting.Rotor1 = core.Rotor1; csetting.Rotor2 = core.Rotor2; csetting.Rotor3 = core.Rotor3; csetting.Ring1 = core.Ring1; csetting.Ring2 = core.Ring2; csetting.Ring3 = core.Ring3; csetting.PlugBoard = core.Plugboard; csetting.Key = settings.Alphabet[rotor3Pos].ToString() + settings.Alphabet[rotor2Pos].ToString() + settings.Alphabet[rotor1Pos].ToString(); analysisCandidates.Add(csetting); analysisCandidates.Sort(); // remove the smallest one analysisCandidates.RemoveAt(0); if (newScore > currentMax) { // new best option string status = String.Format("ANALYSIS: ==> Found better rotor settings: {0},{1},{2}; {3},{4},{5}; Key: {6}; I.C.={7} <==", (rotorEnum)csetting.Rotor3, (rotorEnum)csetting.Rotor2, (rotorEnum)csetting.Rotor1, csetting.Ring3.ToString("00"), csetting.Ring2.ToString("00"), csetting.Ring1.ToString("00"), csetting.Key, newScore.ToString()); pluginFacade.LogMessage(status, NotificationLevel.Info); printBestCandidates(); // fire the event, so someting becomes visible.. if (OnIntermediateResult != null) { OnIntermediateResult(this, new IntermediateResultEventArgs() { Result = result }); } } } } else { //there is room left, hence add the element analysisConfigSettings csetting = new analysisConfigSettings(); csetting.Score = newScore; csetting.Rotor1 = core.Rotor1; csetting.Rotor2 = core.Rotor2; csetting.Rotor3 = core.Rotor3; csetting.Ring1 = core.Ring1; csetting.Ring2 = core.Ring2; csetting.Ring3 = core.Ring3; csetting.PlugBoard = core.Plugboard; csetting.Key = settings.Alphabet[rotor3Pos].ToString() + settings.Alphabet[rotor2Pos].ToString() + settings.Alphabet[rotor1Pos].ToString(); analysisCandidates.Add(csetting); analysisCandidates.Sort(); if (analysisCandidates.Count == maxAnalysisEntries) { printBestCandidates(); // current best option analysisConfigSettings bestOption = analysisCandidates[analysisCandidates.Count - 1]; string status = String.Format("ANALYSIS: Best candidates is filled. Best option so far: {0},{1},{2}; Key: {3}; I.C.={4}", (rotorEnum)bestOption.Rotor3, (rotorEnum)bestOption.Rotor2, (rotorEnum)bestOption.Rotor1, bestOption.Key, bestOption.Score.ToString()); pluginFacade.LogMessage(status, NotificationLevel.Debug); // fire the event, so someting becomes visible.. if (OnIntermediateResult != null) { OnIntermediateResult(this, new IntermediateResultEventArgs() { Result = result }); } } } return(result); }