-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
197 lines (172 loc) · 6.74 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace Convert_ot_Base64
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Stream streamRead = null;
OpenFileDialog openFile = new OpenFileDialog();
openFile.InitialDirectory = Environment.CurrentDirectory;
openFile.Filter = "All files (*.*)|*.*";
if (openFile.ShowDialog() == DialogResult.OK)
{
try
{
if ((streamRead = openFile.OpenFile()) != null)
{
using (streamRead)
{
ConvertToBase64 converter = new ConvertToBase64();
textBox4.Text=converter.Encode(File.ReadAllBytes(openFile.FileName));
streamRead.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
ConvertToBase64 converter = new ConvertToBase64();
textBox2.Text = converter.EncodeString(textBox1.Text);
textBox3.Text = converter.Decode(textBox2.Text).ToString();
}
private void button3_Click(object sender, EventArgs e)
{
SaveFileDialog saveFile = new SaveFileDialog();
ConvertToBase64 converter = new ConvertToBase64();
saveFile.Filter = "base64 files (*.b64)|*.b64";
saveFile.Title = "Save encoded file";
saveFile.DefaultExt = ".b64";
saveFile.ShowDialog();
if (saveFile.FileName != "")
{
using (FileStream fstream = new FileStream(saveFile.FileName, FileMode.OpenOrCreate)) //@"C:\SomeDir\noname\note.txt"
{
fstream.Write(Encoding.ASCII.GetBytes(textBox4.Text), 0, Encoding.ASCII.GetBytes(textBox4.Text).Length);
}
}
}
private void button4_Click(object sender, EventArgs e) //save source file
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "All files (*.*)|*.*";
saveFile.RestoreDirectory = true;
saveFile.ShowDialog();
if (saveFile.FileName != "")
{
using (FileStream fstream = new FileStream(saveFile.FileName, FileMode.OpenOrCreate)) //@"C:\SomeDir\noname\note.txt"
{
ConvertToBase64 converter = new ConvertToBase64();
fstream.Write(converter.Decode(textBox4.Text), 0, converter.Decode(textBox4.Text).Length);
}
}
}
private void button5_Click(object sender, EventArgs e) //open b64 file
{
Stream streamRead = null;
OpenFileDialog openFile = new OpenFileDialog();
openFile.InitialDirectory = Environment.CurrentDirectory;
openFile.Filter = "Base64 files (*.b64)|*.b64";
if (openFile.ShowDialog() == DialogResult.OK)
{
try
{
if ((streamRead = openFile.OpenFile()) != null)
{
using (streamRead)
{
byte[] arr;
ConvertToBase64 converter = new ConvertToBase64();
arr = File.ReadAllBytes(openFile.FileName);
Encoding enc8 = Encoding.UTF8;
textBox4.Text = enc8.GetString(arr);
streamRead.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
};
}
}
}
public class ConvertToBase64
{
private string base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
public ConvertToBase64() { }
public string Encode(byte[] content)
{
Encoding enc8 = Encoding.UTF8;
string arr = enc8.GetString(content);
int c = arr.Length % 3;
string r = "", p = "";
if (c > 0)
{
for (; c < 3; c++)
{
p += "=";
arr += "\0";
}
}
for ( c = 0; c < arr.Length; c += 3)
{
int n = (arr[c] << 16) + (arr[c + 1] << 8) + (arr[c+2]);
int n1 = (n >> 18) & 63, n2 = (n >> 12) & 63, n3 = (n >> 6) & 63, n4 = n & 63;
r += "" + base64[n1] + base64[n2] + base64[n3] + base64[n4];
}
return r.Substring(0, r.Length - p.Length) + p;
}
public string EncodeString(string s)
{
string r = "", p = "";
int c = s.Length % 3;
if (c>0)
{
for (; c < 3; c++)
{
p += "=";
s += "\0";
}
}
for (c = 0; c < s.Length; c+=3)
{
int n = (s[c] << 16) + (s[c + 1]<<8) + (s[c + 2]);
int n1 = (n >> 18) & 63, n2 = (n >> 12) & 63, n3 = (n >> 6) & 63, n4 = n & 63;
r += "" + base64[n1] + base64[n2] + base64[n3] + base64[n4];
}
return r.Substring(0,r.Length-p.Length)+p;
}
public byte[] Decode(string s)
{
byte[] array;
s = s.Replace("[^" + base64 + "=]", "");
String p = (s[s.Length - 1] == '=' ?
(s[s.Length - 2] == '=' ? "AA" : "A") : "");
String r = "";
s = s.Substring(0, s.Length - p.Length) + p;
for (int c = 0; c < s.Length; c += 4)
{
int n = (base64.IndexOf(s[c]) << 18)
+ (base64.IndexOf(s[c+1]) << 12)
+ (base64.IndexOf(s[c+2]) << 6)
+ base64.IndexOf(s[c+3]);
r += "" + (char)((n >> 16) & 0xFF) + (char)((n >> 8) & 0xFF)
+ (char)(n & 0xFF);
}
return array = Encoding.ASCII.GetBytes(r);
}
}
}