Example #1
0
        private String ResolveImports(String Path, List <String> FilesLoaded = null)
        {
            Path = Path.Replace('\\', '/');
            if (FilesLoaded == null)
            {
                FilesLoaded = new List <String>();
            }
            else if (FilesLoaded.Contains(Path))
            {
                return("");
            }
            FilesLoaded.Add(Path);

            var source = LoadSourceFile(Path);

            if (source.Item1 == false)
            {
                Core.LogError(Path + " - " + source.Item2);
                return("");
            }

            var output = new StringBuilder();
            var stream = new System.IO.StringReader(source.Item2);

            while (true)
            {
                var line = stream.ReadLine();
                if (line == null)
                {
                    break;
                }

                if (line.StartsWith("//import "))
                {
                    var importedFilename = line.Substring("//import ".Length).Trim();
                    output.Append(ResolveImports(importedFilename, FilesLoaded));
                    output.AppendLine();
                }
                else
                {
                    output.AppendLine(line);
                }
            }

            return(output.ToString());
        }
Example #2
0
        private async void WorkAsync()
        {
            IsNoActionRunning = false;

            try
            {
                #region Checks

                //Check whether file id is int

                if (!int.TryParse(ChosenFileId, out int FileId))
                {
                    IsCompleted = false;
                    StateText   = "Wrong File id!";
                    return;
                }

                //Check whether file id is correct

                if ((FileId >= FilesLoaded.Count()) || (FileId < 0))
                {
                    IsCompleted = false;
                    StateText   = "Wrong File id!";
                    return;
                }

                if (string.IsNullOrEmpty(ChosenAction))
                {
                    return;
                }

                string FilePath = string.Empty;

                //Load file path

                foreach (var item in FilesLoaded)
                {
                    if (item.Id == FileId)
                    {
                        FilePath = item.FileRealName;
                        break;
                    }
                }

                //if file id points to empty record

                if (string.IsNullOrWhiteSpace(FilePath))
                {
                    IsCompleted = false;
                    StateText   = "File id is wrong.";
                    return;
                }

                // if file was deleted after loading

                if (!File.Exists(FilePath))
                {
                    IsCompleted = false;
                    StateText   = "File doesnt exist.";
                    return;
                }

                #endregion

                #region Rabin
                //Rabin
                if (ChosenCipher == CiphersList[0])
                {
                    BigInteger p = 0;
                    BigInteger q = 0;

                    //Encrypt
                    if (ChosenAction == ActionsList[0])
                    {
                        //Check everything strictly because we are waiting data from user
                        // and not generating it
                        if (!IsKeysAutoGenerating)
                        {
                            #region data parsing

                            if (!BigInteger.TryParse(SecretKeyParam1, out p))
                            {
                                StateText   = "Wrong first secret key parameter .";
                                IsCompleted = false;
                                return;
                            }

                            if (!BigInteger.TryParse(SecretKeyParam2, out q))
                            {
                                StateText   = "Wrong second secret key parameter .";
                                IsCompleted = false;
                                return;
                            }

                            if ((!p.CheckIfPrime()) || (!q.CheckIfPrime()))
                            {
                                StateText   = "One of two numbers was not prime";
                                IsCompleted = false;
                                return;
                            }

                            if ((p % 4 != 3) || (q % 4 != 3))
                            {
                                StateText = "p and q mod 4 must be equal to 3.";
                                return;
                            }

                            if (p == q)
                            {
                                StateText   = "Secret key values must differ.";
                                IsCompleted = false;
                                return;
                            }

                            #endregion
                        }


                        //if user've chosen to generate keys automatically, generate them now.
                        if (IsKeysAutoGenerating)
                        {
                            #region Key generation

                            p = RandomNumbersGenerator.GenerateBlumPrime(defaultKeyLength);
                            q = RandomNumbersGenerator.GenerateBlumPrime(defaultKeyLength);

                            if (p < 0)
                            {
                                p = -p;
                            }
                            if (q < 0)
                            {
                                q = -q;
                            }

                            CredentialsText = "p : " + p.ToString()
                                              + Environment.NewLine + "q: " + q.ToString()
                                              + Environment.NewLine + "Public key is: " +
                                              (p * q).ToString();

                            #endregion
                        }


                        bool result = false;
                        result = RabinInstance.Initialize(p, q);

                        if (!result)
                        {
                            StateText   = "Check key and try again.";
                            IsCompleted = false;
                            return;
                        }

                        await Task.Run(() => result = RabinInstance.Encrypt(p, q, FilePath));

                        if (!result)
                        {
                            StateText   = "Check key and try again.";
                            IsCompleted = false;
                            return;
                        }
                    }
                    //Decrypt
                    else
                    {
                        #region data parsing

                        if (!BigInteger.TryParse(SecretKeyParam1, out p))
                        {
                            StateText   = "Wrong first secret key parameter .";
                            IsCompleted = false;
                            return;
                        }

                        if (!BigInteger.TryParse(SecretKeyParam2, out q))
                        {
                            StateText   = "Wrong second secret key parameter .";
                            IsCompleted = false;
                            return;
                        }

                        if ((p % 4 != 3) || (q % 4 != 3))
                        {
                            StateText = "p and q mod 4 must be equal to 3.";
                            return;
                        }

                        if (p == q)
                        {
                            StateText   = "Secret key values must differ.";
                            IsCompleted = false;
                            return;
                        }

                        bool result = false;
                        result = RabinInstance.Initialize(p, q);

                        if (!result)
                        {
                            StateText   = "Check key and try again.";
                            IsCompleted = false;
                            return;
                        }

                        #endregion

                        result = false;
                        await Task.Run(() => result = RabinInstance.Decrypt(p, q, FilePath));

                        if (!result)
                        {
                            StateText   = "Check data you are decrypting and try again.";
                            IsCompleted = false;
                            return;
                        }
                    }
                }
                #endregion

                #region Yarmolik - Rabin
                //Yarmolik - Rabin
                else if (ChosenCipher == CiphersList[1])
                {
                    //Encrypt
                    if (ChosenAction == ActionsList[0])
                    {
                        if (!short.TryParse(PublicKeyParam1, out var publicKey))
                        {
                            #region Checks

                            // if public key was not provided, try parse private keys
                            if (!short.TryParse(SecretKeyParam1, out var p))
                            {
                                StateText   = "Wrong first secret param.";
                                IsCompleted = false;
                                return;
                            }

                            if (!short.TryParse(SecretKeyParam2, out var q))
                            {
                                StateText   = "Wrong second secret param.";
                                IsCompleted = false;
                                return;
                            }

                            if ((!p.CheckIfPrime()) || (!q.CheckIfPrime()))
                            {
                                StateText   = "One of two numbers was not prime";
                                IsCompleted = false;
                                return;
                            }

                            if ((p % 4 != 3) || (q % 4 != 3))
                            {
                                StateText   = "p and q mod 4 must be equal to 3.";
                                IsCompleted = false;
                                return;
                            }

                            if (p * q < Math.Pow(2, 8))
                            {
                                StateText   = "p * q must be more than 1 byte.";
                                IsCompleted = false;
                                return;
                            }

                            if (!short.TryParse(PublicKeyParam2, out var caesarCoeff))
                            {
                                StateText   = "Wrong caesar coefficient.";
                                IsCompleted = false;
                                return;
                            }

                            if (caesarCoeff >= p * q)
                            {
                                StateText   = "Caesar coefficient must be less than public key.";
                                IsCompleted = false;
                                return;
                            }

                            if (p == q)
                            {
                                StateText   = "Secret key values must differ.";
                                IsCompleted = false;
                                return;
                            }

                            #endregion

                            YarmolikRabinInstance.Encrypt(p, q, caesarCoeff, FilePath);
                        }
                        else
                        { // if public key was provided
                            #region Checks

                            if (!short.TryParse(PublicKeyParam2, out var caesarCoeff))
                            {
                                StateText   = "Wrong caesar coefficient.";
                                IsCompleted = false;
                                return;
                            }

                            if (caesarCoeff >= publicKey)
                            {
                                StateText   = "Caesar coefficient must be less than public key.";
                                IsCompleted = false;
                                return;
                            }

                            if ((IsUnicodeSupported) && (publicKey < russianMaxUnicodeIndx))
                            {
                                StateText   = $"To support unicode symbols, provide a public key larger than {russianMaxUnicodeIndx}";
                                IsCompleted = false;
                                return;
                            }

                            if (publicKey < Math.Pow(2, 8))
                            {
                                StateText   = $"Public key should be more than 1 byte";
                                IsCompleted = false;
                                return;
                            }

                            #endregion

                            YarmolikRabinInstance.Encrypt(publicKey, caesarCoeff, FilePath);
                        }
                    }
                    //Decrypt
                    else
                    {
                        #region Checks

                        if (!short.TryParse(SecretKeyParam1, out var p))
                        {
                            StateText   = "Wrong first secret param.";
                            IsCompleted = false;
                            return;
                        }

                        if (!short.TryParse(SecretKeyParam2, out var q))
                        {
                            StateText   = "Wrong second secret param.";
                            IsCompleted = false;
                            return;
                        }

                        if ((!p.CheckIfPrime()) || (!q.CheckIfPrime()))
                        {
                            StateText   = "One of two numbers was not prime";
                            IsCompleted = false;
                            return;
                        }

                        if ((p % 4 != 3) || (q % 4 != 3))
                        {
                            StateText   = "p and q mod 4 must be equal to 3.";
                            IsCompleted = false;
                            return;
                        }

                        if (p * q < Math.Pow(2, 8))
                        {
                            StateText   = "p * q must be more than 1 byte.";
                            IsCompleted = false;
                            return;
                        }

                        if (!short.TryParse(PublicKeyParam2, out var caesarCoeff))
                        {
                            StateText   = "Wrong caesar coefficient.";
                            IsCompleted = false;
                            return;
                        }

                        if (caesarCoeff >= p * q)
                        {
                            StateText   = "Caesar coefficient must be less than public key.";
                            IsCompleted = false;
                            return;
                        }

                        if (p == q)
                        {
                            StateText   = "Secret key values must differ.";
                            IsCompleted = false;
                            return;
                        }


                        #endregion

                        await Task.Run(() => YarmolikRabinInstance.Decrypt(p, q, caesarCoeff, FilePath));
                    }
                }
                #endregion

                StateText = "Action completed.";

                IsCompleted = true;
            }
            catch (ArgumentException ex)
            {
                StateText   = ex.Message;
                IsCompleted = false;
            }
            catch (OutOfMemoryException)
            {
                StateText   = "Memory limit was exceeded.";
                IsCompleted = false;
            }
            finally
            { IsNoActionRunning = true; }
        }
Example #3
0
 protected virtual void OnFilesLoaded(FilesLoadedResult e)
 {
     FilesLoaded?.Invoke(this, e);
 }
Example #4
0
        private async void WorkAsync()
        {
            IsNoActionRunning = false;

            try
            {
                #region Checks

                //Check whether file id is int

                if (!int.TryParse(ChosenFileId, out int FileId))
                {
                    IsCompleted = false;
                    StateText   = "Wrong File id!";
                    return;
                }

                //Check whether file id is correct

                if ((FileId >= FilesLoaded.Count()) || (FileId < 0))
                {
                    IsCompleted = false;
                    StateText   = "Wrong File id!";
                    return;
                }


                string FilePath = string.Empty;

                //Load file path

                foreach (var item in FilesLoaded)
                {
                    if (item.Id == FileId)
                    {
                        FilePath = item.FileRealName;
                        break;
                    }
                }

                //if file id points to empty record

                if (string.IsNullOrWhiteSpace(FilePath))
                {
                    IsCompleted = false;
                    StateText   = "File id is wrong.";
                    return;
                }

                // if file was deleted after loading

                if (!File.Exists(FilePath))
                {
                    IsCompleted = false;
                    StateText   = "File doesnt exist.";
                    return;
                }

                #endregion

                Enum.TryParse <KeyGenerators>(ChosenKeyGenerator, out var chosenKeyGenerationMethod);

                var keyGeneratorInstance = new KeyGeneratorsFactory().NewKeyGenerator(chosenKeyGenerationMethod);

                var cipher = new StreamCipher();

                cipher.Initialize(keyGeneratorInstance);

                if (IsAutoGeneratingKeys)
                {
                    var generatedKey = RandomNumbersGenerator.GenerateBinaryString(LFSR.DefaultHighestPower);
                    GeneratedKey = $"Your generated key is : {generatedKey}";
                    await cipher.Encrypt(FilePath, generatedKey, progress);
                }
                else
                {
                    await cipher.Encrypt(FilePath, InitialState, progress);
                }


                StateText = "Action completed.";

                IsCompleted = true;
            }
            catch (ArgumentException ex)
            {
                StateText   = ex.Message;
                IsCompleted = false;
            }
            catch (OutOfMemoryException ex)
            {
                StateText   = ex.Message;
                IsCompleted = false;
            }
            finally
            { IsNoActionRunning = true; }
        }
Example #5
0
        private void Work()
        {
            IsNoActionRunning = false;

            try
            {
                #region Checks for chosen file

                //Check whether file id is int

                if (!int.TryParse(ChosenFileId, out int FileId))
                {
                    IsCompleted = false;
                    StateText   = "Wrong File id!";
                    return;
                }

                //Check whether file id is correct

                if ((FileId >= FilesLoaded.Count()) || (FileId < 0))
                {
                    IsCompleted = false;
                    StateText   = "Wrong File id!";
                    return;
                }

                if (string.IsNullOrEmpty(ChosenAction))
                {
                    return;
                }

                string FilePath = string.Empty;

                //Load file path

                foreach (var item in FilesLoaded)
                {
                    if (item.Id == FileId)
                    {
                        FilePath = item.FileRealName;
                        break;
                    }
                }

                //if file id points to empty record

                if (string.IsNullOrWhiteSpace(FilePath))
                {
                    IsCompleted = false;
                    StateText   = "File id is wrong.";
                    return;
                }

                // if file was deleted after loading

                if (!File.Exists(FilePath))
                {
                    IsCompleted = false;
                    StateText   = "File doesnt exist.";
                    return;
                }

                if (Path.GetExtension(FilePath) != ".txt")
                {
                    IsCompleted = false;
                    StateText   = "Text files should be used only...";
                    return;
                }

                #endregion

                var hashFuncChosen = HashFunctionsDict[ChosenSignature];

                BigInteger p = 0, q = 0, secretKey = 0;

                if (!IsKeysAutoGenerating)
                {
                    #region Checks for user input

                    if (!BigInteger.TryParse(PublicKeyParam1, out p))
                    {
                        IsCompleted = false;
                        StateText   = "Wrong p value";
                        return;
                    }


                    if (!BigInteger.TryParse(PublicKeyParam2, out q))
                    {
                        IsCompleted = false;
                        StateText   = "Wrong q value";
                        return;
                    }

                    if (!BigInteger.TryParse(SecretKey, out secretKey))
                    {
                        IsCompleted = false;
                        StateText   = "Wrong secret key value";
                        return;
                    }

                    #endregion
                }
                else
                { //if secret key couldn't meet the requerements here, should regenerate the numbers.
                    p           = RandomNumbersGenerator.GeneratePrime(15 * 8);
                    OutputText += $"P : {p.ToString()} ";
                    q           = RandomNumbersGenerator.GeneratePrime(15 * 8);
                    OutputText += Environment.NewLine + $"Q : {q.ToString()} ";
                    secretKey   = RandomNumbersGenerator.GeneratePrime(15 * 8);
                    OutputText += Environment.NewLine + $"Secret Key : {secretKey.ToString()} ";
                }


                var signatureInstance = new RsaSignature(p, q, secretKey, hashFuncChosen, IsCorrectnessChecksEnabled);

                if (ChosenAction == "Sign a message")
                {
                    var result = signatureInstance.Sign(FilePath);
                    OutputText += Environment.NewLine + $"Result of signing Is: {result.Item1.ToString()}"
                                  + Environment.NewLine + $"Hash in decimal is :{result.Item2.ToString()}"
                                  + Environment.NewLine + $"Hash in hex is :{result.Item2.ToString("X")}";
                }

                if (ChosenAction == "Signature check")
                {
                    if (signatureInstance.CheckSignature(FilePath))
                    {
                        OutputText = "Digital signature is right!";
                    }
                    else
                    {
                        OutputText = "Digital signature is corrupted!";
                    }
                }

                StateText = "Action completed.";

                IsCompleted = true;
            }
            catch (ArgumentException ex)
            {
                StateText   = ex.Message;
                IsCompleted = false;
            }
            catch (OutOfMemoryException)
            {
                StateText   = "Memory limit was exceeded.";
                IsCompleted = false;
            }
            catch (IOException IoExc)
            {
                StateText   = IoExc.Message;
                IsCompleted = false;
            }
            finally
            { IsNoActionRunning = true; }
        }
Example #6
0
        private void Work()
        {
            IsNoActionRunning = false;

            try
            {
                #region Checks

                //Check whether file id is int

                if (!int.TryParse(ChosenFileId, out int FileId))
                {
                    IsCompleted = false;
                    StateText   = "Wrong File id!";
                    return;
                }

                //Check whether file id is correct

                if ((FileId >= FilesLoaded.Count()) || (FileId < 0))
                {
                    IsCompleted = false;
                    StateText   = "Wrong File id!";
                    return;
                }

                if (string.IsNullOrEmpty(ChosenAction))
                {
                    return;
                }

                string FilePath = string.Empty;

                //Load file path

                foreach (var item in FilesLoaded)
                {
                    if (item.Id == FileId)
                    {
                        FilePath = item.FileRealName;
                        break;
                    }
                }

                //if file id points to empty record

                if (string.IsNullOrWhiteSpace(FilePath))
                {
                    IsCompleted = false;
                    StateText   = "File id is wrong.";
                    return;
                }

                // if file was deleted after loading

                if (!File.Exists(FilePath))
                {
                    IsCompleted = false;
                    StateText   = "File doesnt exist.";
                    return;
                }

                #endregion

                var AlphabetInCiphers = string.Empty;

                //Get chosen Alphabet

                if (ChosenAlphabet == AlphabetNamesList[0])
                {
                    AlphabetInCiphers = ResourceStrings.EngCapital;
                }
                else
                if (ChosenAlphabet == AlphabetNamesList[1])
                {
                    AlphabetInCiphers = ResourceStrings.RuCapital;
                }

                var ChosenCipherType = new SimpleCiphers();

                //Find Chosen Algorithm

                for (int i = 0; i < CiphersList.Count(); ++i)
                {
                    if (CiphersList[i] == ChosenCipher)
                    {
                        ChosenCipherType = (SimpleCiphers)i;
                    }
                }

                var Algorithm = new SimpleCipheringAlgorithmFactory().NewAlgorithm(ChosenCipherType);

                Algorithm.Initialize(KeyValue, AlphabetInCiphers);

                var chosenActionType = new Operations();

                for (int i = 0; i < ActionsList.Count(); ++i)
                {
                    if (ActionsList[i] == ChosenAction)
                    {
                        chosenActionType = (Operations)i;
                    }
                }

                var TextToWorkWith = File.ReadAllText(FilePath).ToUpper();

                TextToWorkWith = GetFixedString(ref TextToWorkWith, AlphabetInCiphers, chosenActionType);

                //Encrypt
                if (chosenActionType == Operations.Encrypt)
                {
                    ReturnedText = Algorithm.Encrypt(TextToWorkWith);
                    File.WriteAllText(FilePath.Insert(FilePath.IndexOf("."), "Encrypted"), ReturnedText);
                    StateText = "Successfully created text file " + Path.GetFileName(FilePath);
                }
                else//Decipher
                {
                    ReturnedText = Algorithm.Decipher(TextToWorkWith);
                    File.WriteAllText(FilePath.Insert(FilePath.IndexOf("."), "Decrypted"), ReturnedText);
                    StateText = "Successfully created text file " + Path.GetFileName(FilePath);
                }

                IsCompleted = true;
            }
            catch (ArgumentException ex)
            {
                StateText   = ex.Message;
                IsCompleted = false;
            }

            finally
            { IsNoActionRunning = true; }
        }