Example #1
0
        public void EncryptFile(PasswordSheet sheet)
        {
            FileStream stream = null;
            PasswordDeriveBytes secretKey = null;
            RijndaelManaged rijndaelCipher = null;
            ICryptoTransform encryptor = null;
            CryptoStream cryptoStream = null;

            try
            {
                stream = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.Write);
                stream.SetLength(0);

                secretKey = GetPasswordBytes();

                rijndaelCipher = new RijndaelManaged();
                encryptor = rijndaelCipher.CreateEncryptor(secretKey.GetBytes(32), secretKey.GetBytes(16));

                // Defines a stream that links data streams to cryptographic transformations
                cryptoStream = new CryptoStream(stream, encryptor, CryptoStreamMode.Write);

                byte[] data = null;
                using (var sheetStream = new MemoryStream())
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(PasswordSheet));
                    serializer.Serialize(sheetStream, sheet);

                    data = sheetStream.GetBuffer();
                }

                cryptoStream.Write(data, 0, data.Length);

            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
            }
            finally
            {
                if (secretKey != null)
                {
                    secretKey.Dispose();
                }

                if (rijndaelCipher != null)
                {
                    rijndaelCipher.Dispose();
                }

                if (cryptoStream != null)
                {
                    cryptoStream.Dispose();
                }
            }
        }
Example #2
0
        public void EncryptFile(PasswordSheet sheet)
        {
            FileStream          stream         = null;
            PasswordDeriveBytes secretKey      = null;
            RijndaelManaged     rijndaelCipher = null;
            ICryptoTransform    encryptor      = null;
            CryptoStream        cryptoStream   = null;

            try
            {
                stream = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.Write);
                stream.SetLength(0);

                secretKey = GetPasswordBytes();

                rijndaelCipher = new RijndaelManaged();
                encryptor      = rijndaelCipher.CreateEncryptor(secretKey.GetBytes(32), secretKey.GetBytes(16));

                // Defines a stream that links data streams to cryptographic transformations
                cryptoStream = new CryptoStream(stream, encryptor, CryptoStreamMode.Write);

                byte[] data = null;
                using (var sheetStream = new MemoryStream())
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(PasswordSheet));
                    serializer.Serialize(sheetStream, sheet);

                    data = sheetStream.GetBuffer();
                }


                cryptoStream.Write(data, 0, data.Length);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
            }
            finally
            {
                if (secretKey != null)
                {
                    secretKey.Dispose();
                }

                if (rijndaelCipher != null)
                {
                    rijndaelCipher.Dispose();
                }

                if (cryptoStream != null)
                {
                    cryptoStream.Dispose();
                }
            }
        }
Example #3
0
        public PasswordSheet DecryptFile()
        {
            PasswordSheet       decryptedData  = null;
            FileStream          stream         = null;
            PasswordDeriveBytes secretKey      = null;
            RijndaelManaged     rijndaelCipher = null;
            ICryptoTransform    decryptor      = null;
            CryptoStream        cryptoStream   = null;

            try
            {
                rijndaelCipher = new RijndaelManaged();

                // Making of the key for decryption
                secretKey = GetPasswordBytes();

                // Creates a symmetric Rijndael decryptor object.
                decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16));
                stream    = new FileStream(_fileName, FileMode.Open, FileAccess.Read);

                // Defines the cryptographics stream for decryption.THe stream contains decrpted data
                cryptoStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Read);

                XmlSerializer serializer = new XmlSerializer(typeof(PasswordSheet));
                decryptedData = (PasswordSheet)serializer.Deserialize(cryptoStream);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }

                if (secretKey != null)
                {
                    secretKey.Dispose();
                }

                if (rijndaelCipher != null)
                {
                    rijndaelCipher.Dispose();
                }

                if (cryptoStream != null)
                {
                    cryptoStream.Dispose();
                }
            }

            return(decryptedData);
        }