📄 145-148.html
字号:
<html><head><TITLE>Learn Encryption Techniques with BASIC and C++:Transposition-based Monoalphabetic Substitution</TITLE>
<!-- BEGIN HEADER --><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><SCRIPT><!--function displayWindow(url, width, height) { var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes');}//--></SCRIPT></HEAD><body bgcolor="ffffff" link="#006666" alink="#006666" vlink="#006666"><P>
<CENTER><B>Learn Encryption Techniques with BASIC and C++</B>
<FONT SIZE="-2">
<BR>
<I>(Publisher: Wordware Publishing, Inc.)</I>
<BR>
Author(s): Gil Held
<BR>
ISBN: 1556225989
<BR>
Publication Date: 10/01/98
</FONT></CENTER>
<P>
<!-- Empty Reference Subhead -->
<!--ISBN=1556225989//-->
<!--TITLE=Learn Encryption Techniques with BASIC and C++//-->
<!--AUTHOR=Gilbert Held//-->
<!--PUBLISHER=Wordware Publishing, Inc.//-->
<!--CHAPTER=4//-->
<!--PAGES=145-148//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="141-145.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="148-152.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">The TRANSPORT Subroutine</FONT></H4>
<P>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.
</P>
<P><B>Listing 4.1</B> The TRANSPORT subroutine.</P>
<!-- CODE //-->
<PRE>
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
</PRE>
<!-- END CODE //-->
<P>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.
</P>
<P>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.</P>
<P>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.</P>
<P>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$.</P>
<P>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.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="141-145.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="148-152.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -