Exercise
Encrypter & Decrypter
Objective
Create a class "Encrypter" to encrypt and decrypt text.
It will have a "Encrypt" method, which will receive a string and return another string. It will be a static method, so that we do not need to create any object of type "Encrypter".
There will be also a "Decrypt" method.
In this first approach, the encryption method will be a very simple one: to encrypt we will add 1 to each character, so that "Hello" would become "Ipmb", and to decrypt we would subtract 1 to each character.
An example of use might be
string newText = Encrypter.Encrypt("Hola");
Write Your C# Exercise
C# Exercise Example
// Importing the System namespace which contains basic system functionalities like Console for input/output
using System;
public class Encrypter
{
// Static method to encrypt the input text
public static string Encrypt(string input)
{
// Variable to store the encrypted text
string encryptedText = "";
// Loop through each character of the input text
foreach (char c in input)
{
// Convert the character to its ASCII value and add 1 to it
encryptedText += (char)(c + 1);
}
// Return the encrypted text
return encryptedText;
}
// Static method to decrypt the input text
public static string Decrypt(string input)
{
// Variable to store the decrypted text
string decryptedText = "";
// Loop through each character of the input text
foreach (char c in input)
{
// Convert the character to its ASCII value and subtract 1 from it
decryptedText += (char)(c - 1);
}
// Return the decrypted text
return decryptedText;
}
// Main method to test the encryption and decryption
public static void Main()
{
// Original text to encrypt
string originalText = "Hola";
// Encrypt the text
string encryptedText = Encrypt(originalText);
Console.WriteLine($"Encrypted text: {encryptedText}");
// Decrypt the text
string decryptedText = Decrypt(encryptedText);
Console.WriteLine($"Decrypted text: {decryptedText}");
}
}