The algorithm gets the number associated with each char
, then adds a random number to it and saves the numbers it added. I plan to make an Encrypter
class later. Here is the entire project:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading;
using System.Text.RegularExpressions;
using WindowsInput;
using System.IO;
namespace pl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread tr;
string rr;
string r;
char[] cr;
int[] keys;
int[] more;
char[] c;
string filePath;
string pass;
string en = "";
public delegate void functionpointer(int print);
public delegate void functionpointerr();
private void payl()
{
en = pass;
Random ra = new Random();
this.keys = new int[en.Length];
int ac = 0;
c = en.ToCharArray();
for (int i = 0; i < en.Length; i++)
{
functionpointer b = new functionpointer(pk);
keys[i] = ra.Next(0, 30000);
Invoke(b, keys[i]);
int agfs = (int)c[i] + keys[i];
ac = agfs;
try
{
c[i] = Convert.ToChar(ac);
}
catch
{
MessageBox.Show("The text is way too long!"); // fix this this will keep iterating lmfao
richTextBox1.Text = "";
agfs = 0;
}
}
this.r = new string(c); // Convert the Encrypted Code into a string so we can display it in RichTextbox.
functionpointerr bb = new functionpointerr(lol);
Invoke(bb);
}
public void pk(int print)
{
richTextBox3.Text = richTextBox3.Text + " " + print;
}
public void lol()
{
richTextBox1.Text = this.r;
}
private void button1_Click(object sender, EventArgs e) // Encrypt Button
{
pass = richTextBox1.Text;
Thread t = new Thread(payl);
t.Start();
}
private void button2_Click(object sender, EventArgs e) // Decrypt Button
{
en = richTextBox1.Text;
c = richTextBox1.Text.ToCharArray();
string fix = richTextBox3.Text;
char[] SPACEchecker = fix.ToCharArray(); //BUY EGGS ON WEDNESDAY!
if (SPACEchecker[0] == ' ')
{
fix = fix.Remove(0, 1);
richTextBox3.Text = fix;
}
Decrypt();
}
private void Decrypt()
{
int ac = 0;
cr = new char[en.Length];
string[] RIA = Regex.Split(richTextBox3.Text.ToString(), " ");
int[] NKeys = new int[RIA.Length];
//converting keys
for (int cn = 0; cn < RIA.Length; cn++)
{
string it = RIA[cn].ToString();
NKeys[cn] = int.Parse(it);
}
for (int p = 0; p < en.Length; p++)
{
cr[p] = this.c[p]; // Make cr the same as c.
}
for (int b = 0; b < en.Length; b++)
{
int agfs = (int)this.c[b] - NKeys[b];
ac = agfs;
try
{
cr[b] = Convert.ToChar(ac);
}
catch
{
MessageBox.Show("The text is way too long!"); // fix later
richTextBox2.Text = "";
agfs = 0;
}
}
this.rr = new string(cr);
richTextBox2.Text = this.rr;
}
private bool even(int numm)
{
if (numm % 2 == 0)
{
return true;
}
else
{
return false;
}
}
private void button3_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog o = new OpenFileDialog();
DialogResult = o.ShowDialog();
filePath = o.FileName;
string text = File.ReadAllText(filePath);
pass = text;
tr = new Thread(payl);
tr.Start();
Thread ch = new Thread(check);
ch.Start();
}
catch (Exception ex)
{
MessageBox.Show("There was a problem \n technical details " + ex);
}
}
private void check()
{
while (tr.IsAlive)
{
Thread.Sleep(100);
}
File.Delete(filePath);
string encryptedtext = new string(c);
File.WriteAllText(filePath, encryptedtext);
}
private void button4_Click(object sender, EventArgs e)
{
this.rr = new string(cr);
string fix = richTextBox3.Text;
char[] SPACEchecker = fix.ToCharArray();
if (SPACEchecker[0] == ' ')
{
fix = fix.Remove(0, 1);
richTextBox3.Text = fix;
}
try
{
OpenFileDialog o = new OpenFileDialog();
DialogResult = o.ShowDialog();
filePath = o.FileName;
richTextBox1.Text = File.ReadAllText(filePath);
c = richTextBox1.Text.ToCharArray();
en = richTextBox1.Text;
Decrypt();
File.Delete(filePath);
File.WriteAllText(filePath, this.rr);
}
catch (Exception ex)
{
MessageBox.Show("There was a problem \n technical details " + ex);
}
}
}
}
Encrypter
class later - I would suggest doing organizational tasks like that before implementing anything at all. You're not benefitting by coding everything then having to reorganize later, because you have to update half your code to handle the new architecture. It's especially a good idea to do that before posting to CodeReview :) \$\endgroup\$Random
was usable for cryptography (it's not) - just the seed value, which would allow you to regenerate the sequence on-the-fly. 2) Normally when you do something like this, you restrict the random range to your input range (eg, all letters, numbers, etc), add and then apply mod (%
- or a couple of other options, and this usually operates on the byte level, too). For one thing, it prevents single-possibility results; if your current program outputs0
, there's only one possible input. \$\endgroup\$