2 Replies - 1224 Views - Last Post: 07 June 2012 - 02:40 AM Rate Topic: -----

#1 TheDuceCat  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 30-May 12

Decrypting Video Files

Posted 06 June 2012 - 04:45 PM

Hey guys. I made a very simple program to encrypt and decrypt files. I got it to work with simple text and images, using the Bitmap class. Is there any easy way to write the decrypted data back into a video file? Every time I do it, Windows Media Player says that the file cannot be played.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.IO;


namespace Encryptor
{
    /// <summary>
    /// Interaction logic for Mainwindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        DESCryptoServiceProvider desCrypto;

        public MainWindow()
        {
            InitializeComponent();
        }

        [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
        public static extern bool ZeroMemory(ref string Destination, int Length);

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            desCrypto = new DESCryptoServiceProvider();
        }

        private void Browse(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog inputDialog = new Microsoft.Win32.OpenFileDialog();
            //inputDialog.Filter = "Text files (*.txt)|*.txt";
            inputDialog.ShowDialog();

            string result = inputDialog.FileName;
            if (result != "")
            {
                inputBox.Text = result;
            }
        }

        private void GenerateKey(object sender, RoutedEventArgs e)
        {
            desCrypto.GenerateKey();
            keyBox.Text = ASCIIEncoding.Unicode.GetString(desCrypto.Key);
        }

        private void Save(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog saveDialog = new Microsoft.Win32.SaveFileDialog();
            saveDialog.ShowDialog();

            string result = saveDialog.FileName;
            if (result != "")
            {
                outputBox.Text = result;
            }
        }

        private void Encrypt(object sender, RoutedEventArgs e)
        {
            FileStream fin = new FileStream(inputBox.Text, FileMode.Open, FileAccess.Read);
            FileStream fout = new FileStream(outputBox.Text, FileMode.Create, FileAccess.Write);

            desCrypto.Key = ASCIIEncoding.Unicode.GetBytes(keyBox.Text);
            desCrypto.IV = ASCIIEncoding.Unicode.GetBytes(keyBox.Text);

            ICryptoTransform encrypt = desCrypto.CreateEncryptor();
            CryptoStream cryptoStream = new CryptoStream(fout, encrypt, CryptoStreamMode.Write);

            byte[] byteInput = new byte[fin.Length];
            fin.Read(byteInput, 0, byteInput.Length);
            cryptoStream.Write(byteInput, 0, byteInput.Length);
            cryptoStream.Close();
            fin.Close();
            fout.Close();
        }

        private void Decrypt(object sender, RoutedEventArgs e)
        {
            FileStream fin = new FileStream(inputBox.Text, FileMode.Open, FileAccess.Read);

            desCrypto.Key = ASCIIEncoding.Unicode.GetBytes(keyBox.Text);
            desCrypto.IV = ASCIIEncoding.Unicode.GetBytes(keyBox.Text);

            ICryptoTransform decrypt = desCrypto.CreateDecryptor();
            CryptoStream cryptoStream = new CryptoStream(fin, decrypt, CryptoStreamMode.Read);

            if (image.IsChecked == true)
            {
                System.Drawing.Image img = System.Drawing.Image.FromStream(cryptoStream);
                img.Save(outputBox.Text);
                cryptoStream.Close();
            }

            else
            {
                string output = new StreamReader(cryptoStream).ReadToEnd();
                StreamWriter printer = new StreamWriter(outputBox.Text);

                printer.Write(output);
                cryptoStream.Close();
                printer.Flush();
                printer.Close();
            }
        }

        private void CheckImage(object sender, RoutedEventArgs e)
        {
            video.IsChecked = false;
        }

        private void CheckVideo(object sender, RoutedEventArgs e)
        {
            image.IsChecked = false;
        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Decrypting Video Files

#2 TheDuceCat  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 30-May 12

Re: Decrypting Video Files

Posted 06 June 2012 - 05:50 PM

I solved this. I changed my decrypting code to this:
cryptoStream.Read(byteInput, 0, byteInput.Length);
fout.Write(byteInput, 0, byteInput.Length);

fin.Close();
fout.Close();
cryptoStream.Close();


Was This Post Helpful? 1
  • +
  • -

#3 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: Decrypting Video Files

Posted 07 June 2012 - 02:40 AM

....Aaaand, thanks for sharing your solution.

I generally wrap all my streams in using Statement, it handles the flushing and closing of the streams automatically. It makes easier on you: you don't have to remember to flush and close the stream, just wrap it and forget it. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1