脚本在c#中屏蔽字符串

本文关键字:屏蔽 字符串 脚本 | 更新日期: 2023-09-27 17:50:07

我有一个代码,如果我传递一个输入数据为"sail",我的代码将生成一个掩码输出,如"aisl"或"isal"。其中的输出将是混杂格式的输入。我想要一个输出,它不应该产生与输入完全相同的字母的输出。下面是我目前的代码,请帮助我这个

        string InputData = "123456";
        string MaskedData = InputData;
        if (MaskedData.Length > 0)
        {
            // The technique used to mask the data is to replace numbers with random numbers and letters with letters
            //char[] chars = new char[InputData.Length];
            char[] chars = new char[InputData.Length];
            Random rand = new Random(DateTime.Now.Millisecond);
            int index = 0;
            while (InputData.Length > 0)
            {
                // Get a random number between 0 and the length of the word.
                int next = rand.Next(0, InputData.Length - 1);
                // Take the character from the random position and add to our char array.
                //chars[index] = InputData[next];
                chars[index] = InputData[next];
                // Remove the character from the word.
                InputData = InputData.Substring(0, next) + InputData.Substring(next + 1);
                ++index;
            }
            MaskedData = new String(chars);
}

脚本在c#中屏蔽字符串

在dotnetperls.com的这个页面中有一个随机化字符串数组的算法。通过一些更改,您可以使用它来随机化字符串,使用字符串也是字符数组这一事实。给你:

static class RandomCharArrayTool
{
    static Random _random = new Random();
    public static string RandomizeChars(string theString)
    {
        var arr = theString.ToCharArray();
        List<KeyValuePair<int, char>> list = new List<KeyValuePair<int, char>>();
        // Add all strings from array
        // Add new random int each time
        foreach (char s in arr)
        {
            list.Add(new KeyValuePair<int, char>(_random.Next(), s));
        }
        // Sort the list by the random number
        var sorted = from item in list
             orderby item.Key
             select item;
        // Allocate new string array
        char[] result = new char[arr.Length];
        // Copy values to array
        int index = 0;
        foreach (KeyValuePair<int, char> pair in sorted)
        {
            result[index] = pair.Value;
            index++;
        }
        // Return string generated from copied array
        return new string(result);
    }
}

你可以这样使用:

RandomCharArrayTool.RandomizeChars("sail");

请尝试使用下面的代码片段。

方法1:

string word = "123456";
string temp = word;
string result = string.Empty;
Random rand = new Random();
for (int a = 0; a < word.Length; a++)
{
    //multiplied by a number to get a better result, it was less likely for the last index to be picked
    int temp1 = rand.Next(0, (temp.Length - 1) * 3);
    result += temp[temp1 % temp.Length];
    temp = temp.Remove(temp1 % temp.Length, 1);
}
string str = result;

Method2:

var rnd = new Random();
string InputData = "123456";
string MaskedData = new string(InputData.OrderBy(r => rnd.Next()).ToArray());

您有几种不同的方法,但最简单的可能是实现SecureStringHash。这两种方法中的一种可以很容易地加密你的数据。在这个概念下,mask是隐藏所述内容。

// SecureString
var secure = new SecureString();
foreach(var character in textbox.Text.ToCharArray())
     secure.AppendChar(character);

一旦您的string被放置在内存中,它将在内存中加密。这是一个粗略的实现,但是文档将帮助您改进方法以满足您的需求。

// Hash (BCrypt for simplicity)
private const int factor = 12;
var hashed = BCrypter.HashPassword(textbox.Text, BCrypter.GenerateSalt(factor));
另一种方法是简单的随机化,类似于单词洗牌。