0

I am translating a decryption function that I have found on the internet. The function is written in Python, and I am translating it to C#. I have a fair amount of it done (hopefully correctly), but some of the syntax I'm having a hard time reading.

Here's the original function:

def decrypt_signature(s):
arr = list(s)
   arr[0], arr[52] = arr[52%len(arr)], arr[0]
   arr.reverse()
   arr = arr[3:]
   arr[0], arr[21] = arr[21%len(arr)], arr[0]
   arr.reverse()
   arr = arr[3:]
   arr.reverse()

   return "".join(arr)

And here's what I have translated so far:

private string DecryptSig(string s)
        {
            //arr = list(s)
            char[] arr = s.ToArray();

            //arr[0], arr[52] = arr[52%len(arr)], arr[0]
            var temp = arr[0];
            arr[0] = arr[52 % arr.Length];
            arr[52 % arr.Length] = temp;

            //arr.reverse()
            arr.Reverse();

            //arr = arr[3:]
            //?????????????????

            //arr[0], arr[21] = arr[21 % len(arr)], arr[0]
            temp = arr[0];
            arr[0] = arr[21 % arr.Length];
            arr[21 % arr.Length] = temp;

            //arr.reverse()
            arr.Reverse();

            //arr = arr[3:]
            //??????????????????

            //arr.reverse()
            arr.Reverse();

            //return "".join(arr)
            return "" + new string(arr);
        }

What does the author do when they write arr = arr[3:]? To me it appears like it's just taking the first three values of the array and writing them back to their original indices.

w0f
  • 908
  • 9
  • 23

2 Answers2

0

He's taking all of the items, except for the first 3. You can use the C# LINQ equivalent, Skip:

arr = arr.Skip(3).ToArray();
Uriel
  • 15,579
  • 6
  • 25
  • 46
0

in Python, you can get some elements from a list using [startingIndex:EndingIndex]

arr[:] means all the elements

arr[3:] means skip 3 and take the rest

As Uriel mentions, the C# equivalent is Skip(count)

arr = arr.Skip(3).ToArray();

and you need to add this:

using System.Linq;

Python Slices: Explain slice notation