Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- namespace Bez3
- {
- public partial class Form1 : Form
- {
- public Int64 rnd, s, b, a, c; //ранд
- public Int64 m = (int)byte.MaxValue + 1;
- public Form1()
- {
- InitializeComponent();
- gener();//генерируем новые значения
- }
- //генерация нового случ
- public Int64 rand(Int64 c)
- {
- return (a * c + b) % m;
- }
- //Криптографическая часть
- public byte[] ShifMASH(byte[] s, Int64[] k)
- {//создаем выходной массив
- byte[] res = new byte[s.Length];
- //зашифровываем
- for (int i = 0; i < s.Length; i++)
- {
- byte b = Convert.ToByte(k[i + 1]);
- b ^= s[i];
- res[i] = b;
- }
- return res;
- }
- //смена параметров
- private void button1_Click(object sender, EventArgs e)
- {
- gener();
- }
- //generator параметров
- public void gener()
- {
- rnd = System.DateTime.Now.Millisecond % System.DateTime.Now.Ticks;
- b = System.DateTime.Now.Ticks % (System.DateTime.Now.Millisecond + 340);
- a = System.DateTime.Now.Ticks % System.DateTime.Now.Minute * System.DateTime.Now.Millisecond;
- if (b < 0)//проверка b
- {
- b = 12;
- }
- if (a < 0)//проверка а
- {
- a = 21;
- }
- if (b % 2 == 0)
- {
- b++;
- }
- while (a % 4 != 1)
- {
- a--;
- if (a < 0)
- {
- a = 5;
- }
- }
- while (a >= m)
- {
- a = a / 10;
- }
- while(b >= m)
- {
- b = b / 10;
- }
- while (rnd >= m)
- {
- rnd = rnd / 10;
- }
- //вывод
- textBox3.Text = "a= " + a.ToString() + "\r\n" + "b= " + b.ToString() + "\r\n" + "rnd= " + rnd.ToString();
- }
- //сохранить параметры в заданные файл
- private void button2_Click(object sender, EventArgs e)
- {
- if (TextFileN.TextLength != 0)
- {
- FileInfo file = new FileInfo("C:\\Users\\Семён\\Desktop\\Work4DATA\\" + TextFileN.Text + ".key");
- //если файл сздан то вопрос на пересоздание
- if (file.Exists)
- {
- DialogResult result = MessageBox.Show("Файл с таким именем уже существует", "Заменить?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
- if (result == DialogResult.No)
- {
- return;
- }
- }
- //записываем
- StreamWriter sw = new StreamWriter(File.Open("C:\\Users\\Семён\\Desktop\\Work4DATA\\" + TextFileN.Text + ".key", FileMode.Create));
- sw.WriteLine(a.ToString());
- sw.WriteLine(b.ToString());
- sw.WriteLine(rnd.ToString());
- sw.Close();
- }
- else
- {
- MessageBox.Show("Пусто тут");
- return;
- }
- }
- //считываем параметры из файла
- private void button3_Click(object sender, EventArgs e)
- {
- if (TextFileM.TextLength != 0)
- {
- FileInfo file = new FileInfo("C:\\Users\\Семён\\Desktop\\Work4DATA\\" + TextFileM.Text + ".key");
- if (file.Exists == false)
- {
- MessageBox.Show(TextFileM.Text + " не существует!");
- return;
- }
- StreamReader sr = new StreamReader(file.ToString());
- a = Int32.Parse(sr.ReadLine());
- b = Int32.Parse(sr.ReadLine());
- rnd = Int32.Parse(sr.ReadLine());
- textBox3.Text = "a= " + a.ToString() + "\r\n" + "b= " + b.ToString() + "\r\n" + "rnd= " + rnd.ToString();
- sr.Close();
- }
- else
- {
- MessageBox.Show("Пусто тут");
- return;
- }
- }
- //(За/Рас)шифровать
- private void button4_Click(object sender, EventArgs e)
- {//открыть файл исходный
- FileInfo file_in = new FileInfo("C:\\Users\\Семён\\Desktop\\Work4DATA\\" + textFileIN.Text + ".txt");
- if (file_in.Exists == false)
- {
- MessageBox.Show(textFileIN.Text + " не существует!");
- return;
- }
- //создаем дискриптор
- BinaryReader br = new BinaryReader(file_in.OpenRead());
- byte[] TIN = new byte[file_in.Length];
- //textBox9.Text = file.Length.ToString() + " Байт";
- //считываем все из файла в b
- TIN = br.ReadBytes(TIN.Length);
- br.Close();
- //открыть файл результат
- FileInfo file_out = new FileInfo("C:\\Users\\Семён\\Desktop\\Work4DATA\\" + textFileOUT.Text + ".txt");
- if (file_out.Exists)
- {
- DialogResult result = MessageBox.Show("Файл с таким именем уже существует", "Заменить?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
- if (result == DialogResult.No)
- {
- return;
- }
- }
- BinaryWriter bw = new BinaryWriter(File.Open("C:\\Users\\Семён\\Desktop\\Work4DATA\\" + textFileOUT.Text + ".txt", FileMode.Create));
- //созд массив длинной в кол байт
- Int64[] c = new Int64[TIN.Length + 1];
- //сохр первое число
- c[0] = rnd;
- //заполняем массив случ числами
- for (int i = 1; i <= TIN.Length; i++)
- {
- c[i] = rand(c[i - 1]);
- }
- //заводим флаг времени
- DateTime time1 = DateTime.Now;
- byte[] fout = ShifMASH(TIN, c);
- //флаг результата
- DateTime time2 = DateTime.Now;
- bw.Write(fout);
- textBox1.Text = (time2 - time1).Milliseconds.ToString() + " мс";
- bw.Close();
- }
- //поменяет местами
- private void button5_Click(object sender, EventArgs e)
- {
- string s = textFileIN.Text;
- textFileIN.Text = textFileOUT.Text;
- textFileOUT.Text = s;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement