コード例 #1
0
        public void XTEAEcnryption(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 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
                        var readedValue    = File.ReadAllBytes(file.FullName);
                        var encryptedValue = XTEA.Encrypt(readedValue);
                        bw.Write(encryptedValue);

                        if (LoadedFilesController._END_OF_ENC_DEC_THREADS)
                        {
                            bw.Dispose();
                            fsw.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);
            }
        }
コード例 #2
0
        public void CreateUniqueFileName_InvalidData_Throw(string filePath)
        {
            // Arrange
            var fileNameCreator = new FileNameCreator();

            // Act
            fileNameCreator.CreateUniqueFileName(filePath);

            // Assert
        }
コード例 #3
0
        public void CreatePdfName()
        {
            Active.Document.SendStringToExecute("REGENALL ", true, false, true);
            Dictionary <string, string> attrList = new Dictionary <string, string>();
            Window1 window = new Window1(new BlockViewModel(attrList));

            Application.ShowModalWindow(window);
            if (window.isClicked)
            {
                //ObjectIdCollection objectIds = Utils.SelectDynamicBlockReferences();
                List <Sheet> dict = new List <Sheet>();

                using (Transaction tr = Active.Database.TransactionManager.StartTransaction())
                {
                    BlockSelector blockSelector = new BlockSelector();
                    blockSelector.GetFilterForSelectBlockId(window.BlockName);
                    bool isExec = true;
                    var  res    = blockSelector.SelectionResult;

                    if (res.Status != PromptStatus.OK)
                    {
                        isExec = false;
                        Active.Editor.WriteMessage("Надо выбрать блок");
                    }

                    if (isExec)
                    {
                        SelectionSet selSet      = res.Value;
                        ObjectId[]   idArrayTemp = selSet.GetObjectIds();
                        //ObjectIdCollection idArray = new ObjectIdCollection(idArrayTemp);
                        //TODO printing X Y
                        //string selAttrName = attributeName.AttributeName;


                        FileNameCreator fileNameCreator = new FileNameCreator(window, idArrayTemp);

                        //GetPrintParametersToPdf(Active.Editor, printModels, tr, objectIds, selAttrName);

                        fileNameCreator.GetPrintParametersToPdf(tr);

                        Utils utils = new Utils();
                        utils.CreateJsonFile(dict);

                        foreach (var printModel in fileNameCreator.GetPrintModels())
                        {
                            PlotCurrentLayout(printModel.DocNumber, printModel);
                        }
                    }

                    tr.Commit();
                }
            }
        }
コード例 #4
0
        public void CreateUniqueFileName_FileWithoutNumberExist_FileNameWithNumberOne()
        {
            // Arrange
            string fileNameWithoutNumber = "TestFileName.txt";

            string existingFileNamePath = Path.Combine(_path, fileNameWithoutNumber);

            File.Create(existingFileNamePath).Dispose();

            var fileNameCreator = new FileNameCreator();

            // Act
            string result = fileNameCreator.CreateUniqueFileName(existingFileNamePath);

            // Assert
            Assert.IsTrue(Path.GetFileNameWithoutExtension(result).EndsWith("1"));
        }
コード例 #5
0
        static void Main(string[] args)
        {
            Console.Write("Enter name of file: ");
            string name = Console.ReadLine();

            string[]          variants = new string[] { "txt", "jpeg", "docx", "rtf" };
            FileNameCreator[] creators = new FileNameCreator[] { new TxtFileNameCreator(name),
                                                                 new JpegFileNameCreator(name),
                                                                 new DocxFileNameCreator(name),
                                                                 new RtfFileNameCreator(name) };

            int selection = SelectCreator(variants);

            FileCreator.Create(creators[selection]);
            Console.Write("File was created successfully");

            Console.ReadLine();
        }
コード例 #6
0
        public void CreateUniqueFileName_TenFilesWithSameNumberExist_FileNameWithNumberEleven()
        {
            // Arrange
            string fileNameWithoutNumber = "TestFileName";

            string existingFileNamePath = Path.Combine(_path, fileNameWithoutNumber + ".txt");

            File.Create(existingFileNamePath).Dispose();
            for (int i = 1; i < 11; i++)
            {
                File.Create(Path.Combine(_path, fileNameWithoutNumber + i + ".txt")).Dispose();
            }

            var fileNameCreator = new FileNameCreator();

            // Act
            string result = fileNameCreator.CreateUniqueFileName(existingFileNamePath);

            // Assert
            Assert.IsTrue(Path.GetFileNameWithoutExtension(result).EndsWith("11"));
        }
コード例 #7
0
ファイル: Logging.cs プロジェクト: Iron2213/CSharp-Logger
        private static void QueueAndWrite(LogData JsonObj)
        {
            try {
                string strFileName;

                if (FileNameCreator == null)
                {
                    strFileName = FileNameTemplate;
                    strFileName = strFileName.Replace("yyyyMMdd", DateTime.Now.ToString("yyyyMMdd"));
                }
                else
                {
                    strFileName = FileNameCreator.Invoke();
                }

                WriterThread objWriter = GetWriter(strFileName);

                objWriter.LogQueue.Enqueue(JsonObj);

                //
                // Se il WriterThread non è avviato lo avvio, altrimenti continuo
                //
                if (!objWriter.Running)
                {
                    //
                    // Devo impostare qui il Running a TRUE altrimenti c'è il rischio che un richiesta aggiuntiva di scrittura su log arrivi
                    // prima che il thread parta causando un errore
                    //
                    objWriter.Running    = true;
                    objWriter.FileTarget = strFileName;
                    new Thread(new ThreadStart(objWriter.Write)).Start();
                }
            }
            catch (Exception ex) {
                throw new Exception("QueueAndWrite -> " + ex.Message);
            }
        }
コード例 #8
0
 public static void Create(FileNameCreator file)
 {
     File.Create(file.GetName());
 }
コード例 #9
0
        public void XTEABMPDecryption(FileInfo file, FormModel model)
        {
            bool threadSuccesfull = false;
            var  timeStarted      = DateTime.Now;

            try
            {
                if (!file.Extension.Contains("bmp"))
                {
                    throw new Exception("File is not bmp!");
                }
                //OutputFileName
                string outputFileName = "";

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

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

                using (FileStream fsw = new FileStream(outputFileName, FileMode.Create))
                {
                    using (BinaryWriter bw = new BinaryWriter(fsw, new ASCIIEncoding()))
                    {
                        //Reading and encrypting files
                        var readedValue = File.ReadAllBytes(file.FullName);

                        long   pos    = readedValue[10] + 256 * (readedValue[11] + 256 * (readedValue[12] + 256 * readedValue[13]));
                        byte[] header = new byte[pos];
                        for (int i = 0; i < header.Length; i++)
                        {
                            header[i] = readedValue[i];
                        }

                        byte[] data = readedValue.Skip(header.Length).ToArray();

                        var decryptedValue = XTEA.Decrypt(data);
                        decryptedValue = header.Concat(decryptedValue).ToArray();
                        bw.Write(decryptedValue);

                        if (LoadedFilesController._END_OF_ENC_DEC_THREADS)
                        {
                            bw.Dispose();
                            fsw.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);
            }
        }
コード例 #10
0
        public void XTEADecryption(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);

                //Reading the extension
                var readedValues    = File.ReadAllBytes(file.FullName);
                int lenght          = 0;
                var extensionLength = (int)readedValues[0];
                lenght++;
                char[] extension = new char[extensionLength];
                int    j         = 0;
                for (var i = 1; i <= extensionLength; i++)
                {
                    extension[j] = (char)readedValues[i];
                    j++;
                    lenght++;
                }
                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()))
                    {
                        //DEC
                        byte[] newValue = new byte[readedValues.Length - lenght];
                        int    k        = 0;
                        for (int i = lenght; i < readedValues.Length; i++)
                        {
                            newValue[k] = readedValues[i];
                            k++;
                        }
                        var decryptedValue = XTEA.Decrypt(newValue);
                        bw.Write(decryptedValue);

                        if (LoadedFilesController._END_OF_ENC_DEC_THREADS)
                        {
                            bw.Dispose();
                            fsw.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);
            }
        }
コード例 #11
0
        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);
            }
        }
コード例 #12
0
        public void SimpleSubstitutionDecryption(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 (StreamReader sr = new StreamReader(file.FullName))
                {
                    //Reading the extension
                    var    extensionLength = (int)sr.Read();
                    char[] extension       = new char[extensionLength];
                    for (var i = 0; i < extensionLength; i++)
                    {
                        extension[i] = (char)sr.Read();
                    }
                    var finalExtesnion = "." + new string(extension);

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

                    using (StreamWriter sw = new StreamWriter(outputFileName))
                    {
                        while (sr.Peek() >= 0)
                        {
                            char character = (char)sr.Read();
                            character = Char.ToUpper(character);
                            if (character < 65 || character > 90)
                            {
                                sw.Write(character);
                            }
                            else
                            {
                                sw.Write(SimpleSubstituionCipher.Decrypt(character));
                            }

                            if (LoadedFilesController._END_OF_ENC_DEC_THREADS)
                            {
                                sr.Dispose();
                                sw.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);
            }
        }
コード例 #13
0
        //
        //-----------------------------------------------------------------------------------------------------
        // Algorithms
        //-----------------------------------------------------------------------------------------------------
        //

        public void SimpleSubstitutionEncryption(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 (StreamReader sr = new StreamReader(file.FullName))
                {
                    using (StreamWriter sw = new StreamWriter(outputFileName))
                    {
                        //Writing the extension
                        char[] extension       = file.Extension.Substring(1, file.Extension.Length - 1).ToCharArray();
                        char   extensionLength = (char)extension.Length;
                        sw.Write(extensionLength);
                        for (var k = 0; k < extension.Length; k++)
                        {
                            sw.Write(extension[k]);
                        }

                        //Reading - encrypting - saving data
                        while (sr.Peek() >= 0)
                        {
                            char character = (char)sr.Read();
                            character = Char.ToLower(character);
                            if (character < 97 || character > 122)
                            {
                                sw.Write(character);
                            }
                            else
                            {
                                sw.Write(SimpleSubstituionCipher.Encrypt(character));
                            }

                            if (LoadedFilesController._END_OF_ENC_DEC_THREADS)
                            {
                                sr.Dispose();
                                sw.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);
            }
        }
コード例 #14
0
 public ZipFileModel(TreeNodeExtended treeNodeExtended, FileNameCreator fileNameCreator)
 {
     this.fileNameCreator          = fileNameCreator;
     this.treeViewZipFileStructure = new SerializableTreeNode();
     SetTreeViewZipFileStructureForFileWriting(treeNodeExtended);
 }