Professor Mustard Programming
 

Programming 101 - Unit 01 - The Basics

01f) Project Encryption
 
So, at this point, we've learned about console input and output, variable management, math and string manipulation. Let's create a simple application that uses all of those things.

We want to create a program that will accept a four-digit number from the user, and encrypt its value. What's that? You're saying that we never learned about encryption? Well, true enough. But we have learned about string manipulation, and that should give us everything we need for a basic example.

We'll have to start by getting the number from the user. This should be second nature to you by now:

Console.Write("Please enter the 4-digit number to encrypt: ");
string rawNumber = Console.ReadLine();



Why a string, and not an int? Because we want to perform a simple encryption calculation on each digit in the number, and an easy way to isolate each digit is by using Substring(), like so:

int digit1 = Convert.ToInt32(rawNumber.Substring(0,1));
int digit2 = Convert.ToInt32(rawNumber.Substring(1,1));
int digit3 = Convert.ToInt32(rawNumber.Substring(2,1));
int digit4 = Convert.ToInt32(rawNumber.Substring(3,1));



Okay. We've got each digit in its own variable. Now, what do we do with them? Well, for the purpose of this exercise, let's assume that we want to "adjust" the value of each digit by four. That is, a 4 would become an 8, an 5 would become a 9, a 6 would become a 0, and so on. So, if we started with the number 1236, we want the end result to be 5670. Well, there's an easy way to accomplish this using modulus. Because every digit can have 10 possible values (0-9), and we want to adjust the digit's value by 4, all we need to do is this:

digit1 = (digit1 + 4) % 10;
digit2 = (digit2 + 4) % 10;
digit3 = (digit3 + 4) % 10;
digit4 = (digit4 + 4) % 10;



For each digit, I've added 4 to its original value. The % 10 chops off the "tens" value. So if the number was 11 before modulus 10, the number will now be 1. If the number was 13 before modulus 10, the number will now be 3. And if the number was 9 or under before modulus 10, it will remain exactly the same. In this manner, we "bump" every digits value by 4, and "wrap around" to 0 if the digit's value exceeds 10. Regrettably, I'm not a mathematician, so that's the best job I can do of explaining it. Hope it makes sense.

Alright, now each digit we've stored is encrypted. How do we get them back into one number? Well, one way would be to add them all together on a string, and then convert the string to an integer:

rawNumber = digit1.ToString() + digit2.ToString() + digit3.ToString() + digit4.ToString();
int convertedNumber = rawNumber.ToString();



You might wonder why we have to call ToString() on each digit as we're adding it. Well, what normally happens when you use the "+" operator on two integers? It adds their values, of course! But if our digits are 1, 2, 3 and 4, our final number should be 1234... not 10, which is what you'd get by adding them together. So we use ToString() in order to force C# to interpret our "+" signs as string addition, not integer addition. That being said, however, there's an even more direct way of going from individual digits to one number:

int convertedNumber = (digit1*1000) + (digit2*100) + (digit3*10) + digit4;


If our digits are 1, 2, 3 and 4, then the above math would translate into this:

int convertedNumber = (1000) + (200) + (30) + 4;


Long live the base 10 numbering system. And that's all there is to encrypting a four digit number. Decrypting it is almost the same, except we'd do this with our encrypted digits:

digit1 = (digit1 + 6) % 10;
digit2 = (digit2 + 6) % 10;
digit3 = (digit3 + 6) % 10;
digit4 = (digit4 + 6) % 10;



Why are we adding 6? Simply because we've already added 4, and adding 6 more will result in a complete trip around our 10-value digits (Remember the "wrap-around" effect that this formula produces when a number exceeds 9?) If you don't believe me, create another integer from these newly "decrypted" digits, and see if you don't end up with your original number.

int decryptedNumber = (digit1*1000) + (digit2*100) + (digit3*10) + digit4;
Console.WriteLine("Decrypted Value: " + decryptedNumber);



Of course, this program has many limitations. It relies on the user entering a four digit number to operate perfectly. If the user enters less than four digits, the program crashes. If they enter a non-number, the program crashes. And there's no way to encrypt a new number without running the program again. Yes, the time has come to move on from these linear exercises to programs that use actual logic, and can take different paths in their execution, depending on what the user does. We'll learn all this and more in the next unit, so stay tuned!

In the meantime, here's a code sample of the encryption program:



using System;

namespace F_ProjectEncrpyt
{
  class Class1
  {
    [STAThread]
    static void Main(string[] args)
    {
      // Prompt for number
      Console.Write("Please enter the 4-digit number to encrypt: ");
      string rawNumber = Console.ReadLine();
      Console.WriteLine(Environment.NewLine + " Original value: " + rawNumber);

      // Store each digit
      int digit1 = Convert.ToInt32(rawNumber.Substring(0,1));
      int digit2 = Convert.ToInt32(rawNumber.Substring(1,1));
      int digit3 = Convert.ToInt32(rawNumber.Substring(2,1));
      int digit4 = Convert.ToInt32(rawNumber.Substring(3,1));

      // "Shift" each digit's value by 4 (4 becomes 8, 5 becomes 9, 6 becomes 0,      etc...)
      digit1 = (digit1 + 4) % 10;
      digit2 = (digit2 + 4) % 10;
      digit3 = (digit3 + 4) % 10;
      digit4 = (digit4 + 4) % 10;

      // Use the digits to store our encrypted number
      int convertedNumber = (digit1*1000) + (digit2*100) + (digit3*10) + digit4;
      Console.WriteLine("Encrypted Value: " + convertedNumber);

      // "Shift" each digit's value by 6 (put them back in their original values)
      digit1 = (digit1 + 6) % 10;
      digit2 = (digit2 + 6) % 10;
      digit3 = (digit3 + 6) % 10;
      digit4 = (digit4 + 6) % 10;

      // Use the digits to store our decrypted number
      int decryptedNumber = (digit1*1000) + (digit2*100) + (digit3*10) + digit4;
      Console.WriteLine("Decrypted Value: " + decryptedNumber);

      // Prompt for exit
      Console.Write(Environment.NewLine + "Program complete! Hit enter to exit...");
      Console.ReadLine();
    }
  }
}