home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / b / bcrypt10.zip / BCRYPT.DOC < prev    next >
Text File  |  1993-02-01  |  15KB  |  428 lines

  1.  
  2.         ┬┬─┐  ┌┬──┐ ┬┬──┐ ┬  ┬┬ ┬┬──┐ ┌─┬┬─┐
  3.         │├─┴┐ ││    │├─┬┘ └──┤│ │├──┘   ││
  4.         ┴┴──┘ └┴──┘ ┴┴ ┴─ └──┴┘ ┴┴      ┴┴
  5.  
  6.         |
  7.         |   Quick Basic Encryption/Decryption Library
  8.         |   and Cryptosystems.
  9.         |
  10.         |   Version 1.0
  11.         |   01 February 1993
  12.         |
  13.         |   Copyright 1993, Mountain Bay Software,
  14.         |                   J. Derek Lyons
  15.         |
  16.  
  17.         --------- WARNING ---------
  18.         It may violate US Customs Regulations to export this
  19.         document, program files, or programs using elements of
  20.         this system.
  21.         It is the responsibility of the end user to determine
  22.         the legality or illegality of any use or export, the
  23.         Author(s) take no responsibility for such actions.
  24.         --------- WARNING ---------
  25.  
  26.         Mountain Bay Software makes no warranty of any kind, either
  27.         express or implied, including but not limited to implied
  28.         warranties of merchantability and fitness for a particular
  29.         purpose, with respect to this system and accompanying
  30.         documentation.
  31.  
  32.         IN NO EVENT SHALL MOUNTAIN BAY SOFTWARE BE LIABLE FOR
  33.         ANY DAMAGES (INCLUDING DAMAGES FOR LOSS OF PROFITS, LOSS OF
  34.         INFORMATION, OR OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF
  35.         OR INABILITY TO USE THIS SYSTEM.
  36.  
  37.         Mountain Bay Software specifically does not guarantee that
  38.         any additional security will be gained from using these
  39.         routines.  Full security depends on far more then just an
  40.         encryption system.
  41.  
  42.  
  43.  
  44.         Contacting the Author;
  45.         I can be contacted via the following methods;  Use the one
  46. most convenient for you:
  47.  
  48.         US MAIL:          Mountain Bay Software
  49.                           Derek Lyons
  50.                           2300 Winfield #4
  51.                           Bremerton, WA
  52.                                    98310
  53.                           1-206-479-6123
  54.         INTERNET:         elde@aol.com
  55.         America Online:   ELDE
  56.         RIME Conferences: QuickBasic, Programming, Shareware.
  57.                           (R/O to TSCNET)
  58.         FIDO conferences: Quick Basic,BASIC7
  59.         Intellec:         Programming, Shareware.
  60.         Support BBS:      TSCNET 1-206-692-2388, Conference #11
  61.  
  62.  
  63.  
  64. Introduction:
  65.         This package is intended to provide access to cryptographic
  66. functions for Quick Basic and PDS BASIC programmers.  BCRYPT.LIB is
  67. primarily designed to allow for the encryption of game save files,
  68. journals, passwords, registration information, etc.  However it is
  69. possible to implement a complete cryptosystem using the core routines.
  70.  
  71. Memory considerations:
  72.         Routines contained in this library are operating on strings.
  73. This means that they will consume memory in the string area.
  74. Therefore be careful about passing extremely long strings into the
  75. routines.
  76.  
  77.         Since in most cases at least one copy of the string will be
  78. made in the routine, it is easy to eat memory up quick.  A combination
  79. of a long string and a long key could cause the program to crash.  If
  80. you must encrypt an extremely long string, I reccomend you use a
  81. routine that generates the key as it goes, or split the string into
  82. smaller portions and encrypt each one seperately.
  83.  
  84.         Mathematical functions within the routines all operate on
  85. integers, all variables are explicitly defined as integers.
  86.  
  87. Security considerations:
  88.         Using this system alone will not guarantee beyond minimal
  89. security.
  90.  
  91. Design philosophy:
  92.         Each routine provided in the BCRYPT.LIB is a 'primitive'.
  93. That is to say, the routine is designed to operate at the lowest
  94. possible level.  They are meant to provide a building blocks for
  95. more complex functions.
  96.  
  97.  
  98.  
  99. The routines in the library are broken up into the following
  100. functional groups;
  101.  
  102. Password or Key creation;
  103.         Argen% (Rseed$)
  104.         MakeAlpha$
  105.         MakeAlphaArray (A$())
  106.         MakeAlphaNum$
  107.         MakeAlphaNumArray (A$())
  108.         MakeLong$
  109.         MakeLongArray (A%())
  110.         MakePass (tf%, lf%, pass$)
  111.         MKAlpha$ (Key$)
  112.         MKAlphaNum$ (Key$)
  113.         MKLong$ (Key$)
  114.         Mskey (Ostrg$, strln%, sbv%, ssv%)
  115. String handling routines;
  116.         Reverse (Istrg$, Ostrg$)
  117.         Rol (Istrg$, Ostrg$)
  118.         Ror (Istrg$, Ostrg$)
  119.         Scramble$ (istrg$, key$, Scrambleflag%)
  120. XOR Encryption functions;
  121.         XorKey (Istrg$, Ostrg$, ckey$)
  122.         XorRkn (Istrg$, Ostrg$, strt%, stp%)
  123.         XorRks (Istrg$, Ostrg$, kst$, kpos%)
  124.         XorSingle (Strg$, NewStrg$, ckey%)
  125.         XorString (Istrg$, Ostrg$, ckey%)
  126. Rotation Encryption Functions;
  127.         Rot13A$ (Istrg$)
  128. CRC Calculation;
  129.         CRC32& (X$)
  130. Compression;
  131.         LZ.Compress (A$, B$, Bytes.Out%)
  132.         LZ.Decompress (A$, B$, Bytes.Out%)
  133.  
  134.  
  135.    FUNCTION Argen% (Rseed$)
  136.      '   Rseed$ = 8 character string
  137.  
  138.      Argen generates a random number in the range 0-255.  Rseed$
  139. serves to initialize the generator.  To save on memory, Argen writes
  140. it's internal values back to Rseed$.  Therefore do *not* modify
  141. Rseed$, or else the output of the generator will change.  The only
  142. time I recommend modifying Rseed$ is between encryption sessions
  143. within a single run of the program.
  144.  
  145.      It is best to run thru the generator a number of times when
  146. initialized to provide for maximum randomization.  Something similiar
  147. to the following should suffice;
  148.  
  149.         Rseed$="123345678"
  150.         FOR x= 1 TO 100:Junk%=Argen%(Rseed$):NEXT
  151.  
  152.      Either MSkey or MakePass can also be used for creating input
  153. strings for Argen%.  Caution should be used if MakePass is used, see
  154. the description of MakePass for more information.
  155.  
  156.    FUNCTION CRC32& (X$)
  157.      '   X$    =   String to calculate 32 bit CRC on
  158.  
  159.      Calculates a 32 bit CRC for X$
  160.  
  161.  
  162.    FUNCTION LZ.Compress (A$, B$, Bytes.Out%)
  163.    FUNCTION LZ.Decompress (A$, B$, Bytes.Out%)
  164.      '   A$         =   Input String (String to compress)
  165.      '   B$         =   Output String
  166.      '   Bytes.Out% = Parameter used to trim output string
  167.  
  168.      These functions compress the input string (A$) using an LZ
  169. derived algorythm.
  170.  
  171.      When calling these routines, B$ cannot be null.  Therefore use
  172. something like the following to generate B$ prior to calling:
  173.  
  174.         B$=STRING$(LEN(A$)+100, 0)
  175.  
  176.      To trim the output to the proper length, use:
  177.  
  178.         B$=LEFT$(B$, Bytes.Out%)
  179.  
  180.      Both functions return a True <-1> if succesful and a False <0> if
  181. not.
  182.  
  183.      The LZ.x routines are courtesy of Rick Geldriech from the FIDO
  184. QUICK_BAS echo.  Thanks are due for the amount of time he spent
  185. assisting me in developing and debugging the PDS version and modifying
  186. the routines for stand-alone operation.
  187.  
  188.  
  189. <NOTE: For all MK___$ functions:  Dupe checking on Key$ is not
  190. performed.  Therefore something like Key$="DUMMY" will produce an
  191. incorrect output string>
  192.  
  193.  
  194.    FUNCTION MKAlpha$ (Key$)
  195.      '   Key$ = Key Value
  196.  
  197.      This function generates a key string suitable for use as input to
  198. a substitution system.
  199.  
  200.      It takes Key$, creates a string with Key$ as the first
  201. characters, then the remainder of the upper case alphabet skipping the
  202. characters in Key$.
  203.  
  204.      Example:
  205.      Calling MKAlpha$ with Key$="DUMB" will produce the following
  206. ouput:
  207.  
  208.     DUMBACEFGHIJKLNOPQRSTVWXYZ
  209.  
  210.    FUNCTION MKAlphaNum$ (Key$)
  211.      '   Key$ = Key Value
  212.  
  213.    This function operates similiar to MKAlpha$ except that it accepts
  214. and outputs the uppercase alphabet and the digits 0-9.
  215.  
  216.    FUNCTION MKLong$ (Key$)
  217.      '   Key$ = Key Value
  218.  
  219.    This function operates similiar to MKAlpha$ except that it accepts
  220. and outputs the entire ASCII character set, (0-255).
  221.  
  222.    FUNCTION MakeAlpha$
  223.  
  224.      Creates a string that has all the uppercase alphabetic characters.
  225.  
  226.    SUB MakeAlphaArray (A$())
  227.      '   A$()  = String array dimensioned to at least 26
  228.  
  229.      Creates a basic ASCII string array using the capital letters
  230.  
  231.      A$() must be DIMed to 26 or greater.  The first character is
  232. located at A$(1).
  233.  
  234.    FUNCTION MakeAlphaNum$
  235.  
  236.      Creates a string that has all the uppercase alphabetic characters
  237. followed by the digits 0-9
  238.  
  239.    SUB MakeAlphaNumArray (A$())
  240.      '   A$()  = String array dimensioned to at least 36
  241.  
  242.      Creates a basic ASCII string array using the capital letters and
  243.  the numerals 0 to 9
  244.  
  245.      A$() must be DIMed to 36 or greater.  The first character is
  246. located at A$(1).
  247.  
  248.    FUNCTION MakeLong$
  249.  
  250.      Creates a string holding all 256 ASCII characters (0-255).
  251.  
  252.    SUB MakeLongArray (A%())
  253.      '   A%  = Integer array dimensioned to at least 255
  254.  
  255.      Makes an array of all 255 ASCII characters
  256.  
  257.      A$() must be DIMed to 255 or greater.  The first character is
  258. located at A$(0).
  259.  
  260.    FUNCTION MakePass$ (tf%, lf%, Pass$)
  261.      '   tf%   =   Type flag
  262.      '             0 = Alpha
  263.      '             1 = Numeric
  264.      '             2 = Alphanumeric
  265.      '   lf%   =   Length of output string
  266.      '   Pass$ =   Output string
  267.  
  268.      MakePass generates a password suitable for use on BBS's, or
  269. simple encryption keys.  If your keys must be written down or printed
  270. out, then MakePass is suitable.  If the key material can be stored
  271. using disk files then Mskey is preferable.
  272.  
  273.      MakePass uses the internal PC random number generator to create
  274. the passwords.  Therefore the use of RANDOMIZE TIMER is recommended
  275. prior to calling this routine.  Be extremely cautious in using the
  276. output of this routine as the direct input to an encryption or
  277. decryption routine.  The output of the PC random number generator can
  278. vary between different machines.  Under normal usage this is not a
  279. handicap, however in this instance it can cause loss of data.
  280.  
  281.    FUNCTION MsKey$ (strln%, sbv%, ssv%)
  282.      '   Makes a simple key string.
  283.      '   ostrg$ =  Output string (key)
  284.      '   strln% =  Length of desired output string
  285.      '   sbv%   =  Starting value for counter (1 to 255)
  286.      '   ssv%   =  Stepping value for counter (1 to 255)
  287.  
  288.      MsKey Generates a pseudo-random key based on values provided by
  289. the programmer (user).  Basically sbv% is stepped by ssv%, then the
  290. CHR$ of sbv% is added to ostrg$.  This process loops until ostrg$
  291. reaches the desired length, (strln%).
  292.  
  293.      Use Mskey instead of MakePass for added security.  Mskey uses all
  294. 256 possible ASCII characters.  Since this can include screen control
  295. characters, printing to the screen or printer is not recommended.
  296.  
  297.      For the best possible security using this routine, ssv% should
  298. not be a multiple of 2, 5, 10, 255, or a even divisor or multiple of
  299. sbv%.
  300.  
  301.      This routine only provides minimum security, but is very fast.
  302. For greater security, a routine using Argen% is prefered.  Using this
  303. routine for creating input for Argen% will provide a fairly high
  304. degree of randomness.
  305.  
  306.  
  307.    FUNCTION Reverse$ (Istrg$)
  308.      '   Istrg$    =  Input String
  309.  
  310.      Reverses the order of the original string.  Hence "ABCDEFG"
  311. becomes "GFEDCBA".
  312.  
  313.    FUNCTION Rol$ (Istrg$)
  314.      '  Istrg$     =  Input string
  315.  
  316.      Performs a left wrapping shift of the original string.  Hence
  317. "ABCDEFG" becomes "BCDEFGA".
  318.  
  319.    FUNCTION Ror$ (Istrg$)
  320.      '  Istrg$     =  Input String
  321.  
  322.      Performs a right wrapping shift of the original string.  Hence
  323. "ABCDEFG" becomes "GABCDEF"
  324.  
  325.  
  326.      FUNCTION Scramble$ (Istrg$, Key$, Scrambleflag%)
  327.      '
  328.      '  Istrg$        =  String to be scrambled (8 characters)
  329.      '  Key$          =  String to serve as the 'key' for the scramble
  330.      '                   (1 character)
  331.      '  Scrambleflag% =  Scramble/unscramble flag
  332.      '                   0 = scramble
  333.      '                   1 = scramble
  334.  
  335.      Scramble as string based on the value of Key$.
  336.  
  337.      <Note:  If Istrg$ is longer then 8 characters, then only the
  338. first 8 characters will be scrambled, if Key$ is longer than 1
  339. character, only the first character will be used as the key>
  340.  
  341.  
  342. NOTE:
  343. 1)   For all of the XORxxx routines;  Be careful when modifying any
  344. key values between calls to the SUB's.  Inadvertent modification will
  345. scramble the output.  Any modifications made during encryption *MUST*
  346. be performed during decryption or the output will be scrambled and
  347. unrecoverable.
  348. 2)   Many of these routines are based on extensive discussion with
  349. Richard Vannoy on the FIDO QUICK_BAS echo.
  350.  
  351.    FUNCTION XorKey$ (Istrg$, ckey$)
  352.      '  Istrg$     =  Input String
  353.      '  CKey$      =  Value(s) to XOR
  354.  
  355.      Performs the XOR function on an entire string.
  356.  
  357.      Rotates thru the entire key, but does not preserve the position
  358. within the key.
  359.  
  360.      Useful for one-time encryption of short (under 1k) strings.  Each
  361. time the routine is called using a different value of lstrg$, and the
  362. same value of Ckey$, encryption begins again with the first character
  363. of Ckey$.
  364.  
  365.    FUNCTION XorRkn$ (Istrg$, strt%, stp%)
  366.      '  Istrg$     =  Input String
  367.      '  strt%      =  Starting key value (1-255)
  368.      '  stp%       =  Value to step key by (1-255)
  369.  
  370.      Performs the XOR function on an entire string.
  371.  
  372.      Rotates thru the keyspace (1-255) by stp%, starting at strt%.
  373. Passes strt% back out of the subroutine.  Thus it steps continuously
  374. thru key space
  375.  
  376.      This routine generates a pseudo random key based on strt% and
  377. stp%.  This key is then used to XOR Istrg$.  The current values of the
  378. pseudo random generator are maintained in strt% and stp%.  Caution
  379. should be used in modifying these values.
  380.  
  381.      For the best possible security using this routine, stp% should
  382. not be a multiple of 2, 5, 10, 255, or an even divisor of strt%.
  383.  
  384.    FUNCTION XorRks$ (Istrg$, kst$, kpos%)
  385.      '  Istrg$     =  Input String
  386.      '  Ostrg$     =  Output String
  387.      '  Kst$       =  Value(s) to XOR
  388.      '  Kpos%      =  current position within the key string
  389.  
  390.      Performs the XOR function on an entire string.  Operates like
  391. XorKey, but preserves the key position.
  392.      
  393.      Rotates thru the entire key, but does preserve the position within
  394. the key.
  395.  
  396.      Used when a large amount of data must be encrypted in small
  397. chunks.  kpos% is used to store the current pointer into the key.
  398.  
  399.    FUNCTION XorSingle$ (Istrg$, ckey%)
  400.      '  Istrg$      =  Input String (of length 1)
  401.      '  CKey%      =  Value to XOR
  402.  
  403.      Performs the XOR function on a single character using a single
  404. character key.
  405.  
  406.      Useful for fast encryption.  Recommended for use where speed is
  407. of the essence, but security is not.  Could be used in
  408. telecommunication if ckey% is rotated by a variable amount at
  409. pre-arranged times.  Another option is to use XorRkn with an input
  410. string of length 1.
  411.  
  412.    FUNCTION XorString (Istrg$, ckey%)
  413.      '  Istrg$      =  Input String
  414.      '  CKey%       =  Value to XOR <Range 0-255>
  415.  
  416.      Performs the XOR function on an entire string
  417.  
  418.      Uses the same key value for each position in the string
  419.  
  420.      This routine is reasonably fast and the least secure.
  421.  
  422.    FUNCTION Rot13A$(Istrg$)
  423.      '  Istrg$      =  Input String
  424.  
  425.      This function performs simple encryption of the ASCII characters
  426. a-z and A-Z.  Any other characters passed thru the function are not
  427. changed.
  428.