REPORT ZENCRYPT.
************************************************************************
*  This program encrypts and decrypts a string using an 8 byte
*  strong??? one-way-hash like encrypting method
*  ( instead of bit operation it uses byte operations, because of
*  ABAP's lack of bit operations )
*
*  It is useful to process sensitive data (HR ...), that would be
*  otherwise accessible by both ABAPs and UNIX users (root, adm)
*
*  The algorithm of the encrypting:
*  result(1) = source(1) @ initial_key
*  result(n) = source(n) @ source(n-1) @ result(n-1)
*
*  The algorithm of the decrypting:
*  source(1) = result(1) # initial_key
*  source(n) = result(n) # result(n-1) # source(n-1)
*
*  Where source, result, initial_key are 8 byte strings
*  @ and # are negate byte operations of each other
************************************************************************
PARAMETERS: STRING(96), KEY(8).
PARAMETER ENCRYPT RADIOBUTTON GROUP RB.
PARAMETER DECRYPT RADIOBUTTON GROUP RB.
DATA: I TYPE I, J TYPE I.
DATA: BEGIN OF A,
   1 TYPE X, 2 TYPE X, 3 TYPE X, 4 TYPE X,
   5 TYPE X, 6 TYPE X, 7 TYPE X, 8 TYPE X,
END OF A.
DATA: BEGIN OF B,
   1 TYPE X, 2 TYPE X, 3 TYPE X, 4 TYPE X,
   5 TYPE X, 6 TYPE X, 7 TYPE X, 8 TYPE X,
END OF B.
DATA: BEGIN OF C,
   1 TYPE X, 2 TYPE X, 3 TYPE X, 4 TYPE X,
   5 TYPE X, 6 TYPE X, 7 TYPE X, 8 TYPE X,
END OF C.
DATA: BEGIN OF D,
   1 TYPE X, 2 TYPE X, 3 TYPE X, 4 TYPE X,
   5 TYPE X, 6 TYPE X, 7 TYPE X, 8 TYPE X,
END OF D.
DATA: BEGIN OF E,
   1 TYPE X, 2 TYPE X, 3 TYPE X, 4 TYPE X,
   5 TYPE X, 6 TYPE X, 7 TYPE X, 8 TYPE X,
END OF E.

WRITE: / STRING.
*                          encrypting
J = STRLEN( STRING ).
A = KEY.
DO.
  B = STRING+I.
  E = B.
  IF SY-INDEX = 1.
    B-1 = A-1 + B-1.B-2 = A-2 + B-2.B-3 = A-3 + B-3.B-4 = A-4 + B-4.
    B-5 = A-5 + B-5.B-6 = A-6 + B-6.B-7 = A-7 + B-7.B-8 = A-8 + B-8.
  ELSE.
    B-1 = C-1 + D-1 + B-1.B-2 = C-2 + D-2 + B-2.
    B-3 = C-3 + D-3 + B-3.B-4 = C-4 + D-4 + B-4.
    B-5 = C-5 + D-5 + B-5.B-6 = C-6 + D-6 + B-6.
    B-7 = C-7 + D-7 + B-7.B-8 = C-8 + D-8 + B-8.
  ENDIF.
  D = B. C = E.
  STRING+I(8) = B.
  I = I + 8.
  IF I >= J.
    EXIT.
  ENDIF.
ENDDO.

WRITE: / STRING.
*                            decrypting
J = STRLEN( STRING ). I = 0.
A = KEY.
DO.
  B = STRING+I.
  E = B.
  IF SY-INDEX = 1.
    B-1 = B-1 - A-1.B-2 = B-2 - A-2.B-3 = B-3 - A-3.B-4 = B-4 - A-4.
    B-5 = B-5 - A-5.B-6 = B-6 - A-6.B-7 = B-7 - A-7.B-8 = B-8 - A-8.
  ELSE.
    B-1 = B-1 - C-1 - D-1.B-2 = B-2 - C-2 - D-2.
    B-3 = B-3 - C-3 - D-3.B-4 = B-4 - C-4 - D-4.
    B-5 = B-5 - C-5 - D-5.B-6 = B-6 - C-6 - D-6.
    B-7 = B-7 - C-7 - D-7.B-8 = B-8 - C-8 - D-8.
  ENDIF.
  D = B. C = E.
  STRING+I(8) = B.
  I = I + 8.
  IF I >= J.
    EXIT.
  ENDIF.
ENDDO.

WRITE: / STRING.