Advertisement
Guest User

Untitled

a guest
Jul 14th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.65 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9.  
  10. namespace File_Encoder
  11. {
  12.     /// <summary>
  13.     /// Description of MainForm.
  14.     /// </summary>
  15.     public partial class MainForm : Form
  16.     {
  17.         public MainForm()
  18.         {
  19.             //
  20.             // The InitializeComponent() call is required for Windows Forms designer support.
  21.             //
  22.             InitializeComponent();
  23.            
  24.             //
  25.             // TODO: Add constructor code after the InitializeComponent() call.
  26.             //
  27.         }
  28.          public static string StrToHexStr(string str)
  29.         {
  30.             StringBuilder sb = new StringBuilder();
  31.             for (int i = 0; i < str.Length; i++)
  32.             {
  33.                 int v = Convert.ToInt32(str[i]);
  34.                 sb.Append(string.Format("{0:X2}", v));
  35.             }
  36.             return sb.ToString();
  37.         }
  38.  
  39.         public static string HexStrToStr(string hexStr)
  40.         {
  41.             StringBuilder sb = new StringBuilder();
  42.             for (int i = 0; i < hexStr.Length; i += 2)
  43.             {
  44.                 int n = Convert.ToInt32(hexStr.Substring(i, 2), 16);
  45.                 sb.Append(Convert.ToChar(n));
  46.             }
  47.             return sb.ToString();        
  48.         }
  49.        
  50.         void Button1Click(object sender, EventArgs e)
  51.         {
  52.             OpenFileDialog ofd = new OpenFileDialog();
  53.             if (ofd.ShowDialog() == DialogResult.OK) {
  54.                 textBox1.Text = ofd.FileName;
  55.                
  56.             }
  57.             else{
  58.                 MessageBox.Show("No file selected","Error",MessageBoxButtons.OK,MessageBoxIcon.Error    );
  59.             }
  60.                
  61.         }
  62.         void Button2Click(object sender, EventArgs e)
  63.         {
  64.     if (textBox1.Text == "") {
  65.     MessageBox.Show("No file selected","Error",MessageBoxButtons.OK,MessageBoxIcon.Error    );
  66.     }
  67.             else
  68.             {
  69.                
  70.  
  71.                 FileStream fs = new FileStream(textBox1.Text,
  72.                                    FileMode.Open,
  73.                                    FileAccess.Read);
  74.     byte[] filebytes = new byte[fs.Length];
  75.     fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
  76.     string encodedData =
  77.         Convert.ToBase64String(filebytes,                
  78.                                Base64FormattingOptions.InsertLineBreaks);
  79.     StreamWriter writer = new StreamWriter(textBox1.Text + ".crypted");
  80.    
  81.     writer.Write(StrToHexStr(encodedData));
  82.         writer.Close();
  83.         MessageBox.Show("File Crypted","OK",MessageBoxButtons.OK,MessageBoxIcon.Information);
  84.          
  85.             }
  86.         }
  87.         void Button4Click(object sender, EventArgs e)
  88.         {
  89.     OpenFileDialog ofd = new OpenFileDialog();
  90.             if (ofd.ShowDialog() == DialogResult.OK) {
  91.                 textBox2.Text = ofd.FileName;
  92.                
  93.             }
  94.             else{
  95.                 MessageBox.Show("No file selected","Error",MessageBoxButtons.OK,MessageBoxIcon.Error    );
  96.             }
  97.         }
  98.         void Button3Click(object sender, EventArgs e)
  99.         {
  100.     if (textBox2.Text == "") {
  101.     MessageBox.Show("No file selected","Error",MessageBoxButtons.OK,MessageBoxIcon.Error    );
  102.     }
  103.             else
  104.             {
  105.                 if (textBox2.Text.Contains(".crypted")) {
  106.                 File.WriteAllText(textBox2.Text,HexStrToStr(File.ReadAllText(textBox2.Text)));
  107.                 byte[] filebytes = Convert.FromBase64String(File.ReadAllText(textBox2.Text));
  108.                 FileStream fs = new FileStream(textBox2.Text.Replace(".crypted",""),
  109.                                    FileMode.CreateNew,
  110.                                    FileAccess.Write,
  111.                                    FileShare.None);
  112.     fs.Write(filebytes, 0, filebytes.Length);
  113.     fs.Close();
  114.         MessageBox.Show("File DeCrypted","OK",MessageBoxButtons.OK,MessageBoxIcon.Information);
  115.                 }
  116.                 else
  117.                 {
  118.                 MessageBox.Show("Please Select *.Crypted File","Error",MessageBoxButtons.OK,MessageBoxIcon.Error    );
  119.                 }
  120.             }
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement