public void Attack(int paragraphCount, int letterCount, int attackType, KeyPredicate keyPred) { InitializeArrays(paragraphCount); int part = paragraphCount / Environment.ProcessorCount; Environment.ProcessorCount.Times(i => { Thread thread = new Thread(() => Attack(part, letterCount, attackType, i, keyPred)); thread.Priority = ThreadPriority.BelowNormal; thread.Start(); }); }
protected void Attack(int paragraphCount, int letterCount, int attackType, int turn, KeyPredicate keyPred) { for (int i = 0; i < paragraphCount; i++) { string opentext; if(cipher is Monoalphabetic) opentext = texts.RandomSpacesParagraph(letterCount); else opentext = texts.RandomParagraph(letterCount); string key = cipher.RandomKey(); string ciphertext = cipher.Encrypt(opentext, key); try { var crackKeys = cipher.Crack(ciphertext, attackType, Form1.currentLanguage); success[paragraphCount * turn + i] = keyPred(key, crackKeys.First()) ? 1 : 0; } catch (CryptanalysisCore.Exceptions.MatchNotFound) { } progress(); } SetDone(turn); lock (lockObject) { if (AreThreadsDone()) { afterFinish(string.Format("Úspěšnost: {0} %.", (int)Math.Round(((double)success.Sum() / (double)success.Length * 100)))); } } }