如何在C#中对字符串执行补码运算 [英] How to perform one's complement opration on strings in c#

查看:198
本文介绍了如何在C#中对字符串执行补码运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的项目(家)有一个带有文本框和按钮的Windows窗体.用户必须在文本框中输入一个字符串(例如他的名字).此值存储在变量字符串textdata;"中.现在,我必须将此数据写入文本文件"Log.txt".

为此,我使用"File.WriteAllBytes(textdata)".使用另一种形式,将访问文件"Log.txt",并将字符串值显示在多行文本框中.直到我想到对Log.txt文件中的数据进行加密的想法,这一切都很好.为此,我要对字符串变量执行补码并将补码数据写入文件,并且必须从另一种形式读取补码数据,并且必须在文本框中显示正确的字符串.

我将详细解释其逻辑:

1> Form_Read是用于读取"log.txt"文件并在文本框中显示文件中字符串的表单.
2> Form_Write是用于获取用户名并将其写入"log.txt"文件的表单.

我的意图是:

1>在将字符串数据写入"log.txt"之前,必须对其进行补充,即,如果要在文件上写入1001,则必须将其写入为0110,以使文件数据无法以常规方式显示. > 2>在另一种形式的文本框中显示"log.txt"的内容之前,必须再次对其进行补充,以便普通文本将显示在文本框中.

我该怎么办??

我尝试使用byte [] variable = Encoding.ASCII.GetBytes(data);
但无论哪种情况,输出都只是一个普通的整数值,并且不会显示正确的文本.请提供适当的解决方案.

在此先感谢

My current project(home) has a windows form with a textbox and a button in it. The user hasto enter a string in the textbox (say his name). This value is stored in a variable "string textdata;". Now I''ve to write this data into a text file "Log.txt".

For this, i''m using "File.WriteAllBytes(textdata)". Using another form, the file "Log.txt" will be accessed and the string values are displayed in a multiline textbox. This was going fine until I got an idea of encrypting the data in the Log.txt file. For this, I want to perform one''s complement on the string variable and write the complemented data into the file and the same must be read from another form and proper string must be displayed in the text box.

I''ll explain the logic in detail:

1> Form_Read is a form used to read the "log.txt" file and display the string in the file in a text box.
2> Form_Write is a form used to get the username and write it into the "log.txt" file.

My intention is:

1> Before writing the string data into "log.txt", it must be complemented i.e., if 1001 is to be written on the file, it must be written as 0110 so that the file data will not be visible in normal way.
2> Before displaying the contents of "log.txt" in text box of another form, it must be complemented once again so that the normal text will be displayed in the text box.

How Can I do this???

I''ve tried using byte[] variable= Encoding.ASCII.GetBytes(data);
but the output in either case is just a normal integer values and the proper text is not being displayed. Please give a proper solution..

Thanks in advance

推荐答案

这是加密"的一种非常原始的形式-.NET中内置了许多更好的东西.但这很简单...
It''s a pretty primitive form of "Encryption" - there are a lot betters ones built into .NET. But it''s pretty simple...
string inp = "Hello there";
byte[] data = Encoding.ASCII.GetBytes(inp);
for (int i = 0; i < data.Length; i++)
    {
    data[i] ^= 0xFF;
    }
//... Save and then load
for (int i = 0; i < data.Length; i++)
    {
    data[i] ^= 0xFF;
    }
string outp = System.Text.Encoding.ASCII.GetString(data);


这篇关于如何在C#中对字符串执行补码运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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