Esempio n. 1
0
        public static void DecryptFile(string infile, string outfile, string key, int rounds = 20, string encoding = "iso-8859-1")
        {
            if (!File.Exists(infile))
            {
                throw new ArgumentException("Input file does not exist.");
            }

            byte[] buff = new byte[buffsize];
            byte[] iv   = new byte[10];
            int    read;

            using (var input = File.OpenRead(infile))
            {
                if ((read = input.Read(iv, 0, 10)) != 10)
                {
                    throw new Exception("Invalid input file.");
                }

                using (var output = File.OpenWrite(outfile))
                {
                    List <byte> keybytes = new List <byte>();
                    keybytes.AddRange(Encoding.GetEncoding(encoding).GetBytes(key.Substring(0, Math.Min(key.Length, 246))));
                    keybytes.AddRange(iv);

                    var rc4 = new RC4(keybytes.ToArray(), rounds);
                    while ((read = input.Read(buff, 0, buffsize)) > 0)
                    {
                        output.Write(rc4.CryptBytes(buff, read), 0, read);
                    }
                }
            }
        }