Advertisement
benshepherd

Encryption algorithm

Apr 8th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Text.RegularExpressions;
  9. using System.IO;
  10. using System.Threading;
  11.  
  12. namespace BENcrypt
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public string charArray;
  17.         public long KeyInt;
  18.         public string input;
  19.         public string output;
  20.         XMLclass XmlClass;
  21.  
  22.         public Form1()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         private void Form1_Load(object sender, EventArgs e)
  28.         {
  29.             setCharArray();
  30.             XmlClass = new XMLclass();
  31.             XmlClass.XMLpath = Environment.SpecialFolder.ApplicationData + "\\BENcrypt\\config.xml";
  32.  
  33.  
  34.             if (File.Exists(XmlClass.XMLpath))
  35.             {
  36.                 string[] config = XmlClass.ConfigData;
  37.  
  38.                 //input, ouput, key, log
  39.                 txtIn.Text = config[0];
  40.                 txtOut.Text = config[1];
  41.                 txtKey.Text = config[2];
  42.                 txtLog.Text = config[3];
  43.             }
  44.  
  45.         }
  46.  
  47.         private void btnEn_Click(object sender, EventArgs e)
  48.         {
  49.             log("-------------------------\r\nEncryption event started");
  50.             while (true)
  51.             {
  52.                 if (txtKey.Text.Length == 0)
  53.                 {
  54.                     log("Error: Key cannot be empty");
  55.                     break;
  56.                 }
  57.                 else if (txtIn.Text.Length == 0)
  58.                 {
  59.                     log("Error: Input cannot be empty");
  60.                     break;
  61.                 }
  62.                 string result = encrypt(txtIn.Text, txtKey.Text);
  63.                 log("Encryption finished.");
  64.                 txtOut.Text = result;
  65.                 break;
  66.             }
  67.         }
  68.  
  69.         private void btnDe_Click(object sender, EventArgs e)
  70.         {
  71.             while (true)
  72.             {
  73.                 //string pattern = "/^[(0-9]+[:.]+$/";
  74.                 //string str = txtIn.Text;
  75.  
  76.                 /*if (!Regex.Match(str, pattern).Success)
  77.                 {
  78.                     log("-------------------------\r\nCannot decrypt - Unrecognized format\r\nMust be like:\r\nxx:xxx:x:xxxxx:xxx\r\ne.t.c.");
  79.                     break;
  80.                 }*/
  81.                 log("-------------------------\r\nDecryption event started");
  82.                 while (true)
  83.                 {
  84.                     if (txtKey.Text.Length == 0)
  85.                     {
  86.                         log("Error: Key cannot be empty");
  87.                         break;
  88.                     }
  89.                     else if (txtIn.Text.Length == 0)
  90.                     {
  91.                         log("Error: Input cannot be empty");
  92.                         break;
  93.                     }
  94.                     string result = decrypt(txtIn.Text, txtKey.Text);
  95.                     txtOut.Text = result;
  96.                     break;
  97.                 }
  98.  
  99.                 break;
  100.             }
  101.         }
  102.  
  103.         private long generateKey(string x)
  104.         {
  105.             log("Generating numeric key  (" + x + ")");
  106.             int[] y = new int[x.Length];
  107.             string z = "";
  108.             for (int i = 0; i < x.Length; i++)
  109.             {
  110.                 int bz = z.Length;
  111.                 y[i] = charArray.IndexOf(x[i]);
  112.                 z += y[i].ToString();
  113.             }
  114.  
  115.             long kfinal = 0;
  116.             try
  117.             {
  118.                 kfinal = Convert.ToInt32(z) / z.Length / z.Length;
  119.             }
  120.             catch (Exception ex)
  121.             {
  122.                 kfinal = 0;
  123.                 MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  124.             }
  125.  
  126.             log("Key generated");
  127.             return kfinal;
  128.         }
  129.  
  130.         private void setCharArray()
  131.         {
  132.             charArray = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 !\"£$%^&*()_-+=[]{};:'@#~,<.>/?`¬|\\/";
  133.         }
  134.  
  135.         private string decrypt(string str, string key)
  136.         {
  137.             log("Starting Decryption process...");
  138.             KeyInt = generateKey(key);
  139.             string output = "";
  140.  
  141.             if (str[str.Length - 1] != ':')
  142.             {
  143.                 str = str + ':'.ToString();
  144.             }
  145.  
  146.             // Get number sections, range of numbers before ':'
  147.  
  148.             int erl = 0;
  149.  
  150.             while (true)
  151.             {
  152.                 string[] secRange = new string[str.Length];
  153.                 int seci = 0;
  154.  
  155.                 for (int i = 0; i < str.Length; i++)
  156.                 {
  157.                     if (str[i] != ':')
  158.                     {
  159.                         secRange[seci] += str[i];
  160.                     }
  161.                     else
  162.                     {
  163.                         seci++;
  164.                     }
  165.                 }
  166.  
  167.                 char[] dchars = new char[str.Length];
  168.  
  169.                 for (int i = 0; i < seci; i++)
  170.                 {
  171.                     int sr = 0;
  172.                     try
  173.                     {
  174.                         sr = Convert.ToInt32(secRange[i]);
  175.                     }
  176.                     catch
  177.                     {
  178.                         log("Could not decrypt string");
  179.                         erl++;
  180.                         break;
  181.                     }
  182.  
  183.                     int conv = 0;
  184.                     try
  185.                     {
  186.                         conv = sr / Convert.ToInt32(KeyInt);
  187.                     }
  188.                     catch
  189.                     {
  190.                         log("Could not decrypt string");
  191.                         erl++;
  192.                         break;
  193.                     }
  194.  
  195.                     try
  196.                     {
  197.                         output += charArray[conv];
  198.                     }
  199.                     catch
  200.                     {
  201.                         log("Could not decrypt string");
  202.                         erl++;
  203.                         break;
  204.                     }
  205.  
  206.                     log("'" + sr.ToString() + "' => '" + charArray[conv] + "'");
  207.                 }
  208.  
  209.                 if(erl==0)
  210.                     log("Decrypted");
  211.  
  212.                 break;
  213.             }
  214.  
  215.             return output;
  216.         }
  217.  
  218.         private string encrypt(string str, string key)
  219.         {
  220.             log("Starting Encryption process...");
  221.             KeyInt = generateKey(key);
  222.             string output = "";
  223.  
  224.             while (true)
  225.             {
  226.                 if (str.Contains("\r\n"))
  227.                 {
  228.                     str = str.Replace(Environment.NewLine,"");
  229.                 }
  230.  
  231.                 for (int i = 0; i < str.Length; i++)
  232.                 {
  233.                     int bl = output.Length;
  234.  
  235.                     output += (charArray.IndexOf(str[i]) * KeyInt).ToString() + ":";
  236.  
  237.                     log("'" + str[i] + "' => '" + (charArray.IndexOf(str[i]) * KeyInt).ToString() + "'");
  238.                 }
  239.                 break;
  240.             }
  241.             return output.TrimEnd(':');
  242.         }
  243.  
  244.         private void log(string str)
  245.         {
  246.             txtLog.AppendText(str + "\r\n");
  247.         }
  248.  
  249.         private void linkClear_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  250.         {
  251.             txtLog.Clear();
  252.         }
  253.  
  254.         private void linkSave_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  255.         {
  256.             if (txtLog.Text.Length > 0)
  257.             {
  258.                 SaveFileDialog sfd = new SaveFileDialog();
  259.                 sfd.DefaultExt = "txt";
  260.                 sfd.Filter = "Text File (*.txt)|*.txt";
  261.                 sfd.ShowDialog();
  262.                 if (sfd.FileName.Length > 0)
  263.                 {
  264.                     System.IO.StreamWriter sw = new System.IO.StreamWriter(sfd.FileName);
  265.                     sw.Write(txtLog.Text);
  266.                     sw.Close();
  267.                 }
  268.             }
  269.         }
  270.  
  271.         private void linkPaste_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  272.         {
  273.             if (Clipboard.ContainsText())
  274.             {
  275.                 txtIn.Text = Clipboard.GetText();
  276.                 log("Pasted text");
  277.                 if (Clipboard.GetText().Contains(Environment.NewLine))
  278.                 {
  279.                     log("Note: Line breaks will be removed");
  280.                 }
  281.             }
  282.             else
  283.             {
  284.                 log("Clipboard data empty or not in text format");
  285.             }
  286.         }
  287.  
  288.         private void linkCopy_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  289.         {
  290.             if (txtOut.Text.Length > 0)
  291.             {
  292.                 Clipboard.SetText(txtOut.Text);
  293.                 log("Copied text");
  294.             }
  295.             else
  296.             {
  297.                 log("Nothing to copy");
  298.             }
  299.         }
  300.  
  301.         private void linkClearIn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  302.         {
  303.             txtIn.Clear();
  304.         }
  305.  
  306.         private void linkClearOut_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  307.         {
  308.             txtOut.Clear();
  309.         }
  310.  
  311.         private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  312.         {
  313.             string a = txtIn.Text;
  314.             string b = txtOut.Text;
  315.  
  316.             txtIn.Text = b;
  317.             txtOut.Text = a;
  318.         }
  319.  
  320.         private void newToolStripMenuItem_Click(object sender, EventArgs e)
  321.         {
  322.             //New
  323.             foreach(Control c in this.Controls)
  324.             {
  325.                 if (c is TextBox)
  326.                 {
  327.                     ((TextBox)c).Clear();
  328.                 }
  329.             }
  330.         }
  331.  
  332.         private void loadLogToolStripMenuItem_Click(object sender, EventArgs e)
  333.         {
  334.             txtLog.Text = loadFile();
  335.         }
  336.  
  337.         private void loadOutput_Click(object sender, EventArgs e)
  338.         {
  339.             txtOut.Text = loadFile();
  340.         }
  341.  
  342.         private string loadFile()
  343.         {
  344.             string result_str = String.Empty;
  345.  
  346.             OpenFileDialog x = new OpenFileDialog();
  347.             StreamReader y;
  348.  
  349.             while (true)
  350.             {
  351.                 x.ShowDialog();
  352.  
  353.                 if (x.FileName.Length == 0)
  354.                     break;
  355.                 else
  356.                     y = new StreamReader(x.FileName);
  357.  
  358.                 if (File.Exists(x.FileName))
  359.                     result_str = y.ReadToEnd();
  360.                 else
  361.                     MessageBox.Show("Error loading file. File doesn't exist!", "Error");
  362.  
  363.                 y.Close();
  364.  
  365.                 break;
  366.             }
  367.  
  368.             return result_str;
  369.         }
  370.  
  371.         private bool saveFile(string x)
  372.         {
  373.             SaveFileDialog y = new SaveFileDialog();
  374.             y.DefaultExt = "Text file (*.txt)|*.txt|Any file type (*.*)|*.*";
  375.  
  376.             StreamWriter w;
  377.  
  378.             while (true)
  379.             {
  380.                 y.ShowDialog();
  381.  
  382.                if (y.FileName.Length != 0)
  383.                 {
  384.                     try
  385.                     {
  386.                         w = new StreamWriter(y.FileName);
  387.                         w.Write(x);
  388.                         w.Close();
  389.                     }
  390.                     catch
  391.                     {
  392.                         return false;
  393.                     }
  394.                     return true;
  395.                 }
  396.                 else
  397.                 {
  398.                     return true;
  399.                 }
  400.             }
  401.         }
  402.  
  403.         private void saveLogToolStripMenuItem_Click(object sender, EventArgs e)
  404.         {
  405.             bool save = saveFile(txtLog.Text);
  406.  
  407.             if (!save)
  408.                 MessageBox.Show("Error saving file", "Error");
  409.             else
  410.                 MessageBox.Show("Saved successfully", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
  411.         }
  412.  
  413.         private void saveOut_Click(object sender, EventArgs e)
  414.         {
  415.             bool save = saveFile(txtOut.Text);
  416.  
  417.             if (!save)
  418.                 MessageBox.Show("Error saving file", "Error");
  419.             else
  420.                 MessageBox.Show("Saved successfully", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
  421.         }
  422.  
  423.         private void exitToolStrip_Click(object sender, EventArgs e)
  424.         {
  425.             Application.Exit();
  426.         }
  427.  
  428.         private void openHelpToolStripMenuItem_Click(object sender, EventArgs e)
  429.         {
  430.  
  431.         }
  432.  
  433.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  434.         {
  435.             XmlClass.saveConfig(txtIn.Text, txtOut.Text, txtKey.Text, txtLog.Text);
  436.         }
  437.     }
  438. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement