예제 #1
0
        public void PlayfairTestDecode()
        {
            string  inputData    = "Munxt e yt ugw tnxngkq";
            string  keyWord      = "click";
            string  expectedData = "Press f to pay respect";
            ICipher userData     = new PlayfairCipher();
            string  actualData   = userData.Decode(inputData, keyWord);

            Assert.AreEqual(expectedData, actualData);
        }
예제 #2
0
        public IActionResult PlayfairDecrypt([FromBody] PlayfairCipherViewModel viewModel)
        {
            PlayfairCipher cipher = new PlayfairCipher(viewModel.Key);

            string decrypted = "";

            try
            {
                decrypted = cipher.Decrypt(viewModel.Message);
            }
            catch (Exception e)
            {
                return(BadRequest(new { Result = false, Message = e.Message }));
            }

            return(Json(decrypted));
        }
예제 #3
0
        public IActionResult PlayfairEncrypt([FromBody] PlayfairCipherViewModel viewModel)
        {
            PlayfairCipher cipher = new PlayfairCipher(viewModel.Key);

            string encrypted = "";

            try
            {
                encrypted = cipher.Encrypt(viewModel.Message);
            }
            catch (Exception e)
            {
                return(BadRequest(new { Result = false, Message = "Uzupełnij wartość tajnego klucza!" }));
            }

            return(Json(encrypted));
        }
예제 #4
0
        public IActionResult PlayfairVisualization([FromBody] PlayfairCipherViewModel viewModel)
        {
            PlayfairCipher cipher = new PlayfairCipher(viewModel.Key);

            string encrypted = "";
            string input     = viewModel.Message;

            string[] results = new string[4] {
                "output", "input", "table", "digrams"
            };
            input      = StringHelper.ReplaceWhitespace(input, "");
            input      = input.ToUpper();
            results[1] = input;
            char[,] table;
            try
            {
                encrypted  = cipher.Encrypt(viewModel.Message);
                results[0] = encrypted;

                table = cipher.KeyMatrix;

                var stringTable = new StringBuilder();
                for (int i = 0; i < 5; i++)
                {
                    for (int j = 0; j < 5; j++)
                    {
                        stringTable.Append(table[i, j]);
                    }
                }
                results[2] = stringTable.ToString();

                if (input.Length % 2 != 0)
                {
                    input += "X";
                }

                results[3] = input;
            }
            catch (Exception e)
            {
                return(BadRequest(new { Result = false, Message = e.Message }));
            }

            return(Json(results));
        }
예제 #5
0
        /*
         * Instantiates different ciphers based on the stack of used ciphers.
         */
        private bool LoadCiphers()
        {
            int i = 0;

            try
            {
                foreach (ListBoxItem currentItem in usedCiphers.Items)
                {
                    if (textBoxes[i].Text.Equals(""))
                    {
                        errorsTextBox.Text = "You must introduce a key for each cipher!";
                        return(false);
                    }
                    switch (currentItem.Content)
                    {
                    case "Caesar":
                        ciphers[i] = new CaesarCipher();
                        break;

                    case "Nihilist":
                        ciphers[i] = new NihilistCipher();
                        break;

                    case "Bifid":
                        ciphers[i] = new BifidCipher();
                        break;

                    case "Playfair":
                        ciphers[i] = new PlayfairCipher();
                        break;
                    }
                    ciphers[i].SetKey(textBoxes[i].Text);
                    textBoxes[i].Text = ciphers[i].GetKeyValue();
                    i++;
                }
            }
            catch (InvalidKeyFormatException ex)
            {
                errorsTextBox.Text = ex.Message;
                return(false);
            }

            return(true);
        }
예제 #6
0
        string DecryptPlayfair(string input, string key)
        {
            PlayfairCipher cipher = new PlayfairCipher();

            return(cipher.Decrypt(input, key));
        }
예제 #7
0
        public static void Main(string[] args)
        {
            var playfair = new PlayfairCipher(new DigrathGenerator('X'), new PlayfairKeyManagement());

            /*var a = playfair.EncryptMessage("message", "ZYCDEFGHIJKLMNOPRSTUVWXBA");*/

            /*
             * Console.WriteLine();
             *
             * Console.WriteLine(playfair.EncryptMessage("Hide the gold in the tree stump", "playfair example"));
             */


            var replacementDigraphs = new List <Digraph>
            {
                new Digraph('t', 'h'),
                new Digraph('h', 'e'),
                new Digraph('a', 'n'),
                new Digraph('i', 'n'),
                new Digraph('e', 'r'),
                new Digraph('r', 'e'),
                new Digraph('e', 's')
            };

            var replacementDigraphs2 = new List <Digraph>
            {
                new Digraph('h', 'e'),
                new Digraph('t', 'h'),
                new Digraph('i', 'n'),
                //new Digraph('e', 'r'),
                new Digraph('a', 'n'),
                new Digraph('r', 'e'),
                new Digraph('e', 's'),
                new Digraph('s', 't'),
                new Digraph('e', 'r'),
                new Digraph('t', 'e'),
                new Digraph('e', 'n'),
                new Digraph('a', 't')
            };

            var replacementDigraphs3 = new List <Digraph>
            {
                new Digraph('h', 'e'),
                new Digraph('t', 'h'),
                new Digraph('i', 'n'),
                new Digraph('r', 'e'),
                new Digraph('a', 'n'),
                new Digraph('e', 's'),
                new Digraph('s', 't'),
                new Digraph('t', 'e'),
                new Digraph('e', 'r'),
                new Digraph('e', 'n'),
                new Digraph('a', 't')
            };


            var msg =
                @"According to an October 1998 report by the United States Bureau of Land Management, approximately 65% of Alaska is owned and managed by the U.S. federal government as public lands, including a multitude of national forests, national parks, and national wildlife refuges.[21] Of these, the Bureau of Land Management manages 87 million acres (35 million hectares), or 23.8% of the state. The Arctic National Wildlife Refuge is managed by the United States Fish and Wildlife Service. It is the world's largest wildlife refuge, comprising 16 million acres (6.5 million hectares).

Of the remaining land area, the state of Alaska owns 101 million acres (41 million hectares), its entitlement under the Alaska Statehood Act. A portion of that acreage is occasionally ceded to organized boroughs, under the statutory provisions pertaining to newly formed boroughs. Smaller portions are set aside for rural subdivisions and other homesteading-related opportunities. These are not very popular due to the often remote and roadless locations. The University of Alaska, as a land grant university, also owns substantial acreage which it manages independently.

Another 44 million acres (18 million hectares) are owned by 12 regional, and scores of local, Native corporations created under the Alaska Native Claims Settlement Act (ANCSA) of 1971. Regional Native corporation Doyon, Limited often promotes itself as the largest private landowner in Alaska in advertisements and other communications. Provisions of ANCSA allowing the corporations' land holdings to be sold on the open market starting in 1991 were repealed before they could take effect. Effectively, the corporations hold title (including subsurface title in many cases, a privilege denied to individual Alaskans) but cannot sell the land. Individual Native allotments can be and are sold on the open market, however.

Various private interests own the remaining land, totaling about one percent of the state. Alaska is, by a large margin, the state with the smallest percentage of private land ownership when Native corporation holdings are excluded.";

            Console.WriteLine($"{msg}\n");
            var key = "playfair example";
            var ecr = playfair.EncryptMessage(msg, key);

            Console.WriteLine($"{ecr}\n");


            var pa = new PlayfairAnalyser(new DigrathGenerator('X'));

            Console.WriteLine($"{pa.ReplaceMostCommonDigraphs(ecr, replacementDigraphs3)}\n");

            var dcr = playfair.DecryptMessage(ecr, key);

            Console.WriteLine(dcr);


            Console.ReadKey();
        }