home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / WP / WPUNPASS.ZIP / DECRYPT.BAS < prev    next >
BASIC Source File  |  1993-09-18  |  8KB  |  333 lines

  1.                            'DECRYPT.BAS
  2.                     'Version 1.0  July 4, 1993
  3.                     'Copyrighted 1993, by Gabriel Fineman
  4.                     'Donated as FREEWARE
  5.                     'See details under Help
  6.                     'Written in Turbo Basic
  7.                     'From KFIX (a Mass-11 utility by the
  8.                     ' same Author)
  9.  
  10.        DEFINT A,Z        'Default all variables to integer
  11.  
  12.     CLS            'Clear screen
  13.     
  14.     'Get and test command line    
  15.  
  16.     T$ = COMMAND$
  17.     IF T$ = "" THEN
  18.                GOSUB GetInput            'Interactive with User
  19.        ELSE
  20.         GOSUB COMMANDLINE        'Non-Interactive
  21.         END IF
  22.     GOSUB INITIALIZE
  23.  
  24. Test:
  25.     FOR I = 2 TO 23                'Max key length
  26.         LOCATE 7                'On line seven
  27.         PRINT "TESTING KEY LENGTH " I           'Keep user informed                  
  28.     C = 0                    'count of letters found
  29.     R$ = ""                                 'accumulate letters found
  30.     FOR J  = 1 TO I                         'for each char in this key length
  31.         J1 = J
  32. TestA:                        'Convert a first time
  33.         JA  = I+J1                          'constant
  34.         IF J1 > 30 THEN GOTO TestB          'too large to test
  35.  
  36.         IF FNSKIP(J1) THEN                  'if variable information
  37.         J1 = J1+I                       'try next multiple
  38.         GOTO TestA
  39.         END IF
  40.         T1 = ( K(J1) XOR (JA) ) XOR E(J1)    'algorthym
  41.  
  42.         J2 = J1+I
  43.         JB = J2+I
  44. TestB:                               'Convert a second time
  45.         IF J2 > 30 THEN GOTO TestC
  46.         IF FNSKIP(J2) THEN
  47.         J2 = J2+I
  48.         JB = JB+I
  49.                 GOTO TestB
  50.         END IF
  51.  
  52.         T2 = ( K(J2) XOR (JB) ) XOR E(J2)
  53.          GOTO Printing
  54.  
  55. TestC:                        'Unable to convert 2nd time
  56.         IF (LEN(R$)>0) AND (MID$(R$,LEN(R$),1) <> "?") THEN T2=T1
  57.  
  58. Printing:
  59.         IF T1=T2     THEN            'if conversions the same
  60.         R$ = R$ + CHR$(T1)        'must be correct
  61.         C = C+1
  62.         END IF
  63.  
  64.         IF T1<>T2     THEN R$ = R$ + "?"    '? if different conversions
  65.  
  66.         NEXT
  67.  
  68.     IF C*2 > I THEN                'if more than 1/2 found
  69.         LOCATE Row
  70.         Row = Row+1
  71.         IF I<10 THEN                    'show password
  72.                 PRINT "Key length  " I " has a password of " R$
  73.             D = D+1
  74.          ELSE
  75.                 PRINT "Key length "  I " has a password of " R$
  76.             D = D+1
  77.         END IF
  78.     END IF
  79.  
  80.     NEXT                    'End of main loop
  81.  
  82.         LOCATE 7
  83.         PRINT "                                                           "
  84.         LOCATE ROW
  85.         IF D = 0 THEN PRINT "NO LIKELY KEYS FOUND"
  86.     LOCATE ROW+2
  87.     INPUT "Press any key to exit", Z$
  88.     GOTO ENDING0
  89.  
  90. ERROR1:
  91.     LOCATE 9
  92.     PRINT "NOSUCH FILE AS " S$
  93.     INPUT "Press any key to exit", Z$
  94.     RESUME ENDING0
  95.  
  96.  '_______________________________________________
  97.  
  98.         'GOSUB SUBROUTINES
  99.  '_______________________________________________
  100.  
  101.  
  102.  
  103. COMMANDLINE:        'GOSUB subroutine to parse and set from command line
  104.     T$ = UCASE$(T$)
  105.     INTERACTIVE = 0
  106.     WHILE LEFT$(T$,1) = " "            ' Flush left
  107.         T$ = MID$(T$,2)
  108.      WEND            
  109.     I = INSTR (T$," ")            'Strip file name
  110.     J = INSTR (T$,"/")
  111.     IF I=J THEN
  112.         K=999
  113.        ELSE    
  114.         IF I>J THEN
  115.             IF J>0 THEN K=J ELSE K=I
  116.            ELSE
  117.             IF I>0 THEN K=I ELSE K=J
  118.            END IF
  119.      END IF
  120.     S$ = LEFT$(T$,K)            'File name into S
  121.     PRINT "Finding password for file " S$
  122.     T$ = MID$(T$,K)                'Options into T
  123.  
  124.     RETURN
  125.  
  126.  '-----------------------------------------------
  127.  
  128.  
  129. GETINPUT:        'GOSUB subroutine to get interactive input
  130.         CLS
  131.     PRINT TAB(30); "DECRYPT PASSWORD"
  132.     PRINT
  133. GetInput1:
  134.        PRINT "Enter ? for help"
  135.        PRINT
  136.        INPUT "Enter name of encrypted file: ",S$
  137.        IF S$ = "" THEN GOTO ENDING0
  138.        IF S$ = "?" THEN
  139.                GOSUB HelpIndex
  140.         GOTO GetInput
  141.     END IF
  142.        S$ = UCASE$(S$)
  143.        RETURN
  144.  
  145.  '-----------------------------------------------
  146.  
  147.  
  148. HELPINDEX:            'GOSUB subprogram to give online help
  149. Help1:
  150.     SK=1               'Get index screen #1
  151.     GOSUB GetScreen
  152.  
  153.     INPUT "Return to continue",OK$
  154.     RETURN
  155.  
  156.  '-----------------------------------------------
  157.  
  158. INITIALIZE:
  159. 'print "fnexists(" S$ ") = " FNEXISTS(S$)
  160.      'IF NOT FNEXISTS(S$) THEN GOTO ERROR1    'See if good name
  161.         ON ERROR GOTO ERROR1
  162.     OPEN S$ FOR BINARY AS #1        'Open file to decrypt
  163.     ON ERROR GOTO ERROR0
  164.  
  165.     GET$ #1, 50, A$             'Get first 50 bytes
  166.  
  167.     CK$ = MID$(A$,1,4)            '[FF]WPC
  168.     IF CK$ = "" OR_
  169.       (ASC(MID$(CK$,1,1))=&HFF AND_
  170.        ASC(MID$(CK$,2,1))=&H57 AND_
  171.        ASC(MID$(CK$,3,1))=&H50 AND_
  172.        ASC(MID$(CK$,4,1))=&H43) THEN
  173.      ELSE
  174.         PRINT S$ " is not a WordPerfect file, let alone encrypted."
  175.                 INPUT "Press any key to exit", Z$
  176.             GOTO ENDING0
  177.         END IF
  178.  
  179.     CK$ = MID$(A$,13,4)            'Key checksum
  180.     IF CK$ = "" OR_
  181.       (ASC(MID$(CK$,1,1))=&H00 AND_
  182.            ASC(MID$(CK$,2,1))=&H00 AND_
  183.        ASC(MID$(CK$,3,1))=&H00 AND_
  184.            ASC(MID$(CK$,4,1))=&H00) THEN
  185.         PRINT S$ " is not an encrypted WordPerfect file."
  186.         INPUT "Press any key to exit", Z$
  187.         GOTO ENDING0
  188.     END IF
  189.  
  190.     DIM N(1:8)                    'Numbers to skip
  191.         N(1) =  7
  192.         N(2) =  8
  193.         N(3) =  11
  194.         N(4) =  12
  195.         N(5) =  13
  196.         N(6) =  21
  197.         N(7) =  23
  198.         N(8) =  27
  199.  
  200.     DIM  E(1:48)                'Encrypted constants
  201.     FOR I = 17 TO 46
  202.         E(I-16) = asc(MID$(A$,I,1))
  203.     NEXT
  204.  
  205.  
  206.     DIM  K(1:48)                'Known constants
  207.         K(1)  = &HFB
  208.         K(2)  = &HFF
  209.         K(3)  = &H05
  210.         K(4)  = &H00
  211.         K(5)  = &H32
  212.         K(6)  = &H00
  213.         K(7)  = &H00    'Skip
  214.                 K(8)  = &H00    'Skip
  215.         K(9)  = &H00
  216.         K(10) = &H00
  217.         K(11) = &H07    'Skip
  218.         K(12) = &H00    'Skip
  219.                 K(13) = &H00    'Skip
  220.         K(14) = &H00
  221.         K(15) = &H00
  222.         K(16) = &H00
  223.         K(17) = &H42
  224.         K(18) = &H00
  225.                 K(19) = &H00
  226.                 K(20) = &H00
  227.                 K(21) = &H02    'Skip
  228.                 K(22) = &H00
  229.                 K(23) = &H56    'Skip
  230.                 K(24) = &H00
  231.                 K(25) = &H00
  232.                 K(26) = &H00
  233.         K(27) = &H00    'Skip
  234.                 K(28) = &H00
  235.                 K(29) = &H00
  236.                 K(30) = &H00
  237.     row = 9
  238.     D = 0
  239.     GOSUB InitializeScrArray
  240.  
  241. RETURN
  242.  
  243. '-----------------------------------------------    
  244.  
  245. GetScreen:                'GOSUB to display screen from file
  246.     CLS
  247.     GOSUB InitializeScrArray
  248.     PRINT SCR$(SK)
  249.         RETURN
  250.  
  251.  '-----------------------------------------------
  252.  
  253.     
  254. InitializeScrArray:        'GOSUB routine to initialize help matrix
  255.     DIM STATIC SCR$(2)        'Fast and short but hard to space
  256.     LFCR$ = CHR$(10)+CHR$(13)
  257.     SN$ = LFCR$+SPACE$(10)
  258.     SS$ = CHR$(10)
  259.     SI$ = SS$+SPACE$(15)
  260.  
  261.     SCR$(1) = SPACE$(20)+"WordPerfect Decryption Passwords"+SN$_
  262.         +"When you forget your password this program can help "_
  263.         +"recover it." + SS$ _
  264.         + "It tests possible key lengths of 1-23.  If it can "
  265.     SCR$(1) = SCR$(1) + "decypher at least half of the characters, "_
  266.         +"it shows you the possible key.  If the same letter is "_
  267.         +"shown twice ina row, it may not be correct the second "_
  268.         +"time."
  269.     SCR$(1) = SCR$(1) + "  Hopefully, you will get enough toguess the "_
  270.         +"password."+SN$
  271.     SCR$(1) = SCR$(1) + "The program will accept a file name on the "_
  272.         +"command line.  If none is  provided, it asks you for the "_
  273.         + "file name." + SN$_
  274.         +"This program is based on a 1990 article by Ms. Helen Bergen "_
  275.         +"at the" + SS$ + "Queensland University, reachable at "
  276.     SCR$(1) = SCR$(1) + "<CSZBERGEN@qut.edu.au> on the Internet." + SN$
  277.     SCR$(1) = SCR$(1) + "The program was written by Gabriel Fineman in "_
  278.         +"July of 1993 and is     designated by the author as "_
  279.         +"FREEWARE that may be used and modified by anyone forany "_
  280.         +"purpose.  The author may be reached on GENIE as "_
  281.           +"<GFINEMAN@genie.geis.com>." + LFCR$
  282.     RETURN
  283.  
  284.  '_______________________________________________
  285.  
  286.             'FUNCTIONS
  287.  '_______________________________________________
  288.  
  289.    DEF FNSKIP (i)        'Test if to skip this i
  290.        LOCAL J
  291.        FNSKIP = 1
  292.        FOR J = 1 TO 8
  293.            IF N(J)=I THEN GOTO SKIPEND
  294.        NEXT
  295.        FNSKIP = 0
  296.    SkipEnd:
  297.    END DEF
  298.  
  299.  
  300.    DEF FNEXISTS (N$)           'Test if N$ exists
  301.        FNEXISTS = 0
  302.        ON ERROR   GOTO ExistsR
  303.        OPEN N$ FOR INPUT AS #9
  304.        CLOSE #9
  305.        FNEXISTS=1
  306.        GOTO Exists
  307.  
  308. ExistsR:
  309.     ON ERROR GOTO 965
  310.     RESUME Exists
  311.  
  312. Exists:
  313.    END DEF
  314.  
  315. '________________________________________________
  316.  
  317.         'Error Trapping Routines
  318. '________________________________________________
  319.  
  320. 964:
  321. 965:
  322. Error0:
  323.     Print "Error trap ERR at line ERL"
  324.     RESUME  ENDING0
  325.  
  326.  '_______________________________________________
  327.  
  328.         'EXIT FROM PROGRAM
  329.  '_______________________________________________
  330.  
  331. ENDING0:
  332.     RESET
  333.     END