亲爱的stackoverflow助手 我只是在测试我的技能和学习方式,就像我遇到一些奇怪的东西时一样,在网上搜索但却无法做到。 找出问题所在。
我正在尝试构建一个将密钥存储在isostream中的加密程序。 第一次你可以在没有条件的情况下写密钥,但是之后你需要输入旧密钥
(IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("key.txt", FileMode.CreateNew, isoStore))
{
using (StreamWriter writer = new StreamWriter(isoStream))
{
//keycreation begin
Random rnd = new Random();
int random1 = rnd.Next(20000, 2000000); //number between 2000 and 2 million
stringrandom0 = random1.ToString();
char[] chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
Random r = new Random();
int i = r.Next(chars.Length);
int q = r.Next(chars.Length);
int w = r.Next(chars.Length);
stringrandom1 = chars[i].ToString();
stringrandom2 = chars[q].ToString();
stringrandom3 = chars[w].ToString();
int random2 = rnd.Next(20000, 2000000); //number between 2000 and 2 million
stringrandom4 = random2.ToString();
key = stringrandom0 + stringrandom1 + stringrandom2 + stringrandom3 + stringrandom4;
textBox2.Text = "Creation & Encryption succesful finished your key = " + key + " do not lose this key!";
//key will look like this: letters, numbers, letters
//key creation end
writer.WriteLine(key);
File.Create(@"C:\Users\Legion\Desktop\encryption\text\" + fileName1).Close();
//Console.WriteLine("You have written to the file.");
}
}
所以它给了我一把钥匙:当事情没有成功时我会让它显示在文本框中以确定。我这样接受了。
IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("key.txt", FileMode.Open, isoStore))
{
using (StreamReader reader = new StreamReader(isoStream))
{
keyentered = textBox4.Text;
checkkey = (reader.ReadToEnd());
textBox1.Text = checkkey;
textBox3.Text = keyentered;
}
}
你可以看到我输入的代码设置为keyenter,我输入了:156216 * ek316677。
Checkkey读取书面文件,然后检查,我把它放到文本框中,然后显示:(完全没有空格)156216 * ek316677。
比较 156216 * ek316677。 156216 * ek316677。
所以这是支票代码
if (isoStore.FileExists("key.txt"))
{
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("key.txt", FileMode.Open, isoStore))
{
if (keyentered.Contains(checkkey) || checkkey == keyentered)
{
using (StreamWriter writer = new StreamWriter(isoStream))
{
//keycreation begin
Random rnd = new Random();
int random1 = rnd.Next(20000, 2000000); //number between 2000 and 2 million
stringrandom0 = random1.ToString();
char[] chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
Random r = new Random();
int i = r.Next(chars.Length);
int q = r.Next(chars.Length);
int w = r.Next(chars.Length);
stringrandom1 = chars[i].ToString();
stringrandom2 = chars[q].ToString();
stringrandom3 = chars[w].ToString();
int random2 = rnd.Next(20000, 2000000); //number between 2000 and 2 million
stringrandom4 = random2.ToString();
key = stringrandom0 + stringrandom1 + stringrandom2 + stringrandom3 + stringrandom4;
textBox2.Text = "Creation & Encryption succesful finished your key = " + key + " do not lose this key!";
//key will look like this: letters, numbers, letters
//key creation end
writer.WriteLine(key);
File.Create(@"C:\Users\Legion\Desktop\encryption\text\" + fileName1).Close();
//Console.WriteLine("You have written to the file.");
}
}
else
{
textBox2.Text = "invalid key entered!";
}
}
}
问题是它一直说无效键输入,而它们完全相同,那么为什么if条件失败? 我尝试了一个包含但它以完全相同的方式失败。
感谢您提供的任何帮助。 干杯的神灵,霍比和书呆子。 :d