示例#1
0
        public override (string, double) Break(string cipher = null)
        {
            if (cipher == null)
            {
                cipher = this.Cipher;
            }
            string plain   = cipher;
            double maxProb = FrequencyAnalyst.Analyze(cipher);

            for (int i = 1; i < cipher.Length; i++)
            {
                (string str, bool ok)result = Decode(cipher, i.ToString());
                if (result.ok)
                {
                    double prob = FrequencyAnalyst.Analyze(result.str);
                    if (prob > maxProb)
                    {
                        plain   = result.str;
                        maxProb = prob;
                    }
                }
            }
            this.Plain = plain;
            return(plain, Math.Pow(Math.E, maxProb));
        }
示例#2
0
        private List <Individual> CalMutation(Individual id)
        {
            StringBuilder tmp = new StringBuilder(id.key.Clone() as string);

            double fatherFitness = FrequencyAnalyst.Analyze(Decode(tmp.ToString()));

            List <Individual> kids = new List <Individual>();

            Random rand = new Random();

            for (int i = 0; i < Scheme.LetterSetSize; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    (tmp[i], tmp[j]) = (tmp[j], tmp[i]);
                    double r          = rand.NextDouble();
                    double newFitness = FrequencyAnalyst.Analyze(Decode(tmp.ToString()));

                    if (newFitness > fatherFitness)
                    {
                        kids.Add(new Individual(tmp.ToString(), this));
                    }
                    else if (r <= MutationProb)
                    {
                        kids.Add(new Individual(tmp.ToString(), this));
                    }

                    (tmp[i], tmp[j]) = (tmp[j], tmp[i]);
                }
            }

            return(kids);
        }
示例#3
0
        public List <int[]> CalMutation(int[] key)
        {
            int[] tmp = key.Clone() as int[];

            double fatherFitness = FrequencyAnalyst.Analyze(Decode(tmp));

            List <int[]> kids = new List <int[]>();

            Random rand = new Random();

            for (int i = 0; i < Scheme.LetterSetSize; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    (tmp[i], tmp[j]) = (tmp[j], tmp[i]);
                    double r          = rand.NextDouble();
                    double newFitness = FrequencyAnalyst.Analyze(Decode(tmp));

                    if (newFitness > fatherFitness)
                    {
                        kids.Add(tmp.Clone() as int[]);
                    }
                    else if (r <= MutationProb)
                    {
                        kids.Add(tmp.Clone() as int[]);
                    }

                    (tmp[i], tmp[j]) = (tmp[j], tmp[i]);
                }
            }

            return(kids);
        }
示例#4
0
        public override (string, double) Break(string cipher = null)
        {
            if (cipher == null)
            {
                cipher = this.Cipher;
            }

            string plain   = cipher;
            double maxProb = FrequencyAnalyst.Analyze(cipher);

            for (int i = 0; i < LetterSetSize; i++)
            {
                if (NumberTheory.Gcd(i, LetterSetSize) == 1)
                {
                    for (int j = 0; j < LetterSetSize; j++)
                    {
                        string a  = i.ToString();
                        string b  = j.ToString();
                        string ab = a + "," + b;
                        (string str, bool ok)result = Decode(cipher, ab);
                        if (result.ok)
                        {
                            double prob = FrequencyAnalyst.Analyze(result.str);
                            if (prob > maxProb)
                            {
                                plain   = result.str;
                                maxProb = prob;
                            }
                        }
                    }
                }
            }

            this.Plain = plain;
            ProcessLog.Enqueue(plain);
            ProcessLog.Enqueue("");
            return(plain, Math.Pow(Math.E, maxProb));
            //throw new NotImplementedException();
        }
示例#5
0
 public Individual(string key, IGA iga)
 {
     this.key     = key;
     this.fitness = FrequencyAnalyst.Analyze(iga.Decode(key));
 }