Exemplo n.º 1
0
        public static string Decipher(string raw)
        {
            string originalDecipheredString = "";

            char[]        originalDecipheredChar = new char[raw.Length / 2];
            List <string> decipheredParts        = new List <string>();

            for (int i = 0; i < (raw.Length / 2); i++)
            {
                originalDecipheredChar[i] = raw[(i * 2) + 1];
            }
            originalDecipheredString = new string(originalDecipheredChar);
            int len = originalDecipheredString.Length / 4;

            for (int i = 0; i < 4; i++)
            {
                decipheredParts.Add(originalDecipheredString.Substring(i * len, len));
                decipheredParts[i] = VigenèreCipher.Decrypt(decipheredParts[i], keywords[i]);
            }
            char[] decipheredChar = new char[len];
            for (int j = 0; j < len; j++)
            {
                int sum = 0;
                for (int k = 0; k < 4; k++)
                {
                    sum += decipheredParts[k].ToCharArray() [j] - 65;
                }
                sum += 32;
                decipheredChar[j] = (char)sum;
            }

            return(new string(decipheredChar));
        }
        public void Vigenère()
        {
            VigenèreCipher cipher = new VigenèreCipher();

            const string key = "Lemon";

            const string plaintext  = "Attack at dawn";
            const string ciphertext = "Lxfopv ef rnhr";

            Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, key));
            Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, key));
        }
Exemplo n.º 3
0
        public static string Encipher(string value)
        {
            string              randomKey     = Keys.RandomKey(value.Length * 4);
            List <string>       cipheredParts = new List <string>();
            List <List <char> > keys          = new List <List <char> >()
            {
                new List <char>(),
                new List <char>(),
                new List <char>(),
                new List <char>()
            };

            var charArray = value.ToCharArray();
            int a = 0, b = 0;

            for (int i = 0; i < value.Length; i++)
            {
                a = 0;
                b = 0;
                int temp              = charArray[i] - 32;
                int temp_part         = temp / 4;
                int temp_parts_excess = temp % 4;

                if (i % 2 == 0)
                {
                    a = temp_parts_excess;
                }
                else
                {
                    b = temp_parts_excess;
                }
                keys[0].Add((char)(temp_part + a + 65));
                keys[1].Add((char)(temp_part + 65));
                keys[2].Add((char)(temp_part + 65));
                keys[3].Add((char)(temp_part + b + 65));
            }

            for (int i = 0; i < 4; i++)
            {
                cipheredParts.Add(new string(keys[i].ToArray()));
                cipheredParts[i] = VigenèreCipher.Encrypt(cipheredParts[i], keywords[i]);
            }
            var cipheredString = cipheredParts[0] + cipheredParts[1] + cipheredParts[2] + cipheredParts[3];

            char[] cipheredMsg = new char[cipheredString.Length * 2];
            for (int i = 0; i < cipheredString.Length; i++)
            {
                cipheredMsg[i * 2]       = randomKey[i];
                cipheredMsg[(i * 2) + 1] = cipheredString[i];
            }

            return(new string(cipheredMsg));
        }