      INTEGER OPTION(102),CORRE0(102)
      INTEGER MAPDN
      INTEGER OPTIO0(4)
      INTEGER CHOICE
      INTEGER GETARG,INDEX
      DATA OPTIO0/233,229,228,0/
      IF((GETARG(1,OPTION,102).EQ.-1))GOTO 10001
      IF((OPTION(1).NE.173))GOTO 10001
      GOTO 10000
10001   CALL USAGE
10000 CHOICE=INDEX(OPTIO0,MAPDN(OPTION(2)))
      GOTO 10002
10004   CALL INITI0
        GOTO 10003
10005   IF((GETARG(2,CORRE0,102).NE.-1))GOTO 10104
          CALL USAGE
10104   CALL SEND(-10,-11,CORRE0)
        GOTO 10003
10006   IF((GETARG(2,CORRE0,102).NE.-1))GOTO 10105
10105   CALL RECEI0(-10,-11,CORRE0)
        GOTO 10003
10007 CONTINUE
10002 GOTO(10004,10005,10006),CHOICE
        CALL USAGE
10003 CALL SWT
      END
      SUBROUTINE USAGE
      CALL REMARK('Usage:  rsa (-i | -e correspondent | -d).')
      CALL SWT
      END
      SUBROUTINE INITI0
      INTEGER * 4 P,Q,EAAAA0,DAAAA0,N
      INTEGER * 4 INVMOD
      INTEGER ENFILE(24)
      INTEGER ENPROT(4)
      INTEGER DEFILE(24)
      INTEGER DEPROT(2)
      INTEGER ENPERM(102),DEPERM(102),NAME(33)
      INTEGER * 2 ENFD,DEFD
      INTEGER * 2 CREATE
      INTEGER AT
      INTEGER AAAAA0(16)
      INTEGER AAAAB0(16)
      DATA ENFILE/189,246,225,242,243,228,233,242,189,175,174,242,243,22
     *5,223,229,238,227,233,240,232,229,242,0/
      DATA ENPROT/225,175,242,0/
      DATA DEFILE/189,246,225,242,243,228,233,242,189,175,174,242,243,22
     *5,223,228,229,227,233,240,232,229,242,0/
      DATA DEPROT/225,0/
      DATA AAAAA0/170,243,189,164,225,236,236,160,164,242,229,243,244,18
     *9,242,0/
      DATA AAAAB0/170,243,189,164,225,236,236,160,164,242,229,243,244,18
     *9,176,0/
      ENFD=CREATE(ENFILE,3)
      IF((ENFD.NE.-3))GOTO 10106
        CALL REMARK('cannot create encryption parameters file.')
        RETURN
10106 DEFD=CREATE(DEFILE,3)
      IF((DEFD.NE.-3))GOTO 10107
        CALL CLOSE(ENFD)
        CALL REMARK('cannot create decryption parameters file.')
        RETURN
10107 CALL GETPR0(P,Q,EAAAA0)
      N=P*Q
      CALL PRINT(ENFD,'*l,*l*n.',EAAAA0,N)
      DAAAA0=INVMOD(EAAAA0,(P-1)*(Q-1))
      CALL PRINT(DEFD,'*l,*l*n.',DAAAA0,N)
      CALL CLOSE(ENFD)
      CALL CLOSE(DEFD)
      CALL SPROT$(ENFILE,ENPROT)
      CALL SPROT$(DEFILE,DEPROT)
      CALL DATE(3,NAME)
      CALL ENCODE(ENPERM,102,AAAAA0,NAME)
      CALL ENCODE(DEPERM,102,AAAAB0,NAME)
      CALL SFDATA(6,ENFILE,ENPERM,AT,0)
      CALL SFDATA(6,DEFILE,DEPERM,AT,0)
      RETURN
      END
      SUBROUTINE GETPR0(P,Q,EAAAA0)
      INTEGER * 4 P,Q,EAAAA0
      INTEGER * 4 RANDOM,PRIME
      CALL RANDO0
      P=PRIME(RANDOM(INTL(20),INTL(54)))
      Q=PRIME(RANDOM(INTL(20),INTL(54)))
      EAAAA0=PRIME(RANDOM(INTL(55),INTL(3511)))
      RETURN
      END
      SUBROUTINE RANDO0
      INTEGER TD(10),I,SEED
      CALL TIMDAT(TD,10)
      SEED=0
      DO 10108 I=1,10
        SEED=SEED+TD(I)
10108 CONTINUE
10109 CALL RND(IABS(SEED))
      RETURN
      END
      INTEGER * 4 FUNCTION RANDOM(LWB,UPB)
      INTEGER * 4 LWB,UPB
      REAL RND
      RANDOM=(UPB-LWB)*RND(0)+LWB
      RETURN
      END
      SUBROUTINE SEND(FIN,FOUT,CORRE0)
      INTEGER * 2 FIN,FOUT
      INTEGER CORRE0(1)
      INTEGER GETEN0
      INTEGER * 4 EAAAA0,N
      IF((GETEN0(CORRE0,EAAAA0,N).NE.-2))GOTO 10110
        CALL ENCIP0(FIN,FOUT,EAAAA0,N)
        GOTO 10111
10110   CALL REMARK('RSA enciphering file missing or unreadable.')
10111 RETURN
      END
      INTEGER FUNCTION GETEN0(USER,EAAAA0,N)
      INTEGER USER(1)
      INTEGER * 4 EAAAA0,N
      INTEGER PREFIX(8)
      INTEGER SUFFIX(15)
      INTEGER PARAM0(102)
      INTEGER LENGTH,INPUT
      INTEGER * 2 PARAM
      INTEGER * 2 OPEN
      DATA PREFIX/189,246,225,242,243,189,175,0/
      DATA SUFFIX/175,174,242,243,225,223,229,238,227,233,240,232,229,24
     *2,0/
      CALL SCOPY(PREFIX,1,PARAM0,1)
      CALL SCOPY(USER,1,PARAM0,LENGTH(PARAM0)+1)
      CALL SCOPY(SUFFIX,1,PARAM0,LENGTH(PARAM0)+1)
      PARAM=OPEN(PARAM0,1)
      IF((PARAM.EQ.-3))GOTO 10113
      IF((INPUT(PARAM,'*,,,l*l.',EAAAA0,N).EQ.-1))GOTO 10113
      GOTO 10112
10113   GETEN0=-3
        GOTO 10114
10112   GETEN0=-2
10114 IF((PARAM.EQ.-3))GOTO 10115
        CALL CLOSE(PARAM)
10115 RETURN
      END
      SUBROUTINE ENCIP0(FI,FO,EAAAA0,N)
      INTEGER * 2 FI,FO
      INTEGER * 4 EAAAA0,N
      INTEGER P
      INTEGER GETCH
      INTEGER * 4 PWRMOD
10116 IF((GETCH(P,FI).EQ.-1))GOTO 10117
        CALL PRINT(FO,'*l*n.',PWRMOD(INTL(P),EAAAA0,N))
      GOTO 10116
10117 RETURN
      END
      SUBROUTINE RECEI0(FIN,FOUT,CORRE0)
      INTEGER * 2 FIN,FOUT
      INTEGER CORRE0(1)
      INTEGER GETDE0
      INTEGER * 4 DAAAA0,N
      IF((GETDE0(DAAAA0,N).NE.-2))GOTO 10118
        CALL DECIP0(FIN,FOUT,DAAAA0,N)
        GOTO 10119
10118   CALL REMARK('RSA deciphering file missing or unreadable.')
10119 RETURN
      END
      INTEGER FUNCTION GETDE0(DAAAA0,N)
      INTEGER * 4 DAAAA0,N
      INTEGER PARAM0(24)
      INTEGER INPUT
      INTEGER * 2 PARAM
      INTEGER * 2 OPEN
      DATA PARAM0/189,246,225,242,243,228,233,242,189,175,174,242,243,22
     *5,223,228,229,227,233,240,232,229,242,0/
      PARAM=OPEN(PARAM0,1)
      IF((PARAM.EQ.-3))GOTO 10121
      IF((INPUT(PARAM,'*,,,l*l.',DAAAA0,N).EQ.-1))GOTO 10121
      GOTO 10120
10121   GETDE0=-3
        GOTO 10122
10120   GETDE0=-2
10122 IF((PARAM.EQ.-3))GOTO 10123
        CALL CLOSE(PARAM)
10123 RETURN
      END
      SUBROUTINE DECIP0(FI,FO,DAAAA0,N)
      INTEGER * 2 FI,FO
      INTEGER * 4 DAAAA0,N
      INTEGER * 4 C
      INTEGER GETCI0
      INTEGER * 4 PWRMOD
10124 IF((GETCI0(C,FI).EQ.-1))GOTO 10125
        CALL PUTCH(INTS(PWRMOD(C,DAAAA0,N)),FO)
      GOTO 10124
10125 RETURN
      END
      INTEGER FUNCTION GETCI0(L,FI)
      INTEGER * 4 L
      INTEGER * 2 FI
      INTEGER INPUT
      GETCI0=INPUT(FI,'*l.',L)
      RETURN
      END
C ---- Long Name Map ----
C getcipher                      getci0
C receive                        recei0
C getenparams                    geten0
C decipher                       decip0
C randomize                      rando0
C getprimes                      getpr0
C encipher                       encip0
C D                              daaaa0
C correspondent                  corre0
C E                              eaaaa0
C initialize                     initi0
C paramfile                      param0
C getdeparams                    getde0
C options                        optio0
