Encrypter & Decrypter - C# Programming Exercise

In this exercise, you need to create a class called "Encrypter" to encrypt and decrypt text. The class will have an "Encrypt" method, which will receive a string and return another string. This method will be static, so you won’t need to create any object of type "Encrypter".

There will also be a "Decrypt" method. In this first approach, the encryption method will be very simple: to encrypt, we will add 1 to each character, so "Hola" would become "Ipmb", and to decrypt we would subtract 1 from each character. An example of use might be:

string newText = Encrypter.Encrypt("Hola");

 Category

OOP More On Classes

 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

// 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}");
    }
}

 Share this C# exercise

 More C# Programming Exercises of OOP More On Classes

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Complex numbers

    In this exercise of C#, the concept of complex numbers is introduced, which consist of two parts: the real part and the imaginary part. In an expression like a+bi (fo...

  •  Table + coffetable + leg

    In this exercise of C#, the example of tables and coffee tables is extended by adding a class called "Leg". This class should include a method named ShowData, ...

  •  Catalog

    In this exercise of C#, you are required to create a class diagram and then, using Visual Studio, develop a project with the corresponding classes for a catalog utili...

  •  Random number

    In this exercise of C#, you need to create a class called RandomNumber with three static methods. The first method, GetFloat, should return a number bet...

  •  Text to HTML

    In this exercise of C#, you need to create a class called TextToHTML, which should be able to convert several texts entered by the user into an HTML sequence. ...

  •  Class ScreenText

    In this exercise of C#, you need to create a class called ScreenText, which will display a certain text at specified screen coordinates. The class must have a ...