예제 #1
0
        static void Main(string[] args)
        {
            string keyString = null;

            while (string.IsNullOrWhiteSpace(keyString))
            {
                Console.WriteLine("Enter key word:");
                keyString = Console.ReadLine();
            }

            var key         = Encoding.UTF8.GetBytes(keyString);
            var fileName    = args.Single();
            var fileContent = new LazyFileReader(fileName);
            var prefix      = fileContent.Take(2).ToArray();
            var cryptAsBmp  = false;

            if (prefix[0] == 0x42 && prefix[1] == 0x4D)
            {
                cryptAsBmp = IsCryptAsBmp();
            }

            IEnumerable <byte> result;
            var rc4 = new RC4lAgorithm();

            if (cryptAsBmp)
            {
                var byteOffset = fileContent.Skip(10).Take(4).ToArray();
                var intOffset  = BitConverter.ToInt32(byteOffset, 0);
                result = fileContent
                         .Take(intOffset)
                         .Concat(rc4.Crypt(
                                     fileContent.Skip(intOffset), key));
            }
            else
            {
                result = rc4.Crypt(fileContent, key);
            }

            var fileWriter = new LazyFileWriter(GenerateFileName(fileName));

            fileWriter.Write(result);
        }
예제 #2
0
        static void Main(string[] args)
        {
            var fileName           = args.Single();
            var mode               = ChooseMode();
            var fileContent        = new LazyFileReader(fileName);
            var strategy           = new PCBCCryptingStrategy <Iso10126PaddingStandard>(k => new TEACrypter(k));
            var key                = File.ReadAllBytes("key");
            var initializingVector = File.ReadAllBytes("initializingVector");

            if (mode == Mode.Encrypt)
            {
                var encryptedContent = strategy.Encrypt(fileContent, key, initializingVector);
                var fileWriter       = new LazyFileWriter(GenerateFileName(fileName, "encrypted"));
                fileWriter.Write(encryptedContent);
            }

            if (mode == Mode.Decrypt)
            {
                var decrypt    = strategy.Decrypt(fileContent, key, initializingVector);
                var fileWriter = new LazyFileWriter(GenerateFileName(fileName, "decrypted"));
                fileWriter.Write(decrypt);
            }
        }
예제 #3
0
        static void Main(string[] args)
        {
            var fileName    = args.Single();
            var mode        = ChooseMode();
            var fileContent = new LazyFileReader(fileName);

            if (mode == Mode.Encrypt)
            {
                var alphabet         = GetAlphabet();
                var encryptedContent = new CaesarEncrypter(alphabet).Encrypt(fileContent);
                var fileWriter       = new LazyFileWriter(GenerateFileName(fileName, "encrypted"));
                fileWriter.Write(encryptedContent);
            }

            if (mode == Mode.Decrypt)
            {
                var alphabet   = GetAlphabet();
                var decrypt    = new CaesarDecrypter(alphabet).Decrypt(fileContent);
                var fileWriter = new LazyFileWriter(GenerateFileName(fileName, "decrypted"));
                fileWriter.Write(decrypt);
            }

            if (mode == Mode.Analyze)
            {
                Console.WriteLine("Analyzing...");
                var byteFrequencies = new ByteFrequencyAnalyzer().Analyze(fileContent).ToArray();
                var index           = 0;
                Console.WriteLine("The most common bytes:");
                foreach (var batch in byteFrequencies.Batch(10))
                {
                    foreach (var byteFrequency in batch)
                    {
                        Console.WriteLine($"{++index}. {byteFrequency}");
                    }

                    bool skip;
                    while (true)
                    {
                        Console.WriteLine("Continue? (y/n)");
                        var input = Console.ReadLine();
                        if (input == "Y" || input == "y")
                        {
                            skip = false;
                            break;
                        }

                        if (input == "N" || input == "n")
                        {
                            skip = true;
                            break;
                        }
                    }
                    if (skip)
                    {
                        break;
                    }
                }

                var sb = new StringBuilder();
                sb.AppendLine("Байт,количество");
                foreach (var frequency in byteFrequencies)
                {
                    sb.AppendLine($"{frequency.Value},{frequency.Frequency}");
                }
                File.WriteAllText(GenerateFileName(fileName, "analyzed"), sb.ToString());
            }
        }