Caxton Foster's Bifid Analysis Tools

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
AElfstangard
Newbie
Posts: 5
Joined: Fri Jun 29, 2018 10:13 am

Caxton Foster's Bifid Analysis Tools

Post by AElfstangard »

Hi Everyone,

Hopefully, I'm not too off topic here but bare with me a bit.

I'm trying to convert the Bifid decryption tools in Caxton Foster's book<I><s></s> Cryptanalysis for microcomputers. <e></e></I> I have typed the programs as they are presented (line numbers included) in his book in Chapter 13 but, try as I may, I cannot get it to run without a bad function call (error #5) for the line Print chr$(t+64);"reverse off"; in line 770.

Here are my few ideas why it is not working:
  • There is something specific to PET BASIC I'm unfamiliar with and have not accounted for,
  • The program itself is faulty,
  • The program works only in interpreter mode but not when compiled (haven't tested that yet),
  • There is a conflict between PETSCII and ASCII,
  • There are too may GOTOs, GOSUBs and labels,
  • Some variable names are too close to each other which confused the compiler (e.g. T & T%() and lastly (taa-daa),
  • I have made a monumental stoooopid that I can't find.
Anyway, I'm attaching the program as far as I got with it and, hopefully, someone here can spot where it all falls apart. Thank you all in advance for your help. One thing that bothers me in QB64 is that there seem to be no debugging tools. Are there stand-alone programs to do that? Is there, perchance, some line number removal tools that work under Windows 10. The one I have runs only in DOS or DOSBox.

Again, thank you all for your help.

Code: Select all

1 DIM T%(1000), L1(26), L2(26), TA%(1000), TB%(1000), TC%(1000)
4 DIM TP%(50), R%(1000), C%(1000), B(11), LB(11)
5 DIM KS%(9, 9), RL%(26), CL%(26)
10 REM DETERMINE PERIOD
20 REM OPEN FILES, READ AND PURGE
25 GOSUB 90000: REM Set UP And OPEN FILES
30 GOSUB 10000: REM READ DATA FROM FILE
40 GOSUB 10200: REM REMOVE UNWANTED CHARACTERS & SPACES
41 PRINT "DO YOU KNOW THE PERIOD? TYPE Y OR N."
42 A$ = "": A$ = INKEY$: IF A$ = "" GOTO 42
43 IF UCASE$(A$) = "Y" GOTO 300
45 PRINT "PERIOD IC"
50 FOR P = 3 TO 20
    55 FOR Q = 1 TO 26: L1(Q) = 0: NEXT Q
    60 HP = INT(P / 2)
    70 OD = P - 2 * HP
    75 S = 0
    80 TA% = 0: K = 0
    90 FOR B = 0 TO J - P STEP P
        100 FOR X = 1 TO HP + OD
            110 F = T%(B + X)
            120 G = T%(B + X + HP)
            125 S = S + 1
            130 GOSUB 1000
        140 NEXT X
        150 FOR X = 1 TO HP
            160 L1(T%(X + B)) = L1(T%(X + B)) + 1
            170 L2(T%(B + P + 1 - X)) = L2(T%(B + P + 1 - X)) - 1
        180 NEXT X
    190 NEXT B
    200 IC = 0
    210 GOSUB 2000
    220 GOSUB 3000
    230 PRINT P; INT((IC * 625) + .5); INT(CS + .5): REM MODIFIED TO SHOW INTEGER VALUES ONLY
240 NEXT P
250 REM PLACE THE TIP (SUSPECTED WORD OR PHRASE ENCIPHERED WITHIN THE MESSAGE)
300 INPUT "WHAT IS THE PERIOD? "; P
320 HP = INT(P / 2)
330 OD = P - 2 * HP
340 INPUT "ENTER THE TIP -- NO BLANKS "; A$
355 TL = LEN(A$)
360 FOR I = 1 TO TL
    370 TP%(I) = ASC(MID$(A$, I, 1)) - 64: REM INDEX OMITTED IN ORIGINAL LISTING
380 NEXT I
390 REM MAKE VECTORS OF ROW AND COLUMN ELEMENTS: AR = 1, AC = 101
400 K = 1
401 REM SEE ALSO: ALTERNATE CODE ON P. 243
410 FOR B = 0 TO J - P STEP P
    415 C%(K) = T%(B + 1)
    420 FOR X = 1 TO HP + OD
        430 R%(K) = T%(B + X)
        440 R%(K + 1) = T%(B + X) - 100
        445 C%(K + 1 - OD) = T%(B + X + HP) + 100
        450 C%(K + 2 - OD) = T%(B + X + HP + 1)
        460 K = K + 2
    470 NEXT X
    480 K = K - OD
490 NEXT B
495 B(11) = 10000: FOR XX = 1 TO 10: B(XX) = 0: NEXT XX
500 IF OD = 0 GOTO 610
510 REM FIND ODD PERIOD NATURALS
520 FOR I = 0 TO B - TL
    530 C = 0
    540 FOR K = 1 TO TL
        550 IF R%(I + K) = TP%(K) THEN C = C + 1
        560 IF C%(I + K) = TP%(K) + 100 THEN C = C + 1
    570 NEXT K
    580 GOSUB 4000
590 NEXT I
600 GOTO 700
610 REM DO IDENTICALS FOR EVEN PERIOD
620 FOR I = 0 TO B - TL
    630 C = 0
    640 FOR K = 1 TO TL
        650 IF (TP%(K) = R%(I + K)) AND (TP%(K + 1) = C%(I + K)) THEN C = C + 2
    670 NEXT K
    680 GOSUB 4000
690 NEXT I
700 PRINT: PRINT "PT=" ': REM CHECK THIS ORIGINALLY 700 PRINT: PRINT "PT='
710 FOR I = 1 TO TL
    720 PRINT CHR$(TP%(I) + 64);
730 NEXT I
735 PRINT
740 FOR I = 10 TO 1 STEP -1
    745 IF B(I) = 0 GOTO 830
    750 PRINT LB(I),
    760 FOR X = 1 TO TL
        765 T = R%(LB(I) + X): IF T > 100 THEN T = T - 100: PRINT "reverse";
        770 PRINT CHR$(T + 64); "reverse off";
    780 NEXT X
    790 PRINT: PRINT ,
    800 FOR X = 1 TO TL
        805 T = C%(LB(I) + X): IF T > 100 THEN T = T - 100: PRINT "reverse";
        810 PRINT CHR$(T + 64); "reverse off";
    820 NEXT X
    825 PRINT
830 NEXT I
833 CLOSE #2
835 A$ = "": A$ = INKEY$: IF A$ = "" THEN GOTO 835
840 STOP

1000 FOR Y = 1 TO K
    1010 IF (F = TA%(Y)) AND (G = TB%(Y)) THEN TC%(Y) = TC%(Y) + 1: RETURN
1020 NEXT Y
1030 K = K + 1
1040 TA%(K) = F
1050 TB%(K) = G
1060 TC%(K) = I
1070 RETURN
2000 FOR Y = 1 TO K
    2010 IC = IC + TC%(Y) * (TC%(Y) - 1)
2020 NEXT Y
2030 IC = IC / (S * (S - 1))
2040 RETURN
3000 CS = 0
3010 FOR Z = 1 TO 26
    3015 IF L1(Z) + L2(Z) = 0 GOTO 3030
    3020 CS = CS + (L1(Z) - L2(Z)) ^ 2 / (L1(Z) + L2(Z))
3030 NEXT Z
3040 RETURN
4000 IF C < B(1) THEN RETURN
4010 FOR XX = 2 TO 11
    4020 IF C < B(XX) THEN B(XX - 1) = C: LB(XX - 1) = I: RETURN
    4030 B(XX - 1) = B(XX)
    4040 LB(XX - 1) = LB(XX)
4050 NEXT XX
4060 RETURN
10000 REM Read in the text
10010 J = 0
10020 F = 0
10030 CLS: PRINT
10040 PRINT "ENTERING TEXT FROM FILE"
10045 PRINT: PRINT
10050 LINE INPUT #1, A$
10055 PRINT A$
10060 FOR I = 1 TO LEN(A$)
    10070 X = ASC(MID$(A$, I, 1)) - 64
    10080 IF X = -17 THEN F = 1
    10090 J = J + 1
    10100 T%(J) = X
10110 NEXT I
10120 IF F = 0 GOTO 10050
10121 SLEEP 5: PRINT: PRINT
10130 J = J - 1
10135 CLOSE #1
10140 RETURN
10200 REM SQUEESE OUT THE BLANKS
10210 I = 0
10220 FOR K = 1 TO J
    10230 IF T%(K) < 1 OR T%(K) > 26 GOTO 10260
    10240 I = I + 1
    10250 T%(I) = T%(K)
10260 NEXT K
10270 J = I
10280 RETURN
90000 REM OPEN INPUT AND OUTPUT FILE
90005 SCREEN _NEWIMAGE(800, 600, 256): REM SET SCREEN PROPERTIES
90010 CLS
90020 INPUT "WHAT IS THE NAME OF THE INPUT FILE  > "; A$
90030 OPEN A$ FOR INPUT AS #1
90040 PRINT: PRINT
90050 INPUT "WHAT IS THE NAME OF THE OUTPUT FILE > "; A$
90060 OPEN A$ FOR OUTPUT AS #2
90070 RETURN
Post Reply