public void DecryptFromResources(string inputName, int key, string sampleName, double accuracy, dynamic sup) { LettersSetSettings((TypeLettersSupport)sup); string expect = Properties.Resources.ResourceManager.GetString(inputName); string sample = Properties.Resources.ResourceManager.GetString(sampleName); CharacterFrequencyAnalyzer an = new CharacterFrequencyAnalyzer(sample); string dec = an.Decrypt(Encryption.Encrypt(expect, key)); int errors = 0; int all = 0; for (int i = 0; i < dec.Length || i < expect.Length; i++) { if (i < dec.Length || i < expect.Length) { if (LettersSupport.Contains(expect[i])) { all++; if (dec[i] != expect[i]) { errors++; } } } else { Console.Write($"{i}:{(i < dec.Length ? dec[i] : expect[i])}"); } } Console.WriteLine(); double accuracyCurrent = 1 - (double)errors / all; Console.WriteLine("accuracy: " + accuracyCurrent); Console.WriteLine("dec: " + dec); Assert.IsTrue(accuracyCurrent > accuracy); }
private char GetDecryptChar(char v, SortedList <BigInteger, char> sample) { if (LettersSupport.Contains(v)) { return(TopSample.Values[(int)Math.Round((double)sample.IndexOfValue(v) / sample.Count * TopSample.Count)]); } return(v); }
private static char EncryptChar(char input, int key) { int i = LettersSupport.IndexOf(input); if (i == -1) { return(input); } i += key; while (i < 0) { i += LettersSupport.Count; } while (i >= LettersSupport.Count) { i -= LettersSupport.Count; } return(LettersSupport[i]); }
private async Task <SortedList <BigInteger, char> > AnalyzeAsync(StreamReader sample) { ConcurrentDictionary <char, BigInteger> map = new ConcurrentDictionary <char, BigInteger>(); Memory <char> buffer = new Memory <char>(new char[1024 * 1024 * 256]); int i = 0; while ((i = await sample.ReadAsync(buffer)) > 0) { for (int j = 0; j <= i; j++) { if (LettersSupport.Contains(buffer.Span[j])) { map[buffer.Span[j]] = map.ContainsKey(buffer.Span[j]) ? map[buffer.Span[j]] + 1 : 1; } } } BigInteger maxRepeat = SearchMaxReapeat(map.Values); if (maxRepeat > 1) { Parallel.ForEach(map.Keys, key => { map[key] *= maxRepeat; }); } SortedList <BigInteger, char> output = new SortedList <BigInteger, char>(map.Count); foreach (KeyValuePair <char, BigInteger> pair in map) { BigInteger key = pair.Value; while (output.ContainsKey(key)) { key++; } output.Add(key, pair.Key); } return(output); }