[SEAV Softwares Logo]



Home Page
Program Nook
Instructional
Open Forum
Portfolio
Visitor's Area
Connections
About the Site

Case Study Problem A4
Text Encryption
Cryptography / Intermediate level

Make a program that inputs text from the user. Change the input so that the text is encrypted. Every letter will be substituted with the letter succeeding it in the alphabet and every digit is replaced with the digit higher than it. Z is replaced by A, and 9 is replaced by 0. All other characters will stay the same. Lettercase must be preserved. As an added challenge, you cannot use any IF...THENs, you have to use SELECT CASE with only three branches. Plus, the program must be written in under 15 lines of code. The encrypted text will be displayed afterwards.

Examples:
AaBbCcXxYyZz  BbCcDdYyZzAa
9:01 pm  0:12 qn
SEAV Softwares  TFBW Tpguxbsft
Sept. 25, 1998  Tfqu. 36, 2009

Download the A4 Zip file containing this page plus QBASIC files for you to try.

Go back to the Case Study page.



The Solution


Structuring the program. Before doing anything else, let's first construct the structure of the program. Going by the problem, we could follow this structure:
Initialization

Input string to be encrypted

Construct the encrypted string

Scan each character
Encrypt character if it's a letter or a number
Add encrypted character

Print encrypted string
You follow that? Good. Cause now we're going to write the code to the four outer steps.
' Initialization
CLS

' Input string to be encrypted
LINE INPUT "Enter text: ", Text$

' Construct the encrypted string
.
.
.

' Print the encrypted string
PRINT "Encrypted text: "; Text$

We used LINE INPUT since the text could have commas in it. We leave the construction of the encrypted string for later since this is where the bulk of the programming is located and needs a more comprehensive discussion.

To build the encrypted string. There are two ways that I can see in building the encrypted string. Which one you use depends on whether you want to retain the original string. The first method is that we build a new string from the original one, thus preserving the original string (just in case you still need it). In the second method, we modify the original string itself, completely replacing its content when the program ends. For both methods, we go through the string on a character-by-character basis, and making the necessary changes to each character (replacing an "A" with a "B", a "g" with an "h", and so on).
Here is the first method (new string):
' Construct the encrypted string
FOR I = 1 to LEN(Text$)
  Char$ = MID$(Text$, I, 1)

  ' Here's where the character is modified
  .
  .
  .

  ' Concatenate character to end of new string
  New$ = New$ + Char$

NEXT

And here is the second method (replacement):
' Construct the encrypted string
FOR I = 1 to LEN(Text$)
  Char$ = MID$(Text$, I, 1)

  ' Here's where the character is modified
  .
  .
  .

  ' Replace character in original string
  MID$(Text$, I, 1) = Char$

NEXT

The second method requires that you use the statement form of MID$. The statement form enables us to replace parts of a string without changing the rest of its content or its length.

Encrypting the characters. Now the only thing left is the encryption process. Since we're dealing with text here and changing individual characters based on their order, we may need to use the ASCII functions, ASC and CHR$. If we have a letter contained in a string variable Char$, we can find the next character in the alphabet by simply getting the ASCII number of the character, adding one to it, and obtaining the character with that number. This process can also be applied to numerals.
Char$ = CHR$(ASC(Char$) + 1)

However, we would have problems if the letter is Z or the number is 9. The next ASCII character to upper-case z is a left square bracket, [, while the characters after lower-case z and 9 are the left curly bracket, {, and the colon, :, respectively. What we can do is to take them as special cases.
So we apply the ASCII code above to each character in the original string, but before we do so, we make sure that they're letters and numbers first (we shouldn't encrypt the remaining characters such as periods and commas).
' Here's where the character is modified
SELECT CASE Char$
CASE "0" TO "8", "A" TO "Y", "a" TO "y"
  Char$ = CHR$(ASC(Char$) + 1)
CASE "9"
  Char$ = "0"
CASE "Z"
  Char$ = "A"
CASE "z"
  Char$ = "a"
END SELECT

Note that we included the special cases. Also, we used SELECT CASE instead of a bunch of IF...THENs since the problem stated that.
This is almost finished but we have one too many CASE branches. We can actually combine the last two branches and simplify the conditions:
' Here's where the character is modified
SELECT CASE UCASE$(Char$)
CASE "0" TO "8", "A" TO "Y"

  Char$ = CHR$(ASC(Char$) + 1)
CASE "9"
  Char$ = "0"
CASE "Z"
  Char$ = CHR$(ASC(Char$) - 25)
END SELECT

That's it!

Finally, the completed program! Your final program should look something like the program in Listing A4.1. Here, I used the second method of building the encrypted string since I don't really need the original.
Listing A4.1


' Initialization
CLS

' Input string to be encrypted
LINE INPUT "Enter text    : ", Text$

' Construct the encrypted string
FOR I = 1 to LEN(Text$)
  Char$ = MID$(Text$, I, 1)

  ' Here's where the character is modified
  SELECT CASE UCASE$(Char$)
  CASE "0" TO "8", "A" TO "Y"
    Char$ = CHR$(ASC(Char$) + 1)
  CASE "9"
    Char$ = "0"
  CASE "Z"
    Char$ = CHR$(ASC(Char$) - 25)
  END SELECT

  ' Replace character in original string
  MID$(Text$, I, 1) = Char$
NEXT

' Print the encrypted string
PRINT "Encrypted text: "; New$

Here's a sample output:
Enter text    : AaBbYyZz...123890!
Encrypted text: BbCcZzAa...234901!

A different algorithm. You'll find on Listing A4.2 another program that answers our problem. This one uses a completely different method of encryption and it also uses integer math instead of string functions, which are notoriously slow. Using integer math is really necessary for speed especially if you decide to use the encryption on a long string, or even whole text files.
Listing A4.2


' Initialization
DEFINT A-Z
CLS

' Input string to be encrypted
LINE INPUT "Enter text    : ", Text$

' Construct the encrypted string
FOR I = 1 TO LEN(Text$)
  Char = ASC(MID$(Text$, I, 1))

  ' Here's where the character is modified
  SELECT CASE Char
  CASE 65 TO 90       'numerals
    Char = (Char - 64) MOD 26 + 65
  CASE 97 TO 122      'lowercase letter
    Char = (Char - 96) MOD 26 + 97
  CASE 48 TO 57       'uppercase letters
    Char = (Char - 47) MOD 10 + 48
  END SELECT

  ' Replace character in original string
  MID$(Text$, I, 1) = CHR$(Char)
NEXT

' Print the encrypted string
PRINT "Encrypted text: "; Text$

Flexible programming. As you have probably noticed by now, there is more than one way of making programs for a problem. Programming is a very flexible discipline, in the sense that it lets you do anything you want. Of course, you should pick the most efficient solution.
Another way of solving the problem is by using a look-up table. This can be either a 256-integer array, a 256-character array, or a string of length 256. To use this method, you scan each character finding its ASCII number and referencing the look-up table to get the modified character. For the integer array, the elements will contain the ASCII code of the new character. So for our problem, for example, Array(65) = 66. The string array will contain the character itself, not its ASCII code. The same goes for the string variable.
This method of using look-up tables is very flexible. The two programs I showed earlier were very specific. With look-up tables, you can change encryption systems on the fly. You can even have two tables: one for encoding and the other for decoding.

Further Challenges


Create a program that reverses the process of the original program—a decoder. Create a program that uses look-up tables to encrypt text files (make your own encoding/decoding tables).
For the file encryption program, you can either open the file as BINARY and changing each printable character or open the file as INPUT, and manipulating successive lines (loading each using LINE INPUT). Obviously, you need to output the results into a new file.



Go back to the Case Study page.

Home Page | Program Nook | Instructional | Open Forum
Portfolio | Visitor's Area | Connections | About the Site

Copyright © 1997-1999, SEAV Softwares. All rights reserved.
Webmaster: Eugene Villar (SEAV); e-mail: [email protected]