XOR编码的密码错误 [英] cipher error for XOR encoding

查看:62
本文介绍了XOR编码的密码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加密一个字符串:

i want to cipher a string:

using System;
class Examole
{
    static void Main()
    {
        string msg = "Mesut Darvishan";
        int key = 180;

        Console.WriteLine("This is our string: " + msg);

        // Error: operator '^' cannot be applied to operands of type 'string' and 'int'
        string result = (msg ^ key);
    }
}


但我给出了这个错误:
运算符"^"不能应用于类型为"string"和"int"的操作数.


but i give this error:
operator ''^'' cannot be applied to operands of type ''string'' and ''int''

推荐答案

您只能将XOR运算符应用于单个运算符字符串中的字符:
You can only apply the XOR operator to individual characters in your string:
char key = (char)180;
StringBuilder sb = new StringBuilder(msg.Length);
foreach (char c in msg)
    {
    sb.Append((char) (c ^ key));
    }
string result = sb.ToString();

但是,我不保证这将适用于所有可能的字符-您最好输出一个字节字符串而不是一个字符串(因为Unicode可能不喜欢您的所有字符结果)

However, I don''t guarantee that this will work for all possible characters - you would be better outputing a string of bytes instead of a string (as Unicode may not like all your character results)


这是我的朋友(Alireza Dehqani)向我建议的另一种解决方法:)
This is another solution that my friend (Alireza Dehqani) suggests it to me :)
using System;
class Example
{
    static void Main()
    {
        Console.WriteLine("Enter a string to cipher: ");
        string str = Console.ReadLine();
        int key = 180;

        char[] chars = new char[str.Length];

        Console.WriteLine("This is your string after encoding: ");
        for (int i = 0; i < chars.Length; i++)
        {
            chars[i] = str[i];
            chars[i] = (char)(chars[i] ^ key);
            Console.Write(chars[i]);
        }
        Console.WriteLine();
    }
}


感谢OriginalGriff和Alireza Dehqani


Thx to OriginalGriff and Alireza Dehqani


这篇关于XOR编码的密码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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