Click here to Skip to main content
15,895,709 members
Home / Discussions / C#
   

C#

 
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 
QuestionSocket programming problem Pin
JLP18821-Sep-09 6:07
JLP18821-Sep-09 6:07 
QuestionRecovering delivery Errors sending SMTP Mails Pin
FJJCENTU21-Sep-09 4:14
FJJCENTU21-Sep-09 4:14 
QuestionCreate a delegate at run time Pin
bonzaiholding21-Sep-09 2:50
bonzaiholding21-Sep-09 2:50 
AnswerRe: Create a delegate at run time Pin
Not Active21-Sep-09 4:04
mentorNot Active21-Sep-09 4:04 
QuestionCreating Registry Keys with Setup if key doesn't exist. Pin
Paramhans Dubey21-Sep-09 2:50
professionalParamhans Dubey21-Sep-09 2:50 
AnswerRe: Creating Registry Keys with Setup if key doesn't exist. Pin
Manas Bhardwaj21-Sep-09 3:24
professionalManas Bhardwaj21-Sep-09 3:24 
GeneralRe: Creating Registry Keys with Setup if key doesn't exist. Pin
Paramhans Dubey21-Sep-09 19:06
professionalParamhans Dubey21-Sep-09 19:06 
QuestionReportViewer control problem Pin
firefeet21-Sep-09 1:23
firefeet21-Sep-09 1:23 
AnswerRe: ReportViewer control problem Pin
Abhishek Sur21-Sep-09 11:42
professionalAbhishek Sur21-Sep-09 11:42 
GeneralRe: ReportViewer control problem Pin
firefeet21-Sep-09 18:16
firefeet21-Sep-09 18:16 
QuestionHow to delete outlook appointment Pin
tspradeep198021-Sep-09 1:06
tspradeep198021-Sep-09 1:06 
Questionsplitting values randomly - C# Pin
flower_t21-Sep-09 1:05
flower_t21-Sep-09 1:05 
AnswerRe: splitting values randomly - C# [modified] Pin
Calla21-Sep-09 1:29
Calla21-Sep-09 1:29 
AnswerRe: splitting values randomly - C# Pin
OriginalGriff21-Sep-09 2:46
mveOriginalGriff21-Sep-09 2:46 
AnswerRe: splitting values randomly - C# Pin
flower_t21-Sep-09 2:56
flower_t21-Sep-09 2:56 

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.