/// <summary> /// Detect Cypher Key based on Correlation Coefficient /// </summary> /// <param name="encryptedText">encryptedText</param> /// <returns>expected key</returns> public int DetectCypherKey(string encryptedText) { var characterCounts = new List <CharacterWithValue <int> >(); var characterFrequencies = new List <CharacterWithValue <double> >(); foreach (var item in StaticData.EnglishLanguageFrequencies) { // calculate character existing count int count = encryptedText.Select(i => i).Count(i => i == item.Character); characterCounts.Add(new CharacterWithValue <int>() { Character = item.Character, Value = count }); // calculate frequency of each character on the phrase (percentage) float frequency = (float)count / (float)encryptedText.Length; characterFrequencies.Add(new CharacterWithValue <double>() { Character = item.Character, Value = frequency }); } double bestCorrelation = -1; int bestShiftIndexKey = -1; int shiftIndexKey = 0; // get basic language characters frequencies double[] basicLangauageFrequencies = StaticData.EnglishLanguageFrequencies.Select(i => i.Value).ToArray(); // try all possibillities for shiting index while (shiftIndexKey < basicLangauageFrequencies.Count() - 1) { // shift frequencies double[] shiftedFrequencies = ArrayManupilation.ShiftByIndex <double>(characterFrequencies.Select(i => i.Value).ToArray(), shiftIndexKey); // calculate new correlation double correlation = CorrelationCoefficient.Calculate(basicLangauageFrequencies, shiftedFrequencies, basicLangauageFrequencies.Count()); // find best correlation exist (best correlation is the expected key) if (correlation > bestCorrelation) { bestCorrelation = correlation; bestShiftIndexKey = shiftIndexKey; } shiftIndexKey++; } return(basicLangauageFrequencies.Count() - bestShiftIndexKey); }
/** * Load and parse data from dat_mes.csv */ internal static void LoadData(StudyData studyData) { if (IsDataLoaded) { return; } List <string[]> rows = new List <string[]>(); using (StreamReader reader = File.OpenText("Data/dat_mes.csv")) { while (reader.Peek() >= 0) { string line = reader.ReadLine(); string[] rowArray = CSVRowToStringArray(line); if (rowArray.Length > 0) { rows.Add(rowArray); } } } string[] headers = rows[0]; rows.Remove(headers); /** * Parse each row array into a Study object. * Assumes CSV column ordering: * id,PublicationYear,n,r,VariablesControlled,StudyDesign, * AdherenceMeasure,ConscientiousnessMeasure ,MeanAge,MethodologicalQuality */ foreach (string[] row in rows) { PublicationYear publicationYear = studyData.PublicationYears.AddUnique(row[1]); CorrelationCoefficient correlationCoefficient = studyData.CorrelationCoefficients.AddUnique(row[2]); SampleSize sampleSize = studyData.SampleSizes.AddUnique(row[3]); VariablesControlled variablesControlled = studyData.VariablesControlled.AddUnique(row[4]); StudyDesign studyDesign = studyData.StudyDesigns.AddUnique(row[5]); AdherenceMeasure adherenceMeasure = studyData.AdherenceMeasures.AddUnique(row[6]); ConscientiousnessMeasure conscientiousnessMeasure = studyData.ConscientiousnessMeasures.AddUnique(row[7]); MeanAge meanAge = studyData.MeanAges.AddUnique(row[8]); MethodologicalQuality methodologicalQuality = studyData.MethodologicalQualities.AddUnique(row[9]); Study newStudy = new Study { Id = row[0], PublicationYear = publicationYear, CorrelationCoefficient = correlationCoefficient, SampleSize = sampleSize, VariablesControlled = variablesControlled, StudyDesign = studyDesign, AdherenceMeasure = adherenceMeasure, ConscientiousnessMeasure = conscientiousnessMeasure, MeanAge = meanAge, MethodologicalQuality = methodologicalQuality }; studyData.Studies.Add(newStudy); } IsDataLoaded = true; }