Bonjour, j'aimerai convertir ce code en C#:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
std::string DCrypt::Decrypt(unsigned char str[])
{
    std::string decrypted_string;
    char table[] = {' ','-','.','0','1','2','3','4','5','6','7','8','9','n'};
    int count = 0;
 
    for (count = 0; count < strlen((const char*)str); )
    {
        if (str[count] <= 0x7A)
        {
            unsigned char len = str[count];
 
            for (int i = 0; i < (int)len; i++)
            {
                count++;
                decrypted_string += str[count] ^ 0xFF;
            }
 
            count++;
        }
        else
        {
            unsigned char len = str[count];
            len &= 0x7F;
 
            for (int i = 0; i < (int)len;)
            {
                count++;
 
                unsigned char highbyte = str[count];
                highbyte &= 0xF0;
                highbyte >>= 0x4;
 
                unsigned char lowbyte = str[count];
                lowbyte &= 0x0F;
 
                if (highbyte != 0x0 && highbyte != 0xF)
                {
                    decrypted_string += table[highbyte-1];
                    i++;
                }
 
                if (lowbyte != 0x0 && lowbyte != 0xF)
                {
                    decrypted_string += table[lowbyte-1];
                    i++;
                }
            }
            count ++;
        }
    }
 
    return decrypted_string;
}

Cette partie de code pose problème surtout, car en fait count peut être plus grand que le nombre de caractères dans la chaine...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
if (str[count] <= 0x7A)
        {
            unsigned char len = str[count];
 
            for (int i = 0; i < (int)len; i++)
            {
                count++;
                decrypted_string += str[count] ^ 0xFF;
            }
 
            count++;
        }
Donc en C++ sa ne pose pas de problème, mais en C# j'ai l'erreur qui me dit que je suis en dehors des limites...
Code essayer en C#:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 public static string Decrypt(byte[] str)
        {
            string decrypted_string = "";
            char[] table = { ' ', '-', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'n' };
            int count = 0;
 
            for (count = 0; count < str.Length; )
            {
                if ((int)str[count] <= 0x7A)
                {
                    byte len = unchecked((byte)str[count]);
 
                    for (int i = 0; i < (int)len; i++)
                    {
                        count++;
                        //Console.WriteLine(count);
                        try
                        {
                            decrypted_string += Encoding.GetEncoding("IBM865").GetString(new byte[1] { (byte)((int)str[count] ^ 0xFF) });
                        }
                        catch
                        {
                            decrypted_string += Encoding.GetEncoding("IBM865").GetString(new byte[1] { (byte)(0 ^ 0xFF) });
                            //Console.WriteLine(0);
                        }
                    }
 
                    count++;
                }
                else
                {
                    byte len = unchecked((byte)str[count]);
                    len &= unchecked((byte)0x7F);
 
                    for (int i = 0; i < (int)len; )
                    {
                        count++;
 
                        byte highbyte = unchecked((byte)str[count]);
                        highbyte &= unchecked((byte)0xF0);
                        highbyte >>= unchecked((byte)0x4);
 
                        byte lowbyte = unchecked((byte)str[count]);
                        lowbyte &= unchecked((byte)0x0F);
 
                        if (highbyte != 0x0 && highbyte != 0xF)
                        {
                            decrypted_string += table[highbyte - 1];
                            i++;
                        }
 
                        if (lowbyte != 0x0 && lowbyte != 0xF)
                        {
                            decrypted_string += table[lowbyte - 1];
                            i++;
                        }
                    }
                    count++;
                }
            }
 
            byte[] decrypted_byte = new byte[decrypted_string.Length];
            for (int i = 0; i < decrypted_string.Length; i++)
            {
                decrypted_byte[i] += unchecked((byte)decrypted_string[i]);
            }
 
            return decrypted_string;
        }
J'ai fait les tests suivant:
En C#
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
byte[] test = new byte[4];
            test[0] = (byte)'t';
            test[1] = (byte)'e';
            test[2] = (byte)'s';
            test[3] = (byte)'t';
            string test2 = Decrypt(test);
            Console.WriteLine(test2.Length);
            Console.WriteLine(test2);
            Console.WriteLine("int: " + (int)test2[1]);
Resultat:


En C++
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
unsigned char *test = (unsigned char*)"test";
std::string test2 = DCrypt::Decrypt(test);
std::cout << test2.size() << std::endl;
std::cout << test2 << std::endl;
std::cout << "int: " << (int)test2[1] << std::endl;
Resultat:


Je me retourne vers vous pour trouver mon aide