End Google Ads 201810 - BS.net 01 --> 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