Пример #1
0
        private static void aesEncryptFile(CipherMode mode, byte[] keyBytes, byte[] ivBytes, string inputFile, string outputFile)
        {
            //Setting Up the AES object
            Aes aes = Aes.Create();

            aes.Mode = mode;
            aes.Key  = keyBytes;

            //For ECB it will ignore the IV anyway so we dont have to worry about checking mode
            if (ivBytes != null)
            {
                aes.IV = ivBytes;
            }
            else
            {
                // In the case where the user has not provided the IV
                // It will print the IV to the console. We would not usally do
                // this. But this allows me to check my output.
                printIV(aes.IV);
            }

            // The Assignment doesn't seem require us to create code that will decrpyt the file as well.
            // However, if you wanted to decrypt as well, it would be the same code as this method except
            // here you would call aes.CreateDecryptor().
            ICryptoTransform cryptoTransform = aes.CreateEncryptor();

            // Here we open the input file
            using (FileStream outputFileStream = new FileStream(outputFile, FileMode.Create))
            {
                using (CryptoStream cryptoStream = new CryptoStream(outputFileStream, cryptoTransform, CryptoStreamMode.Write))
                {
                    // We are reading in blocks in a time just to make it a bit faster
                    var totalBytesRead = 0;
                    var offset         = 0;

                    // This is how many bits per block we read in a time. However, it
                    // However, this has nothing do with AES block size. We could choose
                    // any block size. But we might as well use the AES block size and
                    // hopefully save some padding.
                    int    inputBlockSize = 128;
                    byte[] inputData      = new byte[inputBlockSize];

                    using (FileStream inputFileStream = new FileStream(inputFile, FileMode.Open))
                    {
                        var bytesRead = 0;
                        do
                        {
                            bytesRead = inputFileStream.Read(inputData, 0, inputBlockSize);
                            offset   += bytesRead;
                            // The Crypto Stream will use the transformer we declared above
                            // to actually carry out the transformation.
                            cryptoStream.Write(inputData, 0, bytesRead);
                            totalBytesRead += bytesRead;
                        }while (bytesRead > 0);

                        inputFileStream.Close();
                    }

                    cryptoStream.FlushFinalBlock();
                    cryptoStream.Close();
                }
                outputFileStream.Close();
            }
        }
Пример #2
0
        private void encryptButton_Click(object sender, RoutedEventArgs e)
        {
            string str = putTextBox.Text;

            UnicodeEncoding asen  = new UnicodeEncoding();
            ASCIIEncoding   ascii = new ASCIIEncoding();

            // Key
            byte[] key    = new byte[16];
            int    padNum = 0;
            string keystr;

            if (16 < keyBox.GetLineLength(0))
            {
                MessageBox.Show("Key too long...Only use 16 bytes");
                return;
            }
            else if (16 > keyBox.GetLineLength(0))
            {
                padNum = 16 - keyBox.GetLineLength(0);
                keystr = keyBox.Text;
                for (int i = 0; i < padNum; i++)
                {
                    keystr += "x";
                }
                key = ascii.GetBytes(keystr);
            }
            else
            {
                key = ascii.GetBytes(keyBox.Text);
            }

            // Encrypt the string to an array of bytes.
            byte[] encrypted;
            byte[] IV;
            // Time analysis of encryption
            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = key;
                aesAlg.GenerateIV();
                IV = aesAlg.IV;

                aesAlg.Mode = CipherMode.ECB;

                var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
                using (var msEncrypt = new MemoryStream())
                {
                    using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (var swEncrypt = new StreamWriter(csEncrypt))
                        {
                            //Write all data to the stream.
                            swEncrypt.Write(str);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }

            var combinedIvCt = new byte[IV.Length + encrypted.Length];

            Array.Copy(IV, 0, combinedIvCt, 0, IV.Length);
            Array.Copy(encrypted, 0, combinedIvCt, IV.Length, encrypted.Length);
            string output = System.Convert.ToBase64String(combinedIvCt);

            // combinedIvCt is now the encrypted message
            putTextBox.Text = output;
        }