home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / monitors / rsys / rsyssrc.lha / RSysCrypt.c < prev    next >
C/C++ Source or Header  |  1993-09-19  |  17KB  |  717 lines

  1. /*
  2. ***************************************************************************
  3. *
  4. * Datei:
  5. *      RSysCrypt.c
  6. *
  7. * Inhalt:
  8. *
  9. *      --- Globale Routinen ---
  10. *
  11. *    int OpenCryptWindow ( void );
  12. *    void CryptFile ( void );
  13. *
  14. *      --- Lokale  Routinen ---
  15. *
  16. *    static UBYTE *Decrypt ( UBYTE *Source , long SourceLen , UBYTE *Destination , UBYTE *Key , WORD KeyLen );
  17. *    static UBYTE *Encrypt ( UBYTE *Source , long SourceLen , UBYTE *Destination , UBYTE *Key , WORD KeyLen );
  18. *    static UBYTE Automaton ( void );
  19. *    static void Crypt ( int encrypt );
  20. *    static void MakeROT13 ( UBYTE *buff , UBYTE *dest );
  21. *    static void refreshstrgad ( struct Gadget *gad , char *str );
  22. *    static void WindTitleInfo ( char *msg );
  23. *
  24. * Bemerkungen:
  25. *      Verschlüsselungsroutinen von RSys.
  26. *
  27. * Erstellungsdatum:
  28. *      07-Jul-93     Rolf Böhme
  29. *
  30. * Änderungen:
  31. *      07-Jul-93     Rolf Böhme      Erstellung
  32. *
  33. ***************************************************************************
  34. */
  35.  
  36. #include "RSys.h"
  37.  
  38.     /*
  39.      * Standardtyp der Verschlüsselungart.
  40.      */
  41. static int type = CRY;
  42.  
  43.     /*
  44.      * Die Intuition-Objekte für den Aufbau des
  45.      * Crypt-Windows.
  46.      */
  47. static struct Window *CryptWnd = NULL;
  48. static struct Gadget *CryptGList = NULL;
  49. static struct Gadget *CryptGadgets[8];
  50. static UWORD CryptLeft = 213;
  51. static UWORD CryptTop = 76;
  52. static UWORD CryptWidth = 278;
  53. static UWORD CryptHeight = 76;
  54. static UBYTE *CryptWdt = (UBYTE *) NAME " " VERSION " - Crypting Files";
  55.  
  56.     /*
  57.      * Diese Datei-lokale Struktur beinhaltet die Namen
  58.      * der Original- und der verschlüsselten Datei, sowie
  59.      * das Paßwort im Falle der Verschlüsselung mit dem
  60.      * "zellulären Automaten".
  61.      */
  62. static struct _cryptdata
  63. {
  64.     char    cr_datafile[MAXFULLNAME],
  65.             cr_cryptfile[MAXFULLNAME],
  66.             cr_pwd[20];
  67. }        cd =
  68.  
  69. {
  70.     "", "", "tiger"
  71. };
  72.  
  73. static UBYTE *TypeCYGad0Labels[] = {
  74.     (UBYTE *)"Crypt",
  75.     (UBYTE *)"ROT 13",
  76.     NULL };
  77.  
  78. static UWORD CryptGTypes[] = {
  79.     STRING_KIND,
  80.     STRING_KIND,
  81.     BUTTON_KIND,
  82.     BUTTON_KIND,
  83.     BUTTON_KIND,
  84.     BUTTON_KIND,
  85.     STRING_KIND,
  86.     CYCLE_KIND
  87. };
  88.  
  89. static struct NewGadget CryptNGad[] = {
  90.     66, 5, 132, 13, (UBYTE *)"Source", NULL, GD_SourceStrGad, PLACETEXT_LEFT, NULL, NULL,
  91.     66, 19, 132, 13, (UBYTE *)"Dest", NULL, GD_CryptStrGad, PLACETEXT_LEFT, NULL, NULL,
  92.     204, 5, 69, 13, (UBYTE *)"Select", NULL, GD_SelSourceGad, PLACETEXT_IN, NULL, NULL,
  93.     204, 19, 69, 13, (UBYTE *)"Select", NULL, GD_SelDestGad, PLACETEXT_IN, NULL, NULL,
  94.     98, 59, 81, 13, (UBYTE *)"Encrypt", NULL, GD_CryptGad, PLACETEXT_IN, NULL, NULL,
  95.     192, 59, 81, 13, (UBYTE *)"Decrypt", NULL, GD_DeccryptGad, PLACETEXT_IN, NULL, NULL,
  96.     66, 39, 207, 13, (UBYTE *)"Passwd", NULL, GD_PwdGad, PLACETEXT_LEFT,NULL,NULL,
  97.     4, 59, 81, 13, NULL, NULL, GD_TypeCYGad, 0, NULL, NULL
  98. };
  99.  
  100. static ULONG *CryptGTags[] = {
  101.     (ULONG *)(GTST_MaxChars), (ULONG *)MAXLINESIZE, (ULONG *)(TAG_DONE),
  102.     (ULONG *)(GTST_MaxChars), (ULONG *)MAXLINESIZE, (ULONG *)(TAG_DONE),
  103.     (ULONG *)(TAG_DONE),
  104.     (ULONG *)(TAG_DONE),
  105.     (ULONG *)(TAG_DONE),
  106.     (ULONG *)(TAG_DONE),
  107.     (ULONG *)(GTST_MaxChars), (ULONG *)256, (ULONG *)(TAG_DONE),
  108.     (ULONG * )(GTCY_Labels), (ULONG *)&TypeCYGad0Labels[ 0 ], (ULONG *)(TAG_DONE)
  109. };
  110.  
  111.     /*
  112.      * Die Prozedur WindTitleInfo() läßt eine Nachricht in
  113.      * der Titelzeile des Crypt-Fensters erscheinen.
  114.      */
  115. static void
  116. WindTitleInfo(char *msg)
  117. {
  118.     SetWindowTitles(CryptWnd,(UBYTE *)(msg),(UBYTE *)(-1));
  119.     return;
  120. }
  121.  
  122.     /*
  123.      * OpenCryptWindow() öffnet ein Fenster mit allen Kontrollelementen
  124.      */
  125. int
  126. OpenCryptWindow( void )
  127. {
  128.     struct NewGadget     ng;
  129.     struct Gadget     *g;
  130.     UWORD       lc, tc;
  131.     UWORD       wleft = CryptLeft, wtop = CryptTop, ww, wh;
  132.     int gl[] = {0,1,6};
  133.  
  134.     DPOS;
  135.  
  136.    AdjustWindowDimensions(Scr, CryptLeft, CryptTop, CryptWidth, CryptHeight,
  137.                                &wleft, &wtop, &ww, &wh);
  138.  
  139.     if ( ! ( g = CreateContext( &CryptGList ))) return 1L;
  140.  
  141.     for( lc = 0, tc = 0; lc < Crypt_CNT; lc++ ) 
  142.    {
  143.  
  144.         CopyMem((char * )&CryptNGad[ lc ], (char * )&ng, (long)sizeof( struct NewGadget ));
  145.  
  146.         ng.ng_VisualInfo = VisualInfo;
  147.         ng.ng_TextAttr   = Font;
  148.         ng.ng_LeftEdge   = OffX + ComputeX( ng.ng_LeftEdge );
  149.         ng.ng_TopEdge      = OffY + ComputeY( ng.ng_TopEdge );
  150.         ng.ng_Width       = ComputeX( ng.ng_Width );
  151.         ng.ng_Height      = ComputeY( ng.ng_Height);
  152.  
  153.         CryptGadgets[ lc ] = g = CreateGadgetA((ULONG)CryptGTypes[ lc ], g, &ng, ( struct TagItem * )&CryptGTags[ tc ] );
  154.  
  155.         makelabelvisible(CryptGadgets[lc]);
  156.  
  157.         while( CryptGTags[ tc ] ) tc += 2;
  158.         tc++;
  159.  
  160.         if ( NOT g ) return 2L;
  161.     }
  162.  
  163.     if ( ! ( CryptWnd = OpenWindowTags( NULL,
  164.                 WA_Left,   wleft,
  165.                 WA_Top,         wtop,
  166.                 WA_Width,    ww,
  167.                 WA_Height,     wh,
  168.                 WA_IDCMP,    IDCMP_VANILLAKEY|
  169.                                 STRINGIDCMP|
  170.                                 BUTTONIDCMP|
  171.                                 IDCMP_CLOSEWINDOW|
  172.                                 IDCMP_REFRESHWINDOW,
  173.                 WA_Flags,    WFLG_DRAGBAR|
  174.                                 WFLG_DEPTHGADGET|
  175.                                 WFLG_CLOSEGADGET|
  176.                                 WFLG_SMART_REFRESH|
  177.                                 WFLG_ACTIVATE|
  178.                                 WFLG_RMBTRAP,
  179.                 WA_Title,    CryptWdt,
  180.                 WA_PubScreenFallBack,TRUE,
  181.                 WA_PubScreen, Scr,
  182.                 TAG_DONE )))
  183.         return 4L;
  184.  
  185.     RefreshRastPort(CryptWnd,CryptGadgets,gl, 3, FALSE, CryptGList);
  186.  
  187.     return NULL;
  188. }
  189.  
  190.     /*
  191.      * refreshstrgad() erneuert den Inhalt eines Stringgadgets
  192.      */
  193. static void
  194. refreshstrgad(struct Gadget *gad, char *str)
  195. {
  196.     GT_SetGadgetAttrs(gad, CryptWnd, NULL, GTST_String, (UBYTE *) str, TAG_DONE);
  197.  
  198.     return;
  199. }
  200.  
  201.     /*
  202.      * Die nächsten Kommentare sind dem Programm-Quelltext von TERM entnommen.
  203.      * Olsen, danke für die Comments :-)
  204.      */
  205.  
  206.      /*
  207.       * The cell ring and the ring index pointers.
  208.       */
  209.  
  210. static UBYTE Cell[2][CELL_WIDTH + 2],
  211.         From,
  212.         To;
  213.  
  214.      /*
  215.       *  Automaton():
  216.       *
  217.       *    A cellular automaton working on a ring of celles, producing
  218.       *    random data in each single cell .
  219.       */
  220.  
  221. static UBYTE
  222. Automaton(void)
  223. {
  224.     register WORD i;
  225.  
  226.      /*
  227.       * Operate on the cell ring...
  228.       */
  229.  
  230.     for (i = 1; i <= CELL_WIDTH; i++)
  231.         Cell[To][i] = Cell[From][i - 1] ^ (Cell[From][i] | Cell[From][i + 1]);
  232.  
  233.      /*
  234.       * Operate on first and last element.
  235.       */
  236.  
  237.     Cell[To][0] = Cell[From][CELL_WIDTH + 1] ^ (Cell[From][0] | Cell[From][1]);
  238.     Cell[To][CELL_WIDTH + 1] = Cell[From][CELL_WIDTH] ^ (Cell[From][CELL_WIDTH + 1] | Cell[From][0]);
  239.  
  240.         /*
  241.          * Swap cell rings.
  242.          */
  243.     To = From;
  244.     From ^= 1;
  245.  
  246.         /*
  247.          * Return contents of first cell.
  248.          */
  249.     return (Cell[From][0]);
  250. }
  251.  
  252.      /*
  253.       * Encrypt(UBYTE *Source,UBYTE *Destination,UBYTE *Key):
  254.       * Encrypt data using cellular automaton as a random number generator.
  255.       */
  256.  
  257. static UBYTE *
  258. Encrypt(UBYTE * Source, long SourceLen, UBYTE * Destination, UBYTE * Key, WORD KeyLen)
  259. {
  260.     register long i,
  261.             j;
  262.  
  263.         /*
  264.          * Set up cell ring index pointers.
  265.          */
  266.     From = 0;
  267.     To = 1;
  268.  
  269.         /*
  270.          * Initialize the cell ring with the key contents.
  271.          */
  272.     for (i = 0, j = KeyLen - 1; i < CELL_WIDTH + 2; i++)
  273.     {
  274.         Cell[0][i] = Key[j];
  275.  
  276.         if (j) j--;
  277.         else
  278.             j = KeyLen - 1;
  279.     }
  280.  
  281.         /*
  282.          * Encrypt the source data.
  283.          */
  284.     for (i = 0; i < SourceLen; i++)
  285.         Destination[i] = (UBYTE) (((WORD) Source[i] + Automaton())% 256);
  286.  
  287.         /*
  288.          * Return result.
  289.          */
  290.     return Destination;
  291. }
  292.  
  293.      /*
  294.       * Decrypt(UBYTE *Source,UBYTE *Destination,UBYTE *Key):
  295.       *
  296.       *    Decrypt data using cellular automaton as a random number generator.
  297.       */
  298.  
  299. static UBYTE *
  300. Decrypt(UBYTE * Source, long SourceLen, UBYTE * Destination, UBYTE * Key, WORD KeyLen)
  301. {
  302.     register long i,
  303.             j,
  304.             Code;
  305.  
  306.         /*
  307.          * Set up cell ring index pointers.
  308.          */
  309.     From = 0;
  310.     To = 1;
  311.  
  312.         /*
  313.          * Initialize the cell ring with the key contents.
  314.          */
  315.     for (i = 0, j = KeyLen - 1; i < CELL_WIDTH + 2; i++)
  316.     {
  317.         Cell[0][i] = Key[j];
  318.  
  319.         if (j) j--;
  320.         else
  321.             j = KeyLen - 1;
  322.     }
  323.  
  324.         /*
  325.          * Decrypt the source data.
  326.          */
  327.     for (i = 0; i < SourceLen; i++)
  328.     {
  329.         if ((Code = Source[i] - Automaton())< 0) Code = 256 + Code;
  330.  
  331.         Destination[i] = (UBYTE) Code;
  332.     }
  333.  
  334.     return Destination;
  335. }
  336.  
  337.     /*
  338.      * Die Prozedur MakeROT13() erzeugt aus einem
  339.      * Eingabestring buff eine Ausgabestring dest
  340.      * dergestalt, daß jeder Buchstabe um 13 Zeichen nach
  341.      * vorne oder hinten verschoben wird. Hier die
  342.      * Translationsliste:
  343.      *
  344.      * Input:        abcdefghijklmnopqrstuvwxyz
  345.      * Output:        nopqrstuvwxyzabcdefghijklm
  346.      *
  347.      * Input:        ABCDEFGHIJKLMNOPQRSTUVWXYZ
  348.      * Output:        NOPQRSTUVWXYZABCDEFGHIJKLM
  349.      */
  350. static void
  351. MakeROT13(UBYTE * buff, UBYTE * dest)
  352. {
  353.     UBYTE *destptr = &dest[0];
  354.     char    ch;
  355.  
  356.     strcpy((char *)dest, (char *)buff);
  357.  
  358.     while (*destptr)
  359.     {
  360.         ch = (char)(*destptr);
  361.  
  362.         if (isalpha((int)ch))
  363.         {
  364.             if (((ch >= 'A') && (ch < 'N')) || ((ch >= 'a') && (ch < 'n')))
  365.                 *destptr = (UBYTE) (ch + 13);
  366.  
  367.             if (((ch >= 'N') && (ch <= 'Z')) || ((ch >= 'n') && (ch <= 'z')))
  368.                 *destptr = (UBYTE) (ch - 13);
  369.         }
  370.  
  371.         destptr++;
  372.     }
  373.  
  374.     return;
  375. }
  376.  
  377.     /*
  378.      * Crypt() ver- oder entschlüsselt eine Datei
  379.      */
  380. static void
  381. Crypt(int encrypt)
  382. {
  383.     long    fsize;
  384.     char    pwd[PWDLEN+1],
  385.             cpwd[PWDLEN+1];
  386.     BPTR    Dfile,
  387.             Cfile;
  388.     UBYTE *Dbuffer,
  389.           *Cbuffer;
  390.     int    pwdlen;
  391.  
  392.     if (strlen(cd.cr_pwd) == 0) strcpy(pwd, "tiger");
  393.     else
  394.         strncpy(pwd, cd.cr_pwd, PWDLEN);
  395.  
  396.     pwdlen = strlen(pwd);
  397.  
  398.     if (encrypt)
  399.     {
  400.         if (NOT(exist(cd.cr_datafile)))
  401.         {
  402.             ErrorHandle(cd.cr_datafile, FILE_ERR, EXIST_FAIL, NO_KILL);
  403.             return;
  404.         }
  405.  
  406.         if (type == R13)
  407.         {
  408.             extern int global_type;
  409.  
  410.             filetype(cd.cr_datafile);
  411.             if (global_type != TYPE_TEXT)
  412.             {
  413.                 ErrorHandle(cd.cr_datafile, FILE_ERR, TYPE_FAIL, NO_KILL);
  414.                 return;
  415.             }
  416.         }
  417.  
  418.         fsize = SizeOfFile(cd.cr_datafile);
  419.         if (fsize == 0L)
  420.         {
  421.             ErrorHandle(cd.cr_datafile, FILE_ERR, SIZE_FAIL, NO_KILL);
  422.             return;
  423.         }
  424.  
  425.         if (exist(cd.cr_cryptfile) && NOT(Question(CryptWnd, "Crypt file exist!\n"
  426.                                                  "Do you want to overwrite them?", YES)))
  427.             return;
  428.  
  429.         if (Dfile = Open((UBYTE *) cd.cr_datafile, MODE_OLDFILE))
  430.         {
  431.             if (Cfile = Open((UBYTE *) cd.cr_cryptfile, MODE_NEWFILE))
  432.             {
  433.                 if ((Dbuffer = MyAllocVec((fsize + 1) * sizeof(UBYTE), MEMF_CLEAR, NO_KILL)) &&
  434.                      (Cbuffer = MyAllocVec((fsize + 1) * sizeof(UBYTE), MEMF_CLEAR, NO_KILL)))
  435.                 {
  436.                     WindTitleInfo("Reading datafile...");
  437.                     if (fsize == Read(Dfile, Dbuffer, fsize))
  438.                     {
  439.                         if (NOT(type))
  440.                         {
  441.                             WindTitleInfo("Encrypt password...");
  442.                             Encrypt((UBYTE *) pwd, (long) pwdlen, (UBYTE *) cpwd,
  443.                                       (UBYTE *) pwd, (WORD) pwdlen);
  444.  
  445.                             if (pwdlen != Write(Cfile, cpwd, pwdlen))
  446.                                 ErrorHandle(cd.cr_cryptfile, FILE_ERR, WRITE_FAIL, NO_KILL);
  447.                             else
  448.                             {
  449.                                 WindTitleInfo("Encrypt data file...");
  450.                                 Cbuffer = Encrypt(Dbuffer, (long)fsize,
  451.                                                         Cbuffer, (UBYTE *) pwd, (WORD) pwdlen);
  452.                             }
  453.                         }
  454.                         else
  455.                         {
  456.                             WindTitleInfo("Rotate data file...");
  457.                             MakeROT13(Dbuffer, Cbuffer);
  458.                         }
  459.  
  460.                         WindTitleInfo(type ? "Writing ROT13 file..." : "Writing crypt file...");
  461.                         if (fsize != Write(Cfile, Cbuffer, fsize))
  462.                             ErrorHandle(cd.cr_cryptfile, FILE_ERR, WRITE_FAIL, NO_KILL);
  463.                     }
  464.                     else ErrorHandle(cd.cr_datafile, FILE_ERR, READ_FAIL, NO_KILL);
  465.  
  466.                     MyFreeVec(Cbuffer);
  467.                     MyFreeVec(Dbuffer);
  468.                 }
  469.  
  470.                 Close(Cfile);
  471.             }
  472.             else ErrorHandle(cd.cr_cryptfile, FILE_ERR, OPEN_FAIL, NO_KILL);
  473.  
  474.             Close(Dfile);
  475.         }
  476.         else ErrorHandle(cd.cr_datafile, FILE_ERR, OPEN_FAIL, NO_KILL);
  477.     }
  478.     else
  479.     {
  480.         if (NOT(exist(cd.cr_cryptfile)))
  481.         {
  482.             ErrorHandle(cd.cr_cryptfile, FILE_ERR, EXIST_FAIL, NO_KILL);
  483.             return;
  484.         }
  485.  
  486.         fsize = SizeOfFile(cd.cr_cryptfile);
  487.         if (fsize == 0L)
  488.         {
  489.             ErrorHandle(cd.cr_cryptfile, FILE_ERR, SIZE_FAIL, NO_KILL);
  490.             return;
  491.         }
  492.  
  493.         if (exist(cd.cr_datafile) &&
  494.              NOT(Question(CryptWnd, "Data file exist!\n" "Do you want to overwrite them?", YES)))
  495.             return;
  496.  
  497.         if (Dfile = Open((UBYTE *) cd.cr_datafile, MODE_NEWFILE))
  498.         {
  499.             if (Cfile = Open((UBYTE *) cd.cr_cryptfile, MODE_OLDFILE))
  500.             {
  501.                 if ((Dbuffer = MyAllocVec((fsize + 1) * sizeof(UBYTE), MEMF_CLEAR, NO_KILL)) &&
  502.                      (Cbuffer = MyAllocVec((fsize + 1) * sizeof(UBYTE), MEMF_CLEAR, NO_KILL)))
  503.                 {
  504.                     WindTitleInfo(type ? "Reading ROT13 file..." : "Reading crypt file...");
  505.                     if (fsize == Read(Cfile, Cbuffer, fsize))
  506.                     {
  507.                         if (NOT(type))
  508.                         {
  509.                             WindTitleInfo("Decrypt password...");
  510.  
  511.                             strncpy(cpwd, (char *)Cbuffer, pwdlen);
  512.                             Decrypt((UBYTE *) cpwd, (long)pwdlen,
  513.                                       (UBYTE *) pwd, (UBYTE *) pwd, (WORD) pwdlen);
  514.  
  515.                             if (strcmp(cd.cr_pwd, pwd))
  516.                                 ErrorHandle("Password", MISC_ERR, WRONG_FAIL, NO_KILL);
  517.                             else
  518.                             {
  519.                                 WindTitleInfo("Decrypt crypt file...");
  520.                                 Dbuffer = Decrypt(&Cbuffer[pwdlen], (long)(fsize - pwdlen),
  521.                                                         Dbuffer, (UBYTE *) pwd, (WORD) pwdlen);
  522.                             }
  523.                         }
  524.                         else
  525.                         {
  526.                             WindTitleInfo("Derotate ROT13-File...");
  527.                             MakeROT13(Cbuffer, Dbuffer);
  528.                         }
  529.  
  530.                         WindTitleInfo("Writing data file...");
  531.  
  532.                         fsize -= (type ? 0 : pwdlen);
  533.                         if (fsize != Write(Dfile, Dbuffer, fsize))
  534.                             ErrorHandle(cd.cr_datafile, FILE_ERR, WRITE_FAIL, NO_KILL);
  535.                     }
  536.                     else
  537.                         ErrorHandle(cd.cr_cryptfile, FILE_ERR, READ_FAIL, NO_KILL);
  538.  
  539.                     MyFreeVec(Cbuffer);
  540.                     MyFreeVec(Dbuffer);
  541.                 }
  542.  
  543.                 Close(Cfile);
  544.             }
  545.             else ErrorHandle(cd.cr_cryptfile, FILE_ERR, OPEN_FAIL, NO_KILL);
  546.  
  547.             Close(Dfile);
  548.         }
  549.         else ErrorHandle(cd.cr_datafile, FILE_ERR, OPEN_FAIL, NO_KILL);
  550.     }
  551.  
  552.     WindTitleInfo((char *)CryptWdt);
  553.  
  554.     return;
  555. }
  556.  
  557.     /*
  558.      * CryptFile() bietet eine kleine Benutzeroberfläche an und
  559.      * kontrolliert die Ein- und Ausgabedateien sowie das Paßwort
  560.      */
  561. void
  562. CryptFile(void)
  563. {
  564.     register struct IntuiMessage *message;
  565.     ULONG class,
  566.             code;
  567.     APTR    object;
  568.     APTR    reqcrypt = NULL;
  569.     char *gadbuff;
  570.    int GID;
  571.  
  572.     DPOS;
  573.  
  574.    HandleHelp(MN_CryptFile);
  575.  
  576.     Flags.quit_cr = 0;
  577.  
  578.     cd.cr_datafile[0] = STRINGEND;
  579.     cd.cr_cryptfile[0] = STRINGEND;
  580.     strcpy(cd.cr_pwd,"tiger");
  581.  
  582.     if (OpenASysWindow(OpenCryptWindow,NO_KILL))
  583.     {
  584.       LockMainWindow(LOCK);
  585.  
  586.         refreshstrgad(CryptGadgets[GD_SourceStrGad - GD_SourceStrGad],cd.cr_datafile);
  587.         refreshstrgad(CryptGadgets[GD_CryptStrGad - GD_SourceStrGad],cd.cr_cryptfile);
  588.         refreshstrgad(CryptGadgets[GD_PwdGad - GD_SourceStrGad],cd.cr_pwd);
  589.  
  590.         do
  591.         {
  592.             Wait(1L << CryptWnd->UserPort->mp_SigBit);
  593.  
  594.             while ((message = (struct IntuiMessage *)
  595.                       GT_GetIMsg(CryptWnd->UserPort)) != NULL)
  596.             {
  597.                 object = message->IAddress;
  598.                 class = message->Class;
  599.                 code = message->Code;
  600.  
  601.                 GT_ReplyIMsg(message);
  602.  
  603.                 switch (class)
  604.                 {
  605.                     case IDCMP_GADGETUP:
  606.                   GID = ((struct Gadget *) object)->GadgetID;
  607.  
  608.                   HandleHelp((enum RSysNumbers)GID);
  609.  
  610.                         switch (GID)
  611.                         {
  612.                             case GD_SourceStrGad:
  613.                                 gadbuff = gadgetbuff(CryptGadgets[GD_SourceStrGad - GD_SourceStrGad]);
  614.  
  615.                                 strncpy(cd.cr_datafile, gadbuff, MAXFULLNAME);
  616.                                 strncpy(cd.cr_cryptfile, gadbuff, MAXFULLNAME);
  617.                                 strcat(cd.cr_cryptfile, (type ? ".ROT" : ".CPT"));
  618.  
  619.                                 refreshstrgad(CryptGadgets[GD_CryptStrGad - GD_SourceStrGad],
  620.                                                   cd.cr_cryptfile);
  621.                                 break;
  622.  
  623.                             case GD_CryptStrGad:
  624.                                 gadbuff = gadgetbuff(CryptGadgets[GD_CryptStrGad - GD_SourceStrGad]);
  625.                                 strncpy(cd.cr_cryptfile, gadbuff, MAXFULLNAME);
  626.                                 break;
  627.  
  628.                             case GD_SelSourceGad:
  629.                                 if (GetFile(CryptWnd,NULL,NULL,NULL,
  630.                                                 "Select File to crypt","Select"))
  631.                                 {
  632.                                     strncpy(cd.cr_datafile,(char *)_fullpath,
  633.                                              MAXFULLNAME);
  634.  
  635.                                     strncpy(cd.cr_cryptfile,(char *)_fullpath,
  636.                                              MAXFULLNAME-4);
  637.                                     strcat(cd.cr_cryptfile, (type ? ".ROT" : ".CPT"));
  638.  
  639.                                     refreshstrgad(CryptGadgets[GD_SourceStrGad - GD_SourceStrGad],
  640.                                                       cd.cr_datafile);
  641.                                     refreshstrgad(CryptGadgets[GD_CryptStrGad - GD_SourceStrGad],
  642.                                                       cd.cr_cryptfile);
  643.                                 }
  644.  
  645.                                 break;
  646.  
  647.                             case GD_SelDestGad:
  648.                                 if (GetFile(CryptWnd,NULL,NULL,
  649.                                                 (type ? "#?.ROT" :"#?.CPT"),
  650.                                                 (type ? "Select ROT13 file" :"Select Crypt file"),
  651.                                                 "Select"))
  652.                                 {
  653.                                     strncpy(cd.cr_cryptfile, (char *)_fullpath,MAXFULLNAME);
  654.                                     refreshstrgad(CryptGadgets[GD_CryptStrGad - GD_SourceStrGad],
  655.                                                       cd.cr_cryptfile);
  656.                                 }
  657.                                 break;
  658.  
  659.                             case GD_CryptGad:
  660.                                 reqcrypt = LockWindow(CryptWnd);
  661.  
  662.                                 Crypt(TRUE);
  663.  
  664.                                 UnlockWindow(reqcrypt);
  665.                                 break;
  666.  
  667.                             case GD_DeccryptGad:
  668.                                 reqcrypt = LockWindow(CryptWnd);
  669.  
  670.                                 Crypt(FALSE);
  671.  
  672.                                 UnlockWindow(reqcrypt);
  673.                                 break;
  674.  
  675.                             case GD_PwdGad:
  676.                                 strncpy(cd.cr_pwd, gadgetbuff(CryptGadgets[GD_PwdGad - GD_SourceStrGad]), 20);
  677.                                 break;
  678.  
  679.                             case GD_TypeCYGad:
  680.                                 type = (int)code;
  681.                         EnableGadget(CryptWnd, CryptGadgets[GD_PwdGad - GD_SourceStrGad], 
  682.                                      NOT(type));
  683.  
  684.                                 if(cd.cr_datafile[0])
  685.                                 {
  686.                                     strncpy(cd.cr_cryptfile, cd.cr_datafile, MAXFULLNAME-4);
  687.                                     strcat(cd.cr_cryptfile, (type ? ".ROT" : ".CPT"));
  688.  
  689.                                     refreshstrgad(CryptGadgets[GD_CryptStrGad - GD_SourceStrGad],
  690.                                                       cd.cr_cryptfile);
  691.                                 }
  692.                                 break;
  693.                         }
  694.  
  695.                         break;
  696.  
  697.                     case IDCMP_VANILLAKEY:
  698.                         if (code == ESC) Flags.quit_cr = 1;
  699.                         break;
  700.  
  701.                     case IDCMP_CLOSEWINDOW:
  702.                         Flags.quit_cr = 1;
  703.                         break;
  704.                 }
  705.             }
  706.         }
  707.         while (NOT(Flags.quit_cr));
  708.  
  709.         CloseASysWindow(&CryptWnd, &CryptGList, NULL);
  710.  
  711.       LockMainWindow(UNLOCK);
  712.     }
  713.  
  714.     return;
  715. }
  716.  
  717.