首页
社区
课程
招聘
[求助] 【C#】救救孩子吧 有大佬帮我找找bug吗?
2021-6-12 22:34 4192

[求助] 【C#】救救孩子吧 有大佬帮我找找bug吗?

2021-6-12 22:34
4192

先上源码(这是次要的,可以直接略过看下面的问题)

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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
 
 
namespace Hcryptor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            notifyIcon1.ShowBalloonTip(999, "Welcome to use Hcryptor", "Written by henry217",ToolTipIcon.None);
 
        }
 
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox1.Checked)
            {
                checkBox2.Checked = false;
            }
        }
 
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox2.Checked)
            {
                checkBox1.Checked = false;
            }
        }
        /**************************functions*****************************/
        public static byte[] AESEncrypt(byte[] Data, string Key, string Vector)
        {
            byte[] bKey = new byte[32];//采用32位密码加密
            Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);//如果用户输入的密码不足32位,自动填充空格至32
            byte[] bVector = new byte[16];//密钥向量,为16
            Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);//如果用户定义的密钥向量不足16位,自动填充空格至16
            byte[] Cryptograph = null;//加密后的密文
            Rijndael Aes = Rijndael.Create();
            try
            {
                using (MemoryStream Memory = new MemoryStream())
                {
                    //把内存流对象包装成加密流对象
                    using (CryptoStream Encryptor = new CryptoStream(Memory, Aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write))
                    {
                        Encryptor.Write(Data, 0, Data.Length);
                        Encryptor.FlushFinalBlock();
                        Cryptograph = Memory.ToArray();
                    }
                }
            }
            catch
            {
                Cryptograph = null;
            }
            return Cryptograph;
        }
        public static byte[] AESDecrypt(byte[] Data, string Key, string Vector)
        {
            byte[] bKey = new byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
            byte[] bVector = new byte[16];
            Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
            byte[] original = null;//解密后的明文
            Rijndael Aes = Rijndael.Create();
            try
            {
                using (MemoryStream Memory = new MemoryStream(Data))
                {
                    //把内存流对象包装成加密对象
                    using (CryptoStream Decryptor = new CryptoStream(Memory, Aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read))
                    {
                        //明文存储区
                        using (MemoryStream originalMemory = new MemoryStream())
                        {
                            byte[] Buffer = new byte[1024];
                            int readBytes = 0;
                            while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
                            {
                                originalMemory.Write(Buffer, 0, readBytes);
                            }
                            original = originalMemory.ToArray();
                        }
                    }
                }
            }
            catch
            {
                original = null;
            }
            return original;
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            foreach (string path in openFileDialog1.FileNames)
            {
                if(File.Exists(path))
                {
                    richTextBox1.Text = richTextBox1.Text+ path + "\n" ;
                }
            }
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            backgroundWorker1.CancelAsync();
            if(checkBox1.Checked==false&&checkBox2.Checked==false)
            {
                notifyIcon1.ShowBalloonTip(999, "Warning", "You should select what to do!", ToolTipIcon.Warning);
                return;
            }
            if(richTextBox1.Text=="")
            {
                notifyIcon1.ShowBalloonTip(999, "Warning", "You should choose files to perform!", ToolTipIcon.Warning);
                return;
            }
            if(textBox1.Text=="")
            {
                notifyIcon1.ShowBalloonTip(999, "Warning", "You should enter the password!", ToolTipIcon.Warning);
                return;
            }
            if (textBox2.Text == ""&&checkBox1.Checked==true)
            {
                notifyIcon1.ShowBalloonTip(999, "Warning", "You should enter the extension of the encrypted files!", ToolTipIcon.Warning);
                return;
            }
            progressBar1.Style = ProgressBarStyle.Marquee;
            backgroundWorker1.RunWorkerAsync();
            progressBar1.Style = ProgressBarStyle.Blocks;
 
 
        }
 
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            foreach(string path in richTextBox1.Lines)
            {
                //MessageBox.Show(path);
                if(File.Exists(path))
                {
                   try
                    {
                        byte[] os= FileContent(path),ns;
 
                        if(checkBox1.Checked==true)
                        {
                            //加密
                              ns = AESEncrypt(os, textBox1.Text, textBox1.Text);
                            FileStream fs = new FileStream(path + "." + textBox2.Text, FileMode.Create, FileAccess.Write);
                        fs.Write(ns, 0, ns.Length);
                        fs.Close();
                        File.Delete(path);
                        progressBar1.Style = ProgressBarStyle.Blocks;
                            richTextBox1.Text = "";
                            notifyIcon1.ShowBalloonTip(999, "Info", "Done", ToolTipIcon.Info);
                        }
                        else
                        {
                            ns = AESDecrypt(os, textBox1.Text, textBox1.Text);
                            FileInfo fi = new FileInfo(path);
                            string np =fi.DirectoryName+Path.GetFileNameWithoutExtension(path);
                            FileStream wi = new FileStream(np, FileMode.Create, FileAccess.Write);
                            wi.Write(ns, 0, ns.Length);
                            wi.Close();
 
                            progressBar1.Style = ProgressBarStyle.Blocks;
                            richTextBox1.Text = "";
                            notifyIcon1.ShowBalloonTip(999, "Info", "Done", ToolTipIcon.Info);
 
 
                        }
 
 
 
                    }
                    catch(Exception)
                    {
                       notifyIcon1.ShowBalloonTip(999, "Error", "Something went wrong!", ToolTipIcon.Error);
                    }
                }
                else
                {
                    if(path!="")
                    notifyIcon1.ShowBalloonTip(999, "Error", path +  " doesn't exist",ToolTipIcon.Error);
                    continue;
                }
            }
        }
 
        private byte[] FileContent(string fileName)
        {
            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                try
                {
                    byte[] buffur = new byte[fs.Length];
                    fs.Read(buffur, 0, (int)fs.Length);
                    fs.Close();
                    return buffur;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            checkBox1.Checked = false;
            checkBox2.Checked = false;
            textBox1.Text = "";
            textBox2.Text = "";
            richTextBox1.Text = "";
        }
    }
}

首先这里的 button2_Click事件是主要事件,大家可以找找思路。

 

我先说明一下问题
出在这段代码

 

try
{
byte[] os= FileContent(path),ns;

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
    if(checkBox1.Checked==true)
    {
        //加密
          ns = AESEncrypt(os, textBox1.Text, textBox1.Text);
        FileStream fs = new FileStream(path + "." + textBox2.Text, FileMode.Create, FileAccess.Write);
    fs.Write(ns, 0, ns.Length);
    fs.Close();
    File.Delete(path);
    progressBar1.Style = ProgressBarStyle.Blocks;
        richTextBox1.Text = "";
        notifyIcon1.ShowBalloonTip(999, "Info", "Done", ToolTipIcon.Info);
    }
    else
    {
        ns = AESDecrypt(os, textBox1.Text, textBox1.Text);
        FileInfo fi = new FileInfo(path);
        string np =fi.DirectoryName+Path.GetFileNameWithoutExtension(path);
        FileStream wi = new FileStream(np, FileMode.Create, FileAccess.Write);
        wi.Write(ns, 0, ns.Length);
        wi.Close();
         File.delete(path);
 
        progressBar1.Style = ProgressBarStyle.Blocks;
        richTextBox1.Text = "";
        notifyIcon1.ShowBalloonTip(999, "Info", "Done", ToolTipIcon.Info);
 
 
    }
 
 
 
}
catch(Exception)
{
   notifyIcon1.ShowBalloonTip(999, "Error", "Something went wrong!", ToolTipIcon.Error);
}

我在写一个C#的文件加密解密程序,这里if判断用户要加密还是解密,然后对应执行操作。
if(checkbox1.checked==true)意味着加密。加密的代码都无误,也能顺利完成,但是else下面的解密代码块就有问题,具体的表现是能成功删除原始被加密的文件,但是无法创建解密后的新文件,简单来说就是只删除了待操作的文件。

 

我很不明白,同样的语法,只不过一个是加密,一个是解密,怎么解密就无法生成解密后文件了?

 

我找了一晚上bug,每个环节都逐步调试,但是还是没发现bug所在


[培训]《安卓高级研修班(网课)》月薪三万计划,掌握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

收藏
免费 1
打赏
分享
最新回复 (2)
雪    币: 6509
活跃值: (3432)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
zhatian 2021-6-13 05:36
2
0
不是好人。。
游客
登录 | 注册 方可回帖
返回