//method to execute from outside public void GenerateLabirynth(int _majorDimension, int _minorDimension, float _minorSize) { //checking if randomNumbersGenerator exist if (randomNumbersGenerator == null) { GameObject rndObject = GameObject.FindGameObjectWithTag("RandomNumbersGenerator"); if (rndObject != null) { randomNumbersGenerator = rndObject.GetComponent <RandomNumbersGenerator>(); if (randomNumbersGenerator == null) { Debug.LogWarning("there is no RandomNumbersGenerator attached to found object"); return; } } else { Debug.LogWarning("can find RandomNumbersGenerator instance"); return; } } //setting up properties majorDimension = _majorDimension; minorDimension = _minorDimension; minorSize = _minorSize; cursor = new IntVector2(1, 1); //starting thread generator = new Thread(Generating); generator.Start(); }
public JsonResult GetNumbers2(int x, int y) { RandomNumbersGenerator numberGenerator = new RandomNumbersGenerator(); var table = numberGenerator.ReturnNumbers(x, y); return(Json(table, JsonRequestBehavior.AllowGet)); }
public void Source_should_return_values_within_range(int minValue, int maxValue) { using (var generator = new RandomNumbersGenerator()) { foreach (var value in generator.Source(minValue, maxValue).Take(1000)) { value.Should().BeGreaterOrEqualTo(minValue).And.BeLessOrEqualTo(maxValue); } } }
public JsonResult GetNumbers4(int x1, int y1, int x2, int y2) { RandomNumbersGenerator numberGenerator1 = new RandomNumbersGenerator(); RandomNumbersGenerator numberGenerator2 = new RandomNumbersGenerator(); var table1 = numberGenerator1.ReturnNumbers(x1, y1); var table2 = numberGenerator2.ReturnNumbers(x2, y2); return(Json(new { tab1 = table1, tab2 = table2 }, JsonRequestBehavior.AllowGet)); }
public void TrueOrFalse_should_work_like_Head_Or_Tails() { using (var generator = new RandomNumbersGenerator()) { const int number = 10000; var expectation = number / 2; uint expectedDelta = number / 50; // delta = 2% foreach (var valueCounter in Enumerable.Range(0, number) .Select(_ => generator.TrueOrFalse()) .GroupBy(x => x) .Select(x => x.Count())) { valueCounter.Should().BeCloseTo(expectation, expectedDelta); } } }
public MinorGrid(IntVector2 _position, int _minorDimension, LabirynthCell.TYPE _type, RandomNumbersGenerator _randomNumbersGenerator, int _minorRepeatChance) { //setting up properties position = _position; minorDimension = _minorDimension; type = _type; randomNumbersGenerator = _randomNumbersGenerator; minorRepeatChance = _minorRepeatChance; minorGrid = new LabirynthCell[minorDimension, minorDimension]; cursor = new IntVector2(1, 1); //filling grid with empty cells (for objects that aren`t generated - majorWalls) for (int y = 0; y < minorDimension; y++) { for (int x = 0; x < minorDimension; x++) { minorGrid[x, y] = new LabirynthCell(new IntVector2(x, y), LabirynthCell.TYPE.EMPTY); } } }
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; } }
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; } }
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; } }