home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / clarion / encrypt.zip / CIPHER.CLA next >
Text File  |  1990-02-04  |  5KB  |  104 lines

  1.          MEMBER('ENCRYPT')
  2.  
  3. !==============================================================================!
  4. !            COPYRIGHT (C) 1990 by Randy Goodhew.               !
  5. !               ALL RIGHTS RESERVED, WORLDWIDE.                   !
  6. !------------------------------------------------------------------------------!
  7. !           Your comments are welcome. Please contact:               !
  8. !                Randy Goodhew                       !
  9. !            PROFESSIONAL TECHNOLOGIES CONSULTANTS               !
  10. !                  508 Greenup Street                   !
  11. !                  Covington, KY 41011                   !
  12. !                 Phone: (606) 491-3020                   !
  13. !==============================================================================!
  14.  
  15. Encipher     FUNCTION(L:PlainText,L:Password)
  16.        GROUP,PRE(L)                 !Local Group Variables
  17. PlainText    STRING(255)             !Plain Text String
  18. CipherText   STRING(255)             !Cipher Text String
  19. Password     STRING(30)                 !Password String
  20. Len_Plain    LONG                 !Length of Plain Text String
  21. Len_Password LONG                 !Length of Password
  22. c         LONG                 !Password Character Position
  23. n         LONG                 !Loop Counter
  24.        END !group
  25.  
  26.          CODE
  27.  !---------- Encipher Loop -----------------------------------------------
  28.  ! This Encryption Method uses a double XORing technique:
  29.  ! 1) Extract the ASCII Value of a character.
  30.  ! 2) XOR the value using the ASCII value of a Password character as a mask.
  31.  ! 3) XOR using the Encryption Loop Counter as a mask.
  32.  ! 4) Then convert back to an ASCII character.
  33.  ! This encryption algorithm will cycle through the Password and use each
  34.  ! character as a mask. Therefore, the longer the Password, the better the
  35.  ! encryption. The second XORing helps make the Cipher Text appear
  36.  ! nonrepeating. A better technique would be to use pseudorandom numbers.
  37.  !------------------------------------------------------------------------
  38.  L:PlainText    = CLIP(L:PlainText)         !Clip Plain Text String
  39.  L:Password    = CLIP(L:Password)         !Clip Password String
  40.  L:Len_Plain    = LEN(CLIP(L:Plaintext))     !Get length of Plain Text
  41.  L:Len_Password = LEN(CLIP(L:Password))         !Get length of Password
  42.  L:CipherText    = ''                 !Clear Cipher Text String
  43.  L:c        = 0                 !Clear Position Counter
  44.  
  45.  LOOP L:n = 1 TO L:Len_Plain BY 1         !Encryption Loop
  46.    L:c += 1                     !Increment Position Counter
  47.    IF L:c > L:Len_Password THEN L:c = 1.     !Reset Position Counter
  48.    L:CipherText = SUB(L:CipherText,1,(L:n - 1)) & |   !Build Cipher Text String
  49.    CHR(BXOR(BXOR(VAL(SUB(L:PlainText,L:n,1)),VAL(SUB(L:Password,L:c,1))),L:n))
  50.  END !encryption loop
  51.  
  52.  RETURN(L:CipherText)                 !Return Encrypted String
  53.  
  54. !====================================================================
  55.  
  56. Decipher     FUNCTION(L:CipherText,L:Password)
  57.        GROUP,PRE(L)                !Local Group Variables
  58. PlainText    STRING(255)            !Plain Text String
  59. CipherText   STRING(255)            !Cipher Text String
  60. Password     STRING(30)                !Password String
  61. Len_Cipher   LONG                !Length of Cipher Text String
  62. Len_Password LONG                !Length of Password String
  63. a         LONG                !Value of Deciphered Character
  64. b         LONG                !Value of Password Character
  65. c         LONG                !Password Character Position
  66. d         LONG                !Value of Cipher Text Character
  67. n         LONG                !Decryption Loop Counter
  68.        END !group
  69.  
  70.          CODE
  71.  !---------- Decipher Loop --------------------------------------------
  72.  ! Reverse the process.
  73.  !---------------------------------------------------------------------
  74.  L:Len_Cipher    = LEN(CLIP(L:CipherText))     !Get length of Cipher Text
  75.  L:Len_Password = LEN(CLIP(L:Password))         !Get length of Password
  76.  L:PlainText    = ''                 !Clear Plain Text String
  77.  L:c        = 0                 !Clear Position Counter
  78.  
  79.  LOOP L:n = 1 TO L:Len_Cipher BY 1         !Decryption Loop
  80.    L:c += 1
  81.    IF L:c > L:Len_Password THEN L:c = 1.
  82.    L:b = VAL(SUB(L:Password,L:c,1))
  83.    L:d = VAL(SUB(L:CipherText,L:n,1))
  84.  
  85.    IF BXOR(L:d,L:n) = 0                 !Test for NULL
  86.      L:a = L:b
  87.    ELSIF L:d = 32                 !Test for Space (ASCII 32)
  88.      L:a = BXOR(BXOR(BXOR(L:d,L:b),L:n),32)
  89.    ELSE
  90.      L:a = BXOR(BXOR(L:d,L:b),L:n)
  91.    END !if
  92.    !-------------------------------------------------------------------
  93.    ! Since Clarion converts NULLs to the Space symbol (ASCII 32),
  94.    ! the above IF structure corrects the Decryption Process.
  95.    ! Note: A NULL will appear in the Encrypted String if the character
  96.    ! being XORed and mask character in the Password are the same.
  97.    !-------------------------------------------------------------------
  98.  
  99.    L:PlainText = SUB(L:PlainText,1,(L:n - 1)) & CHR(L:a) !Build String
  100.  END !decryption loop
  101.  
  102.  RETURN(L:PlainText)                 !Return Plain Text String
  103.  
  104.