C# 要在字符的ASCII码或位移位中添加数字吗

C# 要在字符的ASCII码或位移位中添加数字吗,c#,ascii,C#,Ascii,好的,我接受一个字符串输入,将其转换为字符数组,并将其ASCII码保存在一个数组中 Random r = new Random(); Console.WriteLine("Enter name : "); char[] name = Console.ReadLine().ToCharArray(); byte[] by = new byte [name.Length]; int[] arr = new int[name.Length

好的,我接受一个字符串输入,将其转换为字符数组,并将其ASCII码保存在一个数组中

Random r = new Random();
        Console.WriteLine("Enter name : ");
        char[] name = Console.ReadLine().ToCharArray();
        byte[] by = new byte [name.Length];
        int[] arr = new int[name.Length];
        FileStream fs = new FileStream("F:\\abc.txt", FileMode.Create, FileAccess.Write);
        for (int i = 0; i < name.Length; i++)
        {
            fs.WriteByte((byte)name[i]);
        }

        for (int i = 0; i <name.Length ; i++)
        {
            by[i] =  ( ((byte )name[i]));

        }
        //for (int i = 0; i < name.Length; i++)
        //{
        //   arr[i] = (byte by[i] (Convert.ToInt16);
        //}
        // fs.WriteByte(48); fs.WriteByte(8); fs.WriteByte(60); fs.WriteByte(80);
        fs.Flush();
        fs.Close();
Random r=new Random();
Console.WriteLine(“输入名称:”);
char[]name=Console.ReadLine().ToCharArray();
byte[]by=新字节[name.Length];
int[]arr=newint[name.Length];
FileStream fs=newfilestream(“F:\\abc.txt”,FileMode.Create,FileAccess.Write);
for(int i=0;i对于(int i=0;i这可能会对您有所帮助。您实际上不需要在语言级别上转换为数字-您的信息更易于作为字符串或字节数组进行操作

以下代码替换现有代码的输出部分,并使用
random
实例
r
对随机数进行位加法

byte[] randomNumber = new byte[name.Length];
r.NextBytes(randomNumber);
for (int i = 0; i <name.Length ; i++)
{
    fs.WriteByte((byte)name[i] ^ randomNumber[i]);
}
byte[]randomNumber=新字节[name.Length];
r、 下一字节(随机数);
对于(int i=0;i和C#,这有点棘手,因为(根据)Char是一个16位的Unicode数值,而不仅仅是ASCII,所以您必须小心使用非ASCII符号和使用奇怪编码的解析文件。另一方面,它可以很容易地转换为无符号短int、int、Double和其他类型

基本上,简单的typecast可以做到:

char character;
int ascii_code = (int)character;
//some fency math and encoding with ascii_code goes here...
char encrypted_char = (char)ascii_code;

不确定,如果VisualStudio允许数学直接与<代码> char < /Cord>类型变量(C,C++ do)。< /P> < P>正确地处理这一点的方法是:

Console.WriteLine("Enter your name:");
string name = Console.ReadLine();
byte[] ascii = Encoding.ASCII.GetBytes(name);
short[] shorts = bytes.Select(b => (short)b).ToArray();
int[] finalBytes = new int[shorts.length];
int[] randomKey = new int[shorts.length];
int ndx = 0;
Random r = new Random();

foreach (short b in shorts)
{
    int rByte = r.Next(1, 5000);
    int singleByte = b + rByte;
    finalBytes[ndx] = singleByte;
    randomKey[ndx] = rByte;
    ndx++;
}

// finalBytes now holds your data. Do something with it!
// randomKey holds your hash data. To decode this, you'll
// need to subtract the value in randomKey[n] from finalBytes[n]

正如其他人所说,强烈建议不要在任何生产代码中使用此代码!

我正在删除加密标记,这与加密或安全无关。完成后,我将向您发布整个程序;).NET framework中有用于加密的类。请使用它们。除非您已经是密码学专家,否则不要使用自己的类。使用这些类不会有问题。但这是我的项目,它有一个标准。我们必须从头开始构建,不能使用预定义的类。如果是用于玩具项目或教育nal用途,很好。但如果这是一个真正的项目的真正加密,那么你就犯了一个错误。谢谢你,伙计。这很有帮助。这只是一个小的教育项目。但我仍然很好奇,为什么它不应该用在任何生产代码中?@OsamaMunir-因为一个有天赋的黑客可以对你的代码进行反向工程,从而影响你的努力保护您要保护的任何数据