Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
- namespace File_Encoder
- {
- /// <summary>
- /// Description of MainForm.
- /// </summary>
- public partial class MainForm : Form
- {
- public MainForm()
- {
- //
- // The InitializeComponent() call is required for Windows Forms designer support.
- //
- InitializeComponent();
- //
- // TODO: Add constructor code after the InitializeComponent() call.
- //
- }
- public static string StrToHexStr(string str)
- {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < str.Length; i++)
- {
- int v = Convert.ToInt32(str[i]);
- sb.Append(string.Format("{0:X2}", v));
- }
- return sb.ToString();
- }
- public static string HexStrToStr(string hexStr)
- {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < hexStr.Length; i += 2)
- {
- int n = Convert.ToInt32(hexStr.Substring(i, 2), 16);
- sb.Append(Convert.ToChar(n));
- }
- return sb.ToString();
- }
- void Button1Click(object sender, EventArgs e)
- {
- OpenFileDialog ofd = new OpenFileDialog();
- if (ofd.ShowDialog() == DialogResult.OK) {
- textBox1.Text = ofd.FileName;
- }
- else{
- MessageBox.Show("No file selected","Error",MessageBoxButtons.OK,MessageBoxIcon.Error );
- }
- }
- void Button2Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "") {
- MessageBox.Show("No file selected","Error",MessageBoxButtons.OK,MessageBoxIcon.Error );
- }
- else
- {
- FileStream fs = new FileStream(textBox1.Text,
- FileMode.Open,
- FileAccess.Read);
- byte[] filebytes = new byte[fs.Length];
- fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
- string encodedData =
- Convert.ToBase64String(filebytes,
- Base64FormattingOptions.InsertLineBreaks);
- StreamWriter writer = new StreamWriter(textBox1.Text + ".crypted");
- writer.Write(StrToHexStr(encodedData));
- writer.Close();
- MessageBox.Show("File Crypted","OK",MessageBoxButtons.OK,MessageBoxIcon.Information);
- }
- }
- void Button4Click(object sender, EventArgs e)
- {
- OpenFileDialog ofd = new OpenFileDialog();
- if (ofd.ShowDialog() == DialogResult.OK) {
- textBox2.Text = ofd.FileName;
- }
- else{
- MessageBox.Show("No file selected","Error",MessageBoxButtons.OK,MessageBoxIcon.Error );
- }
- }
- void Button3Click(object sender, EventArgs e)
- {
- if (textBox2.Text == "") {
- MessageBox.Show("No file selected","Error",MessageBoxButtons.OK,MessageBoxIcon.Error );
- }
- else
- {
- if (textBox2.Text.Contains(".crypted")) {
- File.WriteAllText(textBox2.Text,HexStrToStr(File.ReadAllText(textBox2.Text)));
- byte[] filebytes = Convert.FromBase64String(File.ReadAllText(textBox2.Text));
- FileStream fs = new FileStream(textBox2.Text.Replace(".crypted",""),
- FileMode.CreateNew,
- FileAccess.Write,
- FileShare.None);
- fs.Write(filebytes, 0, filebytes.Length);
- fs.Close();
- MessageBox.Show("File DeCrypted","OK",MessageBoxButtons.OK,MessageBoxIcon.Information);
- }
- else
- {
- MessageBox.Show("Please Select *.Crypted File","Error",MessageBoxButtons.OK,MessageBoxIcon.Error );
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement