Find Key Length Algorithm
A program to match sections of the cipher text in an attempt to find the length of the key used to produce it.
Main
Global Constant:
1. loMatchLim: The minimum number of characters that must match before we are interested.
Local variables:
1. mesgLen: Number of characters in cipher text
2. beginMatch: Loop index which goes through cipher text one position at a time
3. firstMatch: The position in the cipher text where the first matching character to the character at beginMatch is found.
4. matchLen: The number of characters that match
Basic Algorithm:
1. Call readCipher returning the number of characters to mesgLen.
2. For Loop: beginMatch takes on [1 ® loMatchLim]
a. Assign to firstMatch the value returned from calling findFirstMatch with 3
parameters:
i. The character at position beginMatch
ii. The first position at which to look for a match: beginMatch+loMatchLim
iii. The length of the message
b. If a matching string has been found (i.e., firstMatch is greater than 0):
i. Assign to matchLen the value returned from calling findMatchLen with 2
parameters: beginMatch and firstMatch
ii. If matchLen is greater than or equal to loMatchLim then:
- Print out information about the matching strings found
- Increment beginMatch by the appropriate amount
findFirstMatch
Parameters:
1. The character for which match is sought
2. The position at which to begin looking
3. The position at which to stop looking
Local variables:
1. lookIndex: Index to iterate through positions of cipher text seeking at match
2. matchPosn: The position at which a match was found (if there was a match)
Basic Algorithm:
1. Set matchPosn to -1 (in case no match is found)
2. For Loop: lookIndex takes on [beginning position ® ending position]
a. If the character at lookIndex matches the character sought then:
i. Set matchPosn to lookIndex
ii. Exit the For Loop
b. Return the value of matchPosn
[Note: If no match was found, then this is still -1; otherwise it is the position at
which the first match was found].
findMatchLen
Parameters:
1. The position at which the first character is found
2. The position at which its matching character is found
Local variables:
1. matchLen: The number of characters that match at both positions
Basic Algorithm:
1. Set matchLen to 1
2. While Loop
with condition: the character at the first position + matchLen matches the
character at the second position + matchLen
a. Increment matchLen by 1
3. Return matchLen