Cade Martinez Cade Martinez - 29 days ago 22
C# Question

How can I make an array to insert a bunch of strings?

I'm making a simple obfuscator in c# forms and I'm using this code to insert it:

string a = RandomString(8);
string b = RandomString(8);
string c = RandomString(8);
string d = RandomString(8);
string e = RandomString(8);
string f = RandomString(8);
string g = RandomString(8);
string h = RandomString(8);
string i = RandomString(8);
string j = RandomString(8);
string k = RandomString(8);
string l = RandomString(8);
string m = RandomString(8);
string n = RandomString(8);
string o = RandomString(8);
string p = RandomString(8);
string q = RandomString(8);
string r = RandomString(8);
string s = RandomString(8);
string t = RandomString(8);
string u = RandomString(8);
string v = RandomString(8);
string w = RandomString(8);
string x = RandomString(8);
string y = RandomString(8);
string z = RandomString(8);

Code.Text = Code.Text.Insert(0, "set " + a + "=a\n" + "set " + b + "=b\n" + "set " + c + "=c\n" + "set " + d + "=d\n" + "set " + e + "=e\n" + "set " + f + "=f\n" + "set " + g + "=g\n" + "set " + h + "=h\n" + "set " + i + "=i\n" + "set " + j + "=j\n" + "set " + k + "=k\n" + "set " + l + "=l\n" + "set " + m + "=m\n" + "set " + n + "=n\n" + "set " + o + "=o\n" + "set " + p + "=p\n" + "set " + q + "=q\n" + "set " + r + "=r\n" + "set " + s + "=s\n" + "set " + t + "=t\n" + "set " + u + "=u\n" + "set " + v + "=v\n" + "set " + w + "=w\n" + "set " + x + "=x\n" + "set " + y + "=y\n" + "set " + z + "=z\n");


This is the output:

set pbatbpkz=a
set aqtwbqlg=b
set hitsvkvc=c
set imuwqdfy=d
set flitwcte=e
etc...


I need to keep the random strings separate so that I can later insert code to replace characters like "a" with string a.

This obviously does not look efficient or professional. I'm considerably new to c#, so I don't know how I could make something like an array to insert this block of text.

Answer

One way to do this is to use a Dictionary.

   Dictionary<char, string> strDict = new Dictionary<char, string>();
        for (int i = 0; i < 26; i++)
        {
            if(!strDict.ContainsKey((char)(i+97)))
            {
                strDict.Add((char)(i + 97), RandomString());
            }
            else
            {
                strDict[(char)(i + 97)] = RandomString();
            }
        }

This will fill your strDict with random strings that can be accessed with characters from 'a' to 'z' Then you get these strings by:

string strA = strDict['a'] //and so on