Click here to Skip to main content
15,891,375 members
Home / Discussions / C#
   

C#

 
GeneralRe: Mark field in child class as NonSerialized Pin
deyaert22-Dec-08 0:58
deyaert22-Dec-08 0:58 
GeneralRe: Mark field in child class as NonSerialized Pin
Dust Signs22-Dec-08 1:01
Dust Signs22-Dec-08 1:01 
GeneralRe: Mark field in child class as NonSerialized Pin
deyaert22-Dec-08 1:02
deyaert22-Dec-08 1:02 
QuestionHow can I update rows in a table using OLEDB in C#? Pin
dliviu21-Dec-08 22:39
dliviu21-Dec-08 22:39 
AnswerRe: How can I update rows in a table using OLEDB in C#? Pin
Christian Graus21-Dec-08 22:45
protectorChristian Graus21-Dec-08 22:45 
GeneralRe: How can I update rows in a table using OLEDB in C#? Pin
dliviu21-Dec-08 22:55
dliviu21-Dec-08 22:55 
GeneralRe: How can I update rows in a table using OLEDB in C#? Pin
Christian Graus21-Dec-08 23:04
protectorChristian Graus21-Dec-08 23:04 
QuestionException of file encryption using c# Pin
cat ang21-Dec-08 21:04
cat ang21-Dec-08 21:04 
Hi,
Currently i am doing a project related to file encryption using c#. I new to the c# programming actually. I used microsoft visual c# express 2008. I had combine some of the code from internet reference, but i am facing a problem where program highlight "cs.Close();" and show
IndexOutOfRangeException was unhandled when decrypt button is clicked. The file decryption is unsuccessful due to this exception i think. Is there anybody know how to solve for this problem? Any suggestion on how to change the code? The code of my project is as below:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFD.Title = "Insert a File";
            openFD.InitialDirectory = "F://";
            openFD.FileName = "";
            DialogResult result = openFD.ShowDialog();
            string filename;
            filename = openFD.FileName;
            if (result == DialogResult.OK)
            {

                textBox2.Text = filename;

            }
            else
            {
                return;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string password = textBox1.Text;
            string salt_value = "abcdefg";
            string hash = "sha1";
            int passwordIterations = 10;
            string initVector = "@1A2C5D1F286AB54";
            int keySize = 128;
            Encrypt(@"filename", @"C:\Encrypted.ppt", password, salt_value, hash, passwordIterations, initVector, keySize);
           
        }
        public static void Encrypt(string _inputFile, string _outputFile, string password, string salt_value, string hash, int passwordIteration, string initVector, int keySize)
        {
            if (password.Length > 8)
                password = password.Substring(0, 8);
            else if (password.Length < 8)
            {
                int add = 8 - password.Length;
                for (int i = 0; i < add; i++)
                    password = password + i;
            }
            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] salt_valueBytes = Encoding.ASCII.GetBytes(salt_value);
            PasswordDeriveBytes key = new PasswordDeriveBytes(password, salt_valueBytes, hash, passwordIteration);
            byte[] keyBytes = key.GetBytes(keySize / 8);

            FileStream fsCrypt = new FileStream(_outputFile, FileMode.Create);


            RijndaelManaged RMCrypto = new RijndaelManaged();
            RMCrypto.Mode = CipherMode.CBC;
            CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(keyBytes, initVectorBytes), CryptoStreamMode.Write);

            FileStream fsIn = new FileStream(_inputFile, FileMode.OpenOrCreate);
            int data;
            while ((data = fsIn.ReadByte()) != -1)
                cs.WriteByte((byte)data);

            fsIn.Close();
            cs.Close();
            fsCrypt.Close();
            cs = null;
            fsIn = null;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            openFD.Title = "Insert a File";
            openFD.InitialDirectory = "F://";
            openFD.FileName = "";
            DialogResult result = openFD.ShowDialog();
            string filename;
            filename = openFD.FileName;
            if (result == DialogResult.OK)
            {

                textBox3.Text = filename;

            }
            else
            {
                return;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string password = textBox1.Text;
            string salt_value = "abcdefg";
            string hash = "sha1";
            int passwordIterations = 10;
            string initVector = "@1A2C5D1F286AB54";
            int keySize = 128;
            Decrypt(@"filename", @"C:\Decrypted.ppt", password, salt_value, hash, passwordIterations, initVector, keySize);
           
        }

        public static void Decrypt(string _inputFile, string _outputFile, string password, string salt_value, string hash, int passwordIteration, string initVector, int keySize)
        {
            if (password.Length > 8)
                password = password.Substring(0, 8);
            else if (password.Length < 8)
            {
                int add = 8 - password.Length;
                for (int i = 0; i < add; i++)
                    password = password + i;
            }
            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] salt_valueBytes = Encoding.ASCII.GetBytes(salt_value);
            PasswordDeriveBytes key = new PasswordDeriveBytes(password, salt_valueBytes, hash, passwordIteration);
            byte[] keyBytes = key.GetBytes(keySize /8);

            FileStream fsCrypt = new FileStream(_outputFile, FileMode.Create);

            RijndaelManaged RMCrypto = new RijndaelManaged();
            RMCrypto.Mode = CipherMode.CBC;
            CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(keyBytes, initVectorBytes), CryptoStreamMode.Write);




            FileStream fsIn = new FileStream(_inputFile, FileMode.OpenOrCreate);
            int data;
            while ((data = fsIn.ReadByte()) != -1)
                cs.WriteByte((byte)data);

            cs.Close();~IndexOutOfRangeException was unhandled
            fsIn.Close();
            fsCrypt.Close();

            cs = null;
            fsIn = null;
            fsCrypt = null;
        }
    }
}


The source code can be download from the link below:
http://cid-1caedb18b83acfa1.skydrive...Encryption.rar
If I am not disturbing,please guide. Thanks a lot!

Regards,
sf
AnswerRe: Exception of file encryption using c# Pin
Christian Graus21-Dec-08 22:42
protectorChristian Graus21-Dec-08 22:42 
GeneralRe: Exception of file encryption using c# Pin
cat ang22-Dec-08 2:16
cat ang22-Dec-08 2:16 
GeneralRe: Exception of file encryption using c# Pin
Colin Angus Mackay22-Dec-08 2:48
Colin Angus Mackay22-Dec-08 2:48 
AnswerRe: Exception of file encryption using c# Pin
Colin Angus Mackay22-Dec-08 2:46
Colin Angus Mackay22-Dec-08 2:46 
GeneralRe: Exception of file encryption using c# Pin
cat ang22-Dec-08 4:03
cat ang22-Dec-08 4:03 
Questionzedgraph Pin
hamidhakimi21-Dec-08 21:04
hamidhakimi21-Dec-08 21:04 
AnswerRe: zedgraph Pin
Garth J Lancaster21-Dec-08 22:35
professionalGarth J Lancaster21-Dec-08 22:35 
Questionhow to identify the button which is clicked Pin
prasadbuddhika21-Dec-08 20:28
prasadbuddhika21-Dec-08 20:28 
AnswerRe: how to identify the button which is clicked Pin
N a v a n e e t h21-Dec-08 20:41
N a v a n e e t h21-Dec-08 20:41 
AnswerRe: how to identify the button which is clicked Pin
Christian Graus21-Dec-08 20:51
protectorChristian Graus21-Dec-08 20:51 
Questionimage processing Pin
komathyc21-Dec-08 20:21
komathyc21-Dec-08 20:21 
AnswerRe: image processing Pin
Christian Graus21-Dec-08 20:51
protectorChristian Graus21-Dec-08 20:51 
QuestionServer path from DLL Pin
yesu prakash21-Dec-08 20:03
yesu prakash21-Dec-08 20:03 
AnswerRe: Server path from DLL Pin
Christian Graus21-Dec-08 20:05
protectorChristian Graus21-Dec-08 20:05 
GeneralRe: Server path from DLL Pin
yesu prakash21-Dec-08 20:23
yesu prakash21-Dec-08 20:23 
GeneralRe: Server path from DLL Pin
Vimalsoft(Pty) Ltd21-Dec-08 20:29
professionalVimalsoft(Pty) Ltd21-Dec-08 20:29 
GeneralRe: Server path from DLL Pin
yesu prakash21-Dec-08 20:47
yesu prakash21-Dec-08 20:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.