Пример #1
0
 public RC4(string key)
 {
     this.Key = RC4.KSA(RC4.StringToByteArray(key));
 }
        public void RC4Decryption(FileInfo file, FormModel model)
        {
            bool threadSuccesfull = false;
            var  timeStarted      = DateTime.Now;

            try
            {
                //OutputFileName
                string outputFileName = "";

                //Log
                loggerController.Add(" ! File dec: " + file.Name + ", Alg: " + model.AlgorithmName);

                //Read a file char by char, and decrypt it
                using (FileStream fsr = new FileStream(file.FullName, FileMode.Open))
                {
                    using (BinaryReader br = new BinaryReader(fsr, new ASCIIEncoding()))
                    {
                        //Reading the extension
                        var    extensionLength = (int)br.ReadByte();
                        char[] extension       = new char[extensionLength];
                        for (var i = 0; i < extensionLength; i++)
                        {
                            extension[i] = (char)br.ReadByte();
                        }
                        var finalExtesnion = "." + new string(extension);

                        //OutputFileName
                        outputFileName = FileNameCreator.CreateFileDecryptedName(
                            model.Folders.OutputFolder,
                            file.Name,
                            finalExtesnion);

                        using (FileStream fsw = new FileStream(outputFileName, FileMode.Create))
                        {
                            using (BinaryWriter bw = new BinaryWriter(fsw, new ASCIIEncoding()))
                            {
                                int    i           = 0;
                                int    j           = 0;
                                byte[] state       = RC4.KSA();
                                byte   readedValue = 0;
                                while (br.BaseStream.Position < br.BaseStream.Length)
                                {
                                    //DEC
                                    readedValue = br.ReadByte();
                                    byte prga           = RC4.PRGA(ref i, ref j, ref state);
                                    byte decryptedValue = RC4.Decrypt(readedValue, prga);
                                    bw.Write(decryptedValue);

                                    if (LoadedFilesController._END_OF_ENC_DEC_THREADS)
                                    {
                                        bw.Dispose();
                                        fsw.Dispose();
                                        br.Dispose();
                                        fsr.Dispose();
                                        File.Delete(outputFileName);
                                        Thread.CurrentThread.Abort();
                                    }
                                }
                            }
                        }
                    }
                }
                threadSuccesfull = true;
                Thread.Sleep(250);
            }
            catch (Exception ex)
            {
                loggerController.Add(" ? Dec exception: " + ex.Message);
                threadSuccesfull = false;
            }
            finally
            {
                this.ThreadEnds(file, threadSuccesfull, timeStarted);
            }
        }
Пример #3
0
 public RC4(byte[] key)
 {
     this.Key = RC4.KSA(key);
 }
        public void RC4Encryption(FileInfo file, FormModel model)
        {
            bool threadSuccesfull = false;
            var  timeStarted      = DateTime.Now;

            try
            {
                //OutputFileName
                string outputFileName = FileNameCreator.CreateFileEncryptedName(
                    model.Folders.OutputFolder,
                    file.Name,
                    model.AlgorithmName);

                //Log
                loggerController.Add(" ! File enc: " + file.Name + ", Alg: " + model.AlgorithmName);

                //Read a file char by char, and encrypt it
                using (FileStream fsr = new FileStream(file.FullName, FileMode.Open))
                {
                    using (BinaryReader br = new BinaryReader(fsr, new ASCIIEncoding()))
                    {
                        using (FileStream fsw = new FileStream(outputFileName, FileMode.Create))
                        {
                            using (BinaryWriter bw = new BinaryWriter(fsw, new ASCIIEncoding()))
                            {
                                //Writing the extension
                                char[] extension       = file.Extension.Substring(1, file.Extension.Length - 1).ToCharArray();
                                char   extensionLength = (char)extension.Length;
                                bw.Write(extensionLength);
                                for (var k = 0; k < extension.Length; k++)
                                {
                                    bw.Write(extension[k]);
                                }

                                //Reading and encrypting files
                                int    i          = 0;
                                int    j          = 0;
                                byte[] state      = RC4.KSA();
                                byte   inputValue = 0;
                                while (br.BaseStream.Position < br.BaseStream.Length)
                                {
                                    //ENC
                                    inputValue = br.ReadByte();
                                    byte prga           = RC4.PRGA(ref i, ref j, ref state);
                                    byte encryptedValue = RC4.Encrypt(inputValue, prga);
                                    bw.Write(encryptedValue);

                                    if (LoadedFilesController._END_OF_ENC_DEC_THREADS)
                                    {
                                        bw.Dispose();
                                        fsw.Dispose();
                                        br.Dispose();
                                        fsr.Dispose();
                                        File.Delete(outputFileName);
                                        Thread.CurrentThread.Abort();
                                    }
                                }
                            }
                        }
                    }
                }
                threadSuccesfull = true;
                Thread.Sleep(250);
            }
            catch (Exception ex)
            {
                loggerController.Add(" ? Enc exception: " + ex.Message);
                threadSuccesfull = false;
            }
            finally
            {
                this.ThreadEnds(file, threadSuccesfull, timeStarted);
            }
        }