Problem with for and string. Remove

Asked

Viewed 44 times

0

I’m having a problem with a program that I did for encryption and decryption from a hash, don’t judge how it works, I did it while it was very tied. What is not working is the Decrypt, I believe it is the function replacerbyhash(), but I couldn’t find the problem.

Code:

else if(type.ToLower() == "decrypt")
{
    Console.WriteLine("Digite o hash.");
    hash = Console.ReadLine();
    Console.WriteLine("Digite a mensagem encriptada.");
    txt = Console.ReadLine();
    replacerbyhash();
    replacer2();
}

Replacerbyhash method:

static void replacerbyhash()
{
    for (int i = 0; i < 26; i++)
    {
        az[i] = hash.Remove(i * 4, 4);
    }
}

Replacer2 method:

static void replacer2()
{
    txt = txt.Replace(az[0], "a");
    txt = txt.Replace(az[1], "b");
    txt = txt.Replace(az[2], "c");
    txt = txt.Replace(az[3], "d");
    txt = txt.Replace(az[4], "e");
    txt = txt.Replace(az[5], "f");
    txt = txt.Replace(az[6], "g");
    txt = txt.Replace(az[7], "h");
    txt = txt.Replace(az[8], "i");
    txt = txt.Replace(az[9], "j");
    txt = txt.Replace(az[10], "k");
    txt = txt.Replace(az[11], "l");
    txt = txt.Replace(az[12], "m");
    txt = txt.Replace(az[13], "n");
    txt = txt.Replace(az[14], "o");
    txt = txt.Replace(az[15], "p");
    txt = txt.Replace(az[16], "q");
    txt = txt.Replace(az[17], "r");
    txt = txt.Replace(az[18], "s");
    txt = txt.Replace(az[19], "t");
    txt = txt.Replace(az[20], "u");
    txt = txt.Replace(az[21], "v");
    txt = txt.Replace(az[22], "w");
    txt = txt.Replace(az[23], "x");
    txt = txt.Replace(az[24], "y");
    txt = txt.Replace(az[25], "z");
}

How Hash is made:

static void compile()
{
    for (int i = 0; i < 26; i++)
    {
        az[i] = x.Next(1000, 9999).ToString();
    }
}

I know the code is poorly done, I didn’t think much to do it.

  • 1

    can post an example of the hash and the example of an encrypted message ?

  • 1

    It is difficult to say anything just with this excerpt, what I can say is that in fact not to do something much simpler, efficient and less tragic for GC. And if I understand correctly, you are using random to "encrypt", then I think I would just be soiling the data, IE, no one else can use it for anything.

  • @Yes, I know I’m doing it the wrong way, I only did it because I was too busy in class, this code is useless to me. But even so this problem I’m having has left me a little curious... Entire code.

  • @Rovannlinhalis The word "test". Hash: 25373944232485139036550471698470496693739437688485855615330811538489511846224254934738599164124870343271 Encrypted Message: 4254903646224254

1 answer

1


Trying to understand what you are doing, the values of the vector would be these then (in bold), what is in Italian was removed by the Remove function():

az[0] = 2537 394423248513903655047169847049669373943768848556153308 11538489511846224254 93473859916412487034 3271

az[1] = 2537 3944 23248513903655047169847049669373943768848556153308 11538489511846224254 93473859916412487034 3271

...and so on...

maybe his intention was to use a Substring instead of the remove, so that the values would look like this:

az[0] = 2537 //a

az[1] = 3944 //b

c      d    e    f    g    h    i   j     k     l    m   n     o
2324 8513 9036 5504 7169 8470 4966 9373  9437 6884 8585 5615 3308‌ ​

 p     q    r   s    t    u    v     w    x    y    z
1153 8489 5118 4622 4254‌ ​9347 3859 9164 1248 7034‌ ​3271

Thus conferring with the

test = 4254 9036 4622 4254 

So just update your function:

static void replacerbyhash()
{
    for (int i = 0; i < 26; i++)
    {
        az[i] = hash.SubString(i * 4, 4);
    }
}
  • 1

    Exactly what I wanted, did not know the existence of Substring, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.