21.

Learn Encryption Techniques with BASIC and C++
(Publisher: Wordware Publishing, Inc.)
Author(s): Gil Held
ISBN: 1556225989
Publication Date: 10/01/98

Previous Table of Contents Next


Limiting the Effect of Errors

The meaning of an enciphered message can be adversely affected by a human typing error or a transmission or transcription error. In such situations, a mistake which adds or omits a character would jumble the decipherment of the meaning of a message. Thus, we need a mechanism to limit the effect of errors caused by humans or technology. One common mechanism has its roots dating to the development of military systems and is known as character grouping—the most common method being the placement of enciphered text into groups of five characters for transmission.

To place ciphertext into groups of five characters, we will modify the CIPHER2.BAS and CIPHER2.CPP programs by the inclusion of a new subroutine. That subroutine, appropriately named GROUPBY5, replaces the subroutine PRTOUT contained in the CIPHER2.BAS program.

The GROUPBY5 Subroutine

Listing 2.7 lists the contents of the subroutine GROUPBY5. That subroutine uses the variable L as a group counter. When invoked, the subroutine sets the value of L to 1. The FOR-NEXT loop in the subroutine prints one character at a time through the use of the MID$ function, which extracts a character from the string variable TEXT$. After a character is printed, the value of the FOR-NEXT loop index (I) divided by 5 is compared to the value of L. If the value of I/5 equals the value of L, five characters have been printed and a branch to the label 6 occurs. At that label, the PRINT statement generates a space. If the value of I/5 does not equal the value of L, a branch to the label 7 occurs and the FOR-NEXT loop terminates the current index value of the loop.

Listing 2.7 The GROUPBY5 subroutine.

 GROUPBY5:   L = 1   FOR I = 1 TO MSGLEN   PRINT MID$(TEXT$, I, 1)   IF I / 5 = L THEN GOTO 6   GOTO 7 6 PRINT ' ';   L = L + 1 7 NEXT I RETURN 


One of the more famous messages transmitted using groupings of five characters was sent by Takeo Yoshikawa, a Japanese naval ensign assigned as a consulate secretary to the Japanese consulate in Honolulu. At 6 P.M. on December 6, 1941, he sent his final message using what was known as Oite to the Japanese and PA-K2 to American codebreakers. This message contained 44 groups of five characters and was transmitted to Tokyo via RCA communications at a cost of $6.82.

The message transmitted by ensign Yoshikawa reported the arrival of an American battleship and mine sweeper into port and provided a summary of the number of ships at anchor and ships in dock in Honolulu by category—battleships, light cruisers, destroyers, and mine sweepers. The message also noted that it appeared that no air reconnaissance was being conducted by the U.S. fleet’s air arm.

Although American cryptanalysts were able to crack messages transmitted in a PA-K2 code, doing so required an average of three days. Unfortunately, the contents of this message were deciphered well after the attack on Pearl Harbor had begun.


The CIPHER3.BAS Program

Listing 2.8 contains the listing of the main body of the program CIPHER3.BAS. That program includes the subroutine GROUPBY5 which groups a line of enciphered text into groups of five characters. Figure 2.5 illustrates an example of the execution of the CIPHER3.BAS program using the alphabetic shift key B, and shows the five-character grouping of the enciphered message. If you are familiar with military systems, you may wonder why the GROUPBY5 subroutine leaves the last group with less than five characters instead of filling the group by adding X’s or some similar group terminator character. The reason for not doing so at this time is because this program only operates on one message line at a time. Thus, before you terminate an unfilled group, you should first modify our program to read and process multiple lines of plaintext.

Listing 2.8 The CIPHER3.BAS program listing.

 REM PROGRAM CIPHER3.BAS DIM PLAINTEXT$(25), CIPHERTEXT$(25) CLS        GOSUB INITIALIZE 1       INPUT "Enter UPPERCASE Alphabetic Shift Key: ", K$        FOR I = 0 TO 25        IF K$ = PLAINTEXT$(I) GOTO 2        NEXT I        PRINT "You must enter a letter from A to Z"        GOTO 1 2       REM Position I represents shift key letter GOSUB FORMCIPHER        PRINT "Enter your message in UPPERCASE:"        INPUT TEXT$        MSGLEN = LEN(TEXT$) GOSUB MSGENCIPHER        REM Print results        PRINT "Resulting enciphered message is:" GOSUB GROUPBY5 STOP INITIALIZE:        REM Initialize plaintext values        FOR I = 0 TO 25        READ PLAINTEXT$(I)        NEXT I        DATA "A","B","C","D","E","F","G","H","I","J","K","L","M","N"        DATA "O","P","Q","R","S","T","U","V","W","X","Y","Z" RETURN FORMCIPHER:        REM Routine to form CIPHERTEXT alphabet based upon defined shift key        J = I + 1        FOR K = 0 TO 25        CIPHERTEXT$(K) = PLAINTEXT$((K + J) MOD 26)        NEXT K RETURN MSGENCIPHER:        REM Convert plaintext to ciphertext        FOR I = 1 TO MSGLEN        FOR J = 0 TO 25        IF MID$(TEXT$, I, 1) = PLAINTEXT$(J) THEN GOTO 5        NEXT J 5       MID$(TEXT$, I, 1) = CIPHERTEXT$(J)        NEXT I RETURN GROUPBY5:        L = 1        FOR I = 1 TO MSGLEN        PRINT MID$(TEXT$, I, 1);        IF I / 5 = L THEN GOTO 6        GOTO 7 6       PRINT " ";        L = L + 1 7       NEXT I RETURN END 


Figure 2.5  A sample execution of the program CIPHER3.BAS.


Previous Table of Contents Next


Learn Encryption Techniques with Basic and C++
Learn Encryption Techniques with BASIC and C++
ISBN: 1556225989
EAN: 2147483647
Year: 2005
Pages: 92
Authors: Gil Held

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net