private void genB_Click(object sender, EventArgs e) { if (hashTB.Text == "") { MessageBox.Show("Задайте пароль!"); return; } if (generator == null) { generator = new CongruentialGenerator(uint.Parse(hashTB.Text)); } try { ulong res = 0; if (minValueTB.Text != "") { if (maxValueTB.Text == "") { MessageBox.Show("Задайте максимальное значение"); return; } res = generator.Next(ulong.Parse(minValueTB.Text), ulong.Parse(maxValueTB.Text)); } else { res = maxValueTB.Text != "" ? generator.Next(ulong.Parse(maxValueTB.Text)) : generator.Next(); } randomTB.Text = res + ""; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void genB_Click(object sender, EventArgs e) { if (hashTB.Text == "") { MessageBox.Show("Задайте пароль!"); return; } if (generator == null) generator = new CongruentialGenerator(uint.Parse(hashTB.Text)); try { ulong res = 0; if (minValueTB.Text != "") { if (maxValueTB.Text == "") { MessageBox.Show("Задайте максимальное значение"); return; } res = generator.Next(ulong.Parse(minValueTB.Text), ulong.Parse(maxValueTB.Text)); } else { res = maxValueTB.Text != "" ? generator.Next(ulong.Parse(maxValueTB.Text)) : generator.Next(); } randomTB.Text = res + ""; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
protected override void GenKeys(string key) { var gen = new CongruentialGenerator(MaHash8v64.GetHashCode(key)); keys = new byte[this.SubBlocks]; for (var i = 0; i < keys.Length - 1; i++) keys[i] = (byte)gen.Next(1, this.BlockLenth - keys.Sum(a => a) - (this.SubBlocks - i - 1)); keys[keys.Length - 1] = (byte)(this.BlockLenth - keys.Sum(a => a)); }
public ECB(byte BlockLenth, byte subBlocks, string pass) : base(BlockLenth, subBlocks, pass) { inds = new int[this.SubBlocks]; keysDecrypt = new byte[this.SubBlocks]; for (int i = 0; i < this.SubBlocks; i++) inds[i] = -1; var gen = new CongruentialGenerator(MaHash8v64.GetHashCode(this.Password)); for (int i = 0; i < this.SubBlocks - 1; i++) { byte newValue; do { newValue = (byte)gen.Next(0, this.SubBlocks - 1); } while (inds[newValue] != -1 || (newValue == 0 && i == 0)); inds[newValue] = i; } for (int i = 0; i < this.SubBlocks; i++) { if (inds[i] == -1) inds[i] = this.SubBlocks - 1; keysDecrypt[i] = keys[inds[i]]; } }
public CongruentialGeneratorDialogView(CongruentialGenerator congruentialGenerator) { InitializeComponent(); DataContext = new CongruetialGeneratorDialogViewModel(congruentialGenerator, this); }
/// <summary> /// Общая функция преобразования входного массива байтов /// </summary> /// <param name="file">входной массив байтов</param> /// <param name="reverse">если false - шифрация, true - дешифрация</param> /// <param name="key">ключ</param> private void Crypt(string inputFile, string outputFile, string key) { var file = File.ReadAllBytes(inputFile); var gen = new CongruentialGenerator(MaHash8v64.GetHashCode(key)); var keys = new byte[Rounds]; for (var i = 0; i < keys.Length; i++) keys[i] = (byte)gen.Next(byte.MaxValue); this.MaxValueProcess = (int)(file.LongLength * Rounds); byte[] l = new byte[BlockLenth / 2]; byte[] r = new byte[BlockLenth / 2]; var lenthBlocks = Math.Truncate((double)(file.LongLength / BlockLenth)); for (byte k = 0; k < Rounds; k++) { for (var i = 0; i < lenthBlocks; i++) { l = file.Skip(i * BlockLenth).Take(BlockLenth / 2).ToArray(); r = file.Skip(i * BlockLenth + BlockLenth / 2).Take(BlockLenth / 2).ToArray(); if (i < Rounds - 1) // если не последний раунд { byte[] t = l; l = XOR(F(l, keys[k]), r); r = t; } else // последний раунд { r = XOR(F(l, keys[k]), r); } for (int j = 0; j < BlockLenth / 2; j++) { file[i * BlockLenth + j] = l[j]; file[i * BlockLenth + j + BlockLenth / 2] = r[j]; } this.CurrentValueProcess = i + k * Rounds; } } File.WriteAllBytes(outputFile, file); }