Click here to Skip to main content
15,904,348 members
Home / Discussions / C#
   

C#

 
GeneralRe: Export and save dataset to excel Pin
kdwarak22-Sep-09 1:16
kdwarak22-Sep-09 1:16 
GeneralRe: Export and save dataset to excel Pin
Programm3r22-Sep-09 1:26
Programm3r22-Sep-09 1:26 
AnswerRe: Export and save dataset to excel Pin
Amit Patel198521-Sep-09 23:35
Amit Patel198521-Sep-09 23:35 
QuestionIs it possible to reconnect an event without raising it? Pin
CBenac21-Sep-09 13:19
CBenac21-Sep-09 13:19 
AnswerRe: Is it possible to reconnect an event without raising it? Pin
Gideon Engelberth21-Sep-09 15:54
Gideon Engelberth21-Sep-09 15:54 
GeneralRe: Is it possible to reconnect an event without raising it? Pin
CBenac22-Sep-09 8:30
CBenac22-Sep-09 8:30 
QuestionHow to start a NamedPipeClientStream and a NamedPipeServerStream together Pin
AndyASPVB21-Sep-09 13:19
AndyASPVB21-Sep-09 13:19 
AnswerRe: How to start a NamedPipeClientStream and a NamedPipeServerStream together Pin
Michael Eber22-Sep-09 19:15
Michael Eber22-Sep-09 19:15 
QuestionSilverlight replace elements app Pin
User 602357121-Sep-09 11:14
User 602357121-Sep-09 11:14 
AnswerRe: Silverlight replace elements app Pin
Henry Minute21-Sep-09 11:35
Henry Minute21-Sep-09 11:35 
QuestionProblem with delegate for C++ DLL callback function Pin
Matthew Page21-Sep-09 11:10
Matthew Page21-Sep-09 11:10 
AnswerRe: Problem with delegate for C++ DLL callback function Pin
Matthew Page21-Sep-09 11:54
Matthew Page21-Sep-09 11:54 
QuestionAdding custom properties to a DataGridViewRow Pin
theallmightycpd21-Sep-09 9:33
theallmightycpd21-Sep-09 9:33 
AnswerRe: Adding custom properties to a DataGridViewRow Pin
Henry Minute21-Sep-09 11:41
Henry Minute21-Sep-09 11:41 
GeneralRe: Adding custom properties to a DataGridViewRow Pin
theallmightycpd22-Sep-09 3:39
theallmightycpd22-Sep-09 3:39 
GeneralRe: Adding custom properties to a DataGridViewRow Pin
Henry Minute22-Sep-09 4:08
Henry Minute22-Sep-09 4:08 
QuestionUsing StringBuilder inside another class? Pin
helfri6221-Sep-09 9:27
professionalhelfri6221-Sep-09 9:27 
AnswerRe: Using StringBuilder inside another class? Pin
Luc Pattyn21-Sep-09 9:40
sitebuilderLuc Pattyn21-Sep-09 9:40 
GeneralRe: Using StringBuilder inside another class? Pin
helfri6221-Sep-09 10:00
professionalhelfri6221-Sep-09 10:00 
GeneralRe: Using StringBuilder inside another class? Pin
Luc Pattyn21-Sep-09 10:10
sitebuilderLuc Pattyn21-Sep-09 10:10 
QuestionThread reach synchronized code block Pin
FJJCENTU21-Sep-09 6:33
FJJCENTU21-Sep-09 6:33 
AnswerRe: Thread reach synchronized code block Pin
N a v a n e e t h21-Sep-09 7:01
N a v a n e e t h21-Sep-09 7:01 
Question[SOLVED] File Encryption Broken [modified] Pin
Ben Magee21-Sep-09 6:26
Ben Magee21-Sep-09 6:26 
Hello,

I Am using the following functions to encrypt and decrypt files:

Encrypt:
private void EncryptFile(string inputFile, string outputFile)
{

    try
    {
        string password = @"19651969"; // Your Key Here
        UnicodeEncoding UE = new UnicodeEncoding();
        byte[] key = UE.GetBytes(password);

        string cryptFile = outputFile;
        FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);

        RijndaelManaged RMCrypto = new RijndaelManaged();

        CryptoStream cs = new CryptoStream(fsCrypt,
            RMCrypto.CreateEncryptor(key, key),
            CryptoStreamMode.Write);

        FileStream fsIn = new FileStream(inputFile, FileMode.Open);

        int data;
        while ((data = fsIn.ReadByte()) != -1)
            cs.WriteByte((byte)data);


        fsIn.Close();
        cs.Close();
        fsCrypt.Close();
    }
    catch
    {
        MessageBox.Show("Encryption failed!", "Error");
    }
}


Decrypt:
private void DecryptFile(string inputFile, string outputFile)
{

    {
        string password = @"19651969"; // Your Key Here

        UnicodeEncoding UE = new UnicodeEncoding();
        byte[] key = UE.GetBytes(password);

        FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);

        RijndaelManaged RMCrypto = new RijndaelManaged();

        CryptoStream cs = new CryptoStream(fsCrypt,
            RMCrypto.CreateDecryptor(key, key),
            CryptoStreamMode.Read);

        FileStream fsOut = new FileStream(outputFile, FileMode.Create);

        int data;
        while ((data = cs.ReadByte()) != -1)
            fsOut.WriteByte((byte)data);

        fsOut.Close();
        cs.Close();
        fsCrypt.Close();

    }
}


I'm using the following code to Decrypt my encrypted file, read it, then encrypt it again.

private void Form1_Load(object sender, EventArgs e)
{

    if (File.Exists("scf.sf"))
    {
        DecryptFile("scf.sf", "sff.sf");
        File.Delete("scf.sf");
        File.Move("sff.sf", "scf.sf");

        TextReader encryptRead = new StreamReader("scf.sf");
        string encryptionCheck = encryptRead.ReadToEnd();
        encryptRead.Close();

        if (encryptionCheck.Contains("locked"))
        {
            MessageBox.Show("**yayaya***", "BlaBla", MessageBoxButtons.OK, MessageBoxIcon.Stop);

            EncryptFile("scf.sf", "ssf.sf");
            File.Delete("scf.sf");
            File.Move("ssf.sf", "scf.sf");

            Application.Exit();
        }
    }
    else
    {
        MessageBox.Show("Component Corrupt, Application Will Now End.", "Slyther Security Runtime", MessageBoxButtons.OK, MessageBoxIcon.Error);
        Application.Exit();
    }


    EncryptFile("slcf.sf", "slcfc.sf");
    File.Delete("slcf.sf");
    File.Move("slcfc.sf", "slcf.sf");

}


However, whenever I run it:

while ((data = cs.ReadByte()) != -1)

IndexOutOfRangeException was Unhandled.

Index was outside the bounds of the array.


This worked fine last night, and im not quite sure how i've tweaked the code to make it stop working. I'm going to continue looking into it while I wait for a reply.

Thanks,
Ben.

modified on Monday, September 21, 2009 12:41 PM

NewsRe: File Encryption Broken Pin
Ben Magee21-Sep-09 6:41
Ben Magee21-Sep-09 6:41 
QuestionPlease help me about working with Word - Very URGENT Pin
designervc21-Sep-09 6:18
designervc21-Sep-09 6:18 

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.