40.

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


The TRANSPORT Subroutine

Listing 4.1 shows the contents of the subroutine labeled TRANSPORT, which creates either a simple or numeric transposition mixed sequence alphabet. This subroutine includes several unnecessary PRINT statements to illustrate how the program operates. Similar to the previous use of extraneous PRINT statements, I have identified those statements with comments consisting of five asterisks to facilitate their removal.

Listing 4.1 The TRANSPORT subroutine.

 TRANSPORT:    REM Routine to form simple or numeric transposition mixed sequence    REM Initialize 26 by 26 matrix to nulls        POSITION = 0        FOR I = 0 TO 26        FOR J = 0 TO 16        TRANS$(I, J) = ""        NEXT J, I    REM Determine actual matrix size        COLUMN = LEN(KEYCOL$)        ROW = 26 / COLUMN - INT(26 / COLUMN)        IF ROW > 0 THEN ROW = (INT(26 / COLUMN) + 1) ELSE ROW = 26                        / COLUMN    REM Place keyword-based alphabet into matrix elements        PRINT "MATRIX IS:": PRINT        FOR I = 0 TO ROW - 1        FOR J = 0 TO COLUMN - 1        IF POSITION > 25 THEN GOTO ALLDONE        TRANS$(I, J) = PLAINTEXT$(POSITION)        PRINT TRANS$(I, J);        '*****        POSITION = POSITION + 1        NEXT J        PRINT                      '***** ALLDONE:   NEXT I        PRINT: PRINT               '*****    IF ORDER$ = "NUMERIC" GOTO NUMERIC    REM Form simple transposition-based mixed alphabet        X$ = ""        FOR I = 0 TO COLUMN - 1        FOR J = 0 TO ROW - 1        X$ = X$ + LTRIM$(TRANS$(J, I))        NEXT J        NEXT I    REM Place into PLAINTEXT array        FOR I = 0 TO 25        PLAINTEXT$(I) = MID$(X$, I + 1, 1)        NEXT I    RETURN    REM Form numeric transposition-based mixed alphabet NUMERIC:   FOR I = 0 TO COLUMN - 1        TEMP$(I) = TRANS$(0, I)'place in temporary storage first row        NEXT I        FOR I = 0 TO COLUMN - 1'begin sort        FOR J = I + 1 TO COLUMN - 1        IF TEMP$(I) > TEMP$(J) THEN SWAP TEMP$(I), TEMP$(J)        NEXT J        NEXT I 'TEMP$ array now contains sorted order of characters    REM Compare sorted keyword to first column in matrix, extract    REM when they match by getting row contents        X$ = ""        FOR I = 0 TO COLUMN - 1'        FOR J = 0 TO COLUMN - 1        IF TEMP$(I) <> TRANS$(0, J) GOTO NMATCH        FOR K = 0 TO ROW - 1        'match, get row contents        X$ = X$ + LTRIM$(TRANS$(K, J))        NEXT K NMATCH:    NEXT J        NEXT I    REM Place into PLAINTEXT array        FOR I = 0 TO 25        PLAINTEXT$(I) = MID$(X$, I + 1, 1)        NEXT I RETURN 

The first group of statements in the subroutine TRANSPORT initializes a 26x26 string matrix labeled TRANS$ to nulls. This matrix permits a keyword or keyword phrase that can be up to 26 characters in length. The second group of statements determines the actual size of the matrix. Here, the value of COLUMN is the length of the keyword in characters. Thus, a keyword consisting of five characters would require a matrix of five columns and six rows for all 26 letters of the alphabet to fit into the matrix.

The third group of statements places the characters of a keyword-based alphabet into the string array TRANS$. Note that the values for the indices for the nested FOR-NEXT loops are adjusted by subtracting 1 from the values of ROW and COLUMN because the matrix elements commence at position 0,0. Also, note values are assigned to TRANS$ from the string array PLAINTEXT$. The array PLAINTEXT$ contains the keyword developed alphabet which was filled by the prior invoking of the previously described KEYWORD subroutine. You must invoke KEYWORD prior to invoking TRANSPORT. To facilitate passing information to determine the number of columns in the matrix, the statement KEYCOL$=X$ was added to the end of the third group of statements in the KEYWORD subroutine.

The PRINT statements included in the third group of statements simply display the matrix for your review and will be removed from the subroutine later in this chapter. The IF statement permits the selection of a simple or numeric-based mixed alphabet. If the string variable ORDER$ does not equal “NUMERIC,” the simple transposition-based mixed alphabet is formed. This occurs in the fourth statement group which simply adds each character in every row of each column to the string X$ in column sequence. The resulting simple transposition-based mixed alphabet is placed into the string array PLAINTEXT$ and the subroutine ends.

If the user wants a numeric transposition-based mixed alphabet, a branch to the label NUMERIC in the subroutine occurs. At this location, the first character in each column is placed into temporary storage in the string array TEMP$. Next, the contents of TEMP$ are sorted, resulting in that array containing the characters of the keyword in sorted order. This is followed by another group of statements which cycles through the elements of the TEMP$ array. Each element of that array is matched against the first character in each column of the TRANS$ array. When a match occurs, the contents of the column are extracted and added to the string X$, which results in the formation of a numeric transposition mixed sequence alphabet. The last group of statements places the resulting alphabet into the string array PLAINTEXT$.

Listing 4.2 illustrates the modified calling sequence used to invoke the subroutine TRANSPORT. This calling sequence will display the formed matrix, ciphertext alphabet, and plaintext alphabet to enable you to verify the operation of the subroutine. To enable you to use the calling sequence and the TRANSPORT subroutine, the code has been stored on a file labeled CIPHERTR.BAS on the companion CD. The PRINT statements that display the matrix and alphabets will be removed when you develop the program CIPHER6.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