예제 #1
0
        /// <summary>
        /// Constructor.
        /// </summary>
        public AesEncryption()
        {
            // Read the key and initialization vector from the secret file.
            string keyEncoded;
            string ivEncoded;

            using (StreamReader reader = new StreamReader(SecretFile))
            {
                keyEncoded = reader.ReadLine();
                ivEncoded  = reader.ReadLine();
            }

            // Decode data and store it in the appropriate instance fields.
            ConvertService convertService = new ConvertService();

            this.key = convertService.DecodeToByteArray(keyEncoded);
            this.initializationVector = convertService.DecodeToByteArray(ivEncoded);
        }
예제 #2
0
        string ICrypto.Decrypt(string textToDecrypt)
        {
            string decryptedText = null;

            byte[] bytesToDecrypt;

            // Convert the byte array with the encrypted text into a string.
            ConvertService convertService = new ConvertService();

            bytesToDecrypt = convertService.DecodeToByteArray(textToDecrypt);

            // Prepare an AES object.
            using (Aes aes = Aes.Create())
            {
                aes.Key = this.key;
                aes.IV  = this.initializationVector;

                // Create a decryptor for stream transformation.
                //ICryptoTransform decryptor = aes.CreateEncryptor(aes.Key, aes.IV);
                ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

                // Prepare necessary streams and encrypt the given text.
                using (MemoryStream memoryStream = new MemoryStream(bytesToDecrypt))
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader streamReader = new StreamReader(cryptoStream))
                        {
                            // Read (and decrypt) the given text.
                            decryptedText = streamReader.ReadToEnd();
                        }
                    }
                }
            }

            return(decryptedText);
        }