XOR加密到文件,然后解密,并显示它? [英] XOR encrypt to file, then decrypt it, and display it?

查看:103
本文介绍了XOR加密到文件,然后解密,并显示它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在使用一个函数从texbox中获取TEXT,然后将其保存到文件中:



Hello all, i am using a function to get the TEXT from a texbox, XOR it, then save to a file:

private static string encryptDecrypt(string input) {
        char[] key = {'K', 'C', 'Q'}; //Any chars will work, in an array of any size
        char[] output = new char[input.Length];

        for(int i = 0; i < input.Length; i++) {
            output[i] = (char) (input[i] ^ key[i % key.Length]);
        }

        return new string(output);
    }





写入文件,XOR全部正常工作,但我有一个richtextbox,我想打开文件并查看当前内容,当然是逐行的(因为该文件是一个列表)。

问题是,我得到一个非常奇怪的例外:打开文件时出错,发现非法字符bla bla ...





Writing to file, XOR all working, BUT i have a richtextbox, in wich i want to open the file and see the current content, of course line by line ( because the file is a list ).
The problem is, i get a really wierd Exception: "Error in opening file, illegal characters found" bla bla...

try
               {
                   if ((myStream = openFileDialog1.OpenFile()) != null)
                   {
                       using (myStream)
                       {
                           richTextBox1.Text = System.IO.File.ReadAllText(encryptDecrypt(openFileDialog1.FileName));
                       }
                   }
               }





有人可以解释一下我做错了什么吗?它应该解密文件,并将其输出到我的richtextbox1。



注意:没有xor一切正常,没有xor ...欢迎它没用。

推荐答案

嗯。

您确实意识到您正在做的是解密文件名,然后尝试打开那个作为文件?

所以你将D:\ Temp \ MyFile.encrypt传递给你的解密方法,然后回到&!^%£&£!^ £)(!£NB£F然后你将其作为有效路径传递给文件......:笑:

尝试:

Um.
You do realise that what you are doing is decrypting the filename, then trying to open that as a file?
So you will pass "D:\Temp\MyFile.encrypt" to your decryption method, and get back "&!^%£&"£!^£)("!£NB"£F" which you then pass to the file as a valid path...:laugh:
Try:
richTextBox1.Text = encryptDecrypt(System.IO.File.ReadAllText(openFileDialog1.FileName));


这篇关于XOR加密到文件,然后解密,并显示它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆