微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 帮我XOR加密

我在C#中编写了这个代码,用一个键来加密一个字符串:
private static int Bin2Dec(string num)
{
    int _num = 0;

    for (int i = 0; i < num.Length; i++)
        _num += (int)Math.Pow(2,num.Length - i - 1) * int.Parse(num[i].ToString());

    return _num;
}

private static string Dec2Bin(int num)
{
    if (num < 2) return num.ToString();

    return Dec2Bin(num / 2) + (num % 2).ToString();
}

public static string StrXor(string str,string key)
{
    string _str = "";
    string _key = "";
    string _xorStr = "";
    string _temp = "";

    for (int i = 0; i < str.Length; i++)
    {
        _temp = Dec2Bin(str[i]);    

        for (int j = 0; j < 8 - _temp.Length + 1; j++)
            _temp = '0' + _temp;

        _str += _temp;
    }

    for (int i = 0; i < key.Length; i++)
    {
        _temp = Dec2Bin(key[i]);

        for (int j = 0; j < 8 - _temp.Length + 1; j++)
            _temp = '0' + _temp;

        _key += _temp;
    }    

    while (_key.Length < _str.Length) _key += _key;

    if (_key.Length > _str.Length) _key = _key.Substring(0,_str.Length);

    for (int i = 0; i < _str.Length; i++)
        if (_str[i] == _key[i]) { _xorStr += '0'; } else { _xorStr += '1'; }

    _str = "";

    for (int i = 0; i < _xorStr.Length; i += 8)
    {
        char _chr = (char)0;
        _chr = (char)Bin2Dec(_xorStr.Substring(i,8)); //ERROR : (Index and length must refer to a location within the string. Parameter name: length)
        _str += _chr;
    }

    return _str;
}

问题是当我想使用这段代码解密一个加密的文本时,总是会收到错误

string enc_text = ENCRYPT.XORENC("abc","a"); // enc_text = " ♥☻"
string dec_text = ENCRYPT.XORENC(enc_text,"a"); // ArgumentOutOfRangeException

任何线索?

解决方法

如果你有一个字符,一个字符,你可以将它转换成一个整数,一个int.

然后您可以使用^操作符对其执行XOR.目前您似乎没有使用该运算符,这可能是您的问题的根源.

string EncryptOrDecrypt(string text,string key)
{
    var result = new StringBuilder();

    for (int c = 0; c < text.Length; c++)
        result.Append((char)((uint)text[c] ^ (uint)key[c % key.Length]));

    return result.ToString();
}

那种事情这是一个更长的版本,有评论的步骤相同,以便于学习:

string EncryptOrDecrypt(string text,string key)
{
    var result = new StringBuilder();

    for (int c = 0; c < text.Length; c++)
    {
        // take next character from string
        char character = text[c];

        // cast to a uint
        uint charCode = (uint)character;

        // figure out which character to take from the key
        int keyPosition = c % key.Length; // use modulo to "wrap round"

        // take the key character
        char keyChar = key[keyPosition];

        // cast it to a uint also
        uint keyCode = (uint)keyChar;

        // perform XOR on the two character codes
        uint combinedCode = charCode ^ keyCode;

        // cast back to a char
        char combinedChar = (char)combinedCode;

        // add to the result
        result.Append(combineChar);
    }

    return result.ToString();
}

短版本是相同的,但中间变量被删除,将表达式直接替换到它们被使用的位置.

原文地址:https://www.jb51.cc/csharp/95357.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐