Esempio n. 1
0
        public static void runTests()
        {
            string     key        = "666";
            string     plainText  = "attack at down";
            CryptorAES aes        = new CryptorAES();
            CryptorDES des        = new CryptorDES();
            string     cipherText = aes.Encrypt(plainText, key);
            string     str        = aes.Decrypt(cipherText, key);

            Debug.Assert(str == plainText);
            cipherText = des.Encrypt(plainText, key);
            str        = des.Decrypt(cipherText, key);
            Debug.Assert(str == plainText);
        }
Esempio n. 2
0
        public static string Decrypt(string cipherText, string key, CipherType cipherType, int n)
        {
            try
            {
                int amount = n;

                string[] textArray = new string[amount];
                int      chunkSize = cipherText.Length / amount;
                int      pos       = 0;
                for (int i = 0; i < amount - 1; ++i)
                {
                    textArray[i] = cipherText.Substring(pos, chunkSize);
                    pos         += chunkSize;
                }
                textArray[amount - 1] = cipherText.Substring(pos);

                string[] resArray = new string[amount];
                ICrypto  cryptor;
                if (cipherType == CipherType.AES)
                {
                    cryptor = new CryptorAES();
                }
                else
                {
                    cryptor = new CryptorDES();
                }

                for (int i = 0; i < amount; ++i)
                {
                    resArray[i] = cryptor.Decrypt(textArray[i], key);
                }
                return(String.Join("", resArray));
            }
            catch (FormatException)
            {
                MessageBox.Show("Некорректные входные данные для дешифрования: входная строка не является шифртекстом");
                return("");
            }
            catch (Exception e)
            {
                MessageBox.Show("Unexpected error: " + e.Message);
                return("");
            }
        }
Esempio n. 3
0
        public static string Encrypt(string plainText, string key, CipherType cipherType, int n)
        {
            try
            {
                int amount = n;

                string[] textArray = new string[amount];
                int      chunkSize = plainText.Length / amount;
                int      pos       = 0;
                for (int i = 0; i < amount - 1; ++i)
                {
                    textArray[i] = plainText.Substring(pos, chunkSize);
                    pos         += chunkSize;
                }
                textArray[amount - 1] = plainText.Substring(pos);

                string[] resArray = new string[amount];
                ICrypto  cryptor;
                if (cipherType == CipherType.AES)
                {
                    cryptor = new CryptorAES();
                }
                else
                {
                    cryptor = new CryptorDES();
                }

                for (int i = 0; i < amount; ++i)
                {
                    resArray[i] = cryptor.Encrypt(textArray[i], key);
                }
                return(String.Join("", resArray));
            }
            catch (Exception e)
            {
                MessageBox.Show("Unexpected error: " + e.Message);
                return("");
            }
        }