Post

EasySharp - TryCrack.Me C# Code Review Challange Walkthrough

Are you ready for a Code Review Challenge ??

Let’s dive into a C# code review from TryCrack.ME and elevate our skills stats.

  • The purpose of this review is to go through the C#e provided for the EasySharp challenge from TryCrack.Me and to solve the challenge effectively. We will break down the code, understand its functionality, and then proceed with solving the problem.

Here is Our Code…..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string your_input = "";
char[] array = your_input.ToCharArray();
string[] array2 = new string[array.Length];
string enc = "";
string correct = "144260662546426066";

for (int i = 0; i < your_input.Length; i++)
{
    array2[i] = Convert.ToString((int)array[i] - 55);
    enc += array2[i];
}

if (enc == correct)
    Console.WriteLine("Good job!");
else
    Console.WriteLine("Wrong Answer!");

What Does Code Says………

# Initialization :

1
2
3
4
5
6
string your_input = "";
char[] array = your_input.ToCharArray();
string[] array2 = new string[array.Length];
string enc = "";
string correct = "144260662546426066";
  • your_input: This variable is initialized as an empty string, but it would typically hold the user’s input.
  • array: The ToCharArray() method converts the string your_input into an array of characters, where each character is stored in a separate index of the array.
  • array2: This is an array of strings, where each element will later store a processed value of the corresponding character in your_input.
  • enc: This is an empty string that will store the encoded version of the your_input.
  • correct: This is a string representing the correct encoded value that will be compared against the encoded value of the input (enc).

# The Loop :

1
2
3
4
5
6
for (int i = 0; i < your_input.Length; i++)
{
    array2[i] = Convert.ToString((int)array[i] - 55);
    enc += array2[i];
}
  • This loop iterates through each character in the your_input string.

Key steps inside the loop:

  • array2[i] = Convert.ToString((int)array[i] - 55);
    • (int)array[i]: This converts the character at position i in your_input to its corresponding ASCII (integer) value.
    • - 55: This operation subtracts 55 from the ASCII value of the character.
      • For example, for the character 'A' (ASCII value 65), (int)'A' - 55 gives 10. Similarly, for 'B' (ASCII value 66), the result is 11.
    • Convert.ToString(...): The resulting integer (after subtraction) is converted to a string and stored in array2[i].
  • enc += array2[i];
    • This appends the encoded value from array2[i] to the string enc. Over time, enc becomes the fully encoded version of your_input

# Comparison :

1
2
3
4
if (enc == correct)
    Console.WriteLine("Good job!");
else
    Console.WriteLine("Wrong Answer!");
  • After the loop completes, the program compares the encoded string (enc) with the pre-defined correct encoded string ("144260662546426066").
    • If the two strings match, it prints “Good job!”
    • Otherwise, it prints “Wrong Answer!”

# Simple View :

The encoding logic takes each character from your_input, converts it to its ASCII value, subtracts 55, and stores the result as a string in array2. All these values are concatenated into the enc string, which is then compared with the correct string.

# Let’s solve this challenge Backwards …..

  • Let’s take the value that was given to us in the code for comparison to our input 144260662546426066 , as the char are in the ASCII value format , let’s split it with two digits and add 55 to each digit ….
1
2
3
4
5
6
7
8
9
10
- 14 + 55 = 69
- 42 + 55 = 97
- 60 + 55 = 115
- 66 + 55 = 121
- 25 + 55 = 80
- 46 + 55 = 101
- 42 + 55 = 97
- 60 + 55 = 115
- 66 + 55 = 121
  • Now converts These to Characters:
1
2
3
4
5
6
7
8
9
- 69 = E
- 97 = a
- 115 = s
- 121 = y
- 80 = P
- 101 = e
- 97 = a
- 115 = s
- 121 = y 

Concatenating these gives: EasyPeasy ..

[Pasted image 20241001104836.png]

# Build an Automated Python Script to Solve Our Challenge

  • Let’s Create a python script that solves our challenge automatically ….Automating little things in your daily life can teach you many concepts ….
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# The correct encoded string from the original challenge
correct = "144260662546426066"

# we will decrypt this encoded string back into the original characters.
decrypted = ""
i = 0

# Split the correct string into chunks corresponding to the original characters encoding

while i < len(correct):
	# The numbers in the encoded string are either 2 or 3 digita long , so we try both . 
	if correct[i:i+2].isdigit() and int(correct[i:i+2]) >= 10;
	# Convert the string back into the characters using the reverse formula ( add 55 )
		decrypt += chr(int(correct(i:i+2)) + 55)
		i += 2
	else 
		decrypted += chr(int(correct[i:i+3])) + 55)
		i += 3 

# Print the decrypted input 

print(f"Decrypted input: {decrypted}")

Breakdown of Decrypt.py code

1 . Initialization:

1
2
3
correct = "144260662546426066"
decrypted = ""
i = 0
  • correct: This variable stores the encoded string from the original challenge, which you need to decrypt.
  • decrypted: This variable will hold the decoded string as we process the encoded values. It starts as an empty string.
  • i: This is an index that will be used to traverse through the correct string, character by character.

2 . While Loop:

1
while i < len(correct):

This loop continues as long as i (the index) is less than the length of the correct string. It allows us to traverse the encoded string, processing it in chunks to decode the original characters.

3 . Conditional Check for Two-Digit or Three-Digit Numbers :

1
if correct[i:i+2].isdigit() and int(correct[i:i+2]) >= 10:
  • correct[i:i+2]: This takes the next 2 characters (digits) from the string starting at index i.
  • .isdigit(): This checks if these 2 characters are digits.
  • int(correct[i:i+2]) >= 10: This checks if the number formed by these two digits is greater than or equal to 10. This is because valid encoded values in the original challenge are either 2 or 3 digits long. For example, 14 is valid but 06 is not.

If the condition is true (i.e., the next two digits are a valid number), we proceed to decode them.

4 . Decoding the Character:

1
2
decrypted += chr(int(correct[i:i+2]) + 55)
i += 2
  • int(correct[i:i+2]) + 55: This converts the 2-digit number into an integer, adds 55 to it, and retrieves the original ASCII value. The +55 operation reverses the -55 that was done during encoding.
    • Example: For 14, the original value was chr(14 + 55) which gives the character 'E'.
  • chr(): This function converts the integer back into a character.
  • decrypted += ...: This appends the decoded character to the decrypted string.
  • i += 2: This increments the index i by 2, so we move to the next chunk of the encoded string.

5 . Handling Three-Digit Numbers:

1
2
3
else:    
	decrypted += chr(int(correct[i:i+3]) + 55)    
	i += 3
  • If the next 2 digits didn’t form a valid number, we assume the next chunk is a 3-digit number.
  • correct[i:i+3]: This extracts the next 3 characters from the string.
  • The decoding process is the same: we convert this number to an integer, add 55, convert it to a character using chr(), and append it to decrypted.
  • The index i is incremented by 3 to move past the 3-digit number.

6 . Final Output :

1
print(f"Decrypted input: {decrypted}")

After processing all chunks in the correct string, the decrypted variable holds the original input that matches the encoded string. This line prints the decrypted input.

Example Walkthrough:

Let’s say the correct string is "144260662546426066". Here’s how the decryption works step by step:

  1. Start with the first two digits, 14:
    • 14 + 55 = 69
    • chr(69) gives 'E'
    • decrypted is now 'E'.
  2. Next two digits, 42:
    • 42 + 55 = 97
    • chr(97) gives 'a'
    • decrypted becomes 'Ea'.
  3. Next two digits, 60:
    • 60 + 55 = 115
    • chr(115) gives 's'
    • decrypted becomes 'Eas'.
  4. The process continues until the entire string is decoded, eventually yielding the original input.

[Pasted image 20241001110414.png]

  • you can build this in the C# too , it is kind of similar to c programming language .


# Final Thoughts


I hope this space continues to be helpful in your learning journey!. If you find this blog helpful, I’d love to hear your thoughts—my inbox is always open for feedback. Please excuse any typos, and feel free to point them out so I can correct them. Thanks for understanding and happy learning!. You can contact me on Linkedin and Twitter

This post is licensed under CC BY 4.0 by the author.