What's new

Encryption program I wrote for CLO EUS

Mest Lover

Not Sea Org Qualified
I used to encrypt files, this is what I used to do it:

Code:
/*
     @(#) encrypt.c 1.0     91/01/06.  COPYRIGHT (C) 1991 BY Q.U.E. computers.
                                       ALL RIGHTS RESERVED.

    THIS C SORCE FILE PROGRAM WAS DESIGNED AND WRITTEN BY ANDRUE C. CARR
    FOR Q.U.E. computers AND IS FOR EXCLUSIVE USE BY CLO EUS.  EXPRESSED
    WRITTEN PERMISSION FROM:  Q.U.E. computers, ANDRUE C. CARR, OR
    3F PRODUCTIONS IS REQUIRED FOR ITS: COMERCIAL USE, COMPILATION,
    DUPLICATION, MODIFICATION, OR EXECUTION. THIS PROGRAM IS A
    NON-COMMERCIAL ENTITY AND MAY NOT BE SOLD.

    IN THE EVENT THAT THE WORKING COPY FOUND IN THE DIRECTORY: /usr/bin
    IS RENDERED NON-EXECUTABLE OR ERASED, WRITTEN PERMISSION IS HEREBY
    GRANTED TO COMPILE THE SORCE CODE FOUND IN THE DIRECTORY:
    /usr/local/cprog AND DUPLICATE THE OBJECT FILE TO THE DIRECTORY:
    /usr/bin BY ANY Q.U.E. computers OR CLO EUS AUTHORIZED C LANGUAGE
    PROGRAMMER FOR THE SOLE PURPOSE OF RESTORING A WORKING COPY OF THIS
    PROGRAM TO THE CLO EUS XENIX SYSTEM V 386 COMPUTER.  BACKUP OR
    HARDCOPIES MAY BE OBTAINED BY CONTACTING ANDRUE C. CARR.
    ALL CURRENT APPLICABLE COPYRIGHT LAWS DO APPLY.

    A PROGRAM TO ENCRYPT ANY FILE BY USING THE "DLH-POLY-86-B CIPHER".
    FOUND IN MicroEMACS 3.10e.

                *) :-)  *===>   -----}|
*/

/*        Crypt:        Encryption routines for MicroEMACS
 *                written by Dana Hoggatt and Daniel Lawrence
 *
 *        MicroEMACS 3.10
 *                written by Daniel M. Lawrence
 *                based on code by Dave G. Conroy.
 *
 *        (C)opyright 1988,1989 by Daniel M. Lawrence
 *        MicroEMACS 3.10 can be copied and distributed freely for any
 *        non-commercial purposes. MicroEMACS 3.10 can only be incorporated
 *        into commercial software with the permission of the current author.
 *
 *      This program was put together to use separate from MicroEMACS 3.10e
 *      by Andrue C. Carr to enable encryption of files.  The main() module
 *      copyfile() module, checkfile() module are all original code by
 *      Andrue C. Carr. Modifications were made to coordinate the rest of
 *      the program which has code written by:
 *      Daniel Lawrence or Dana Hoggatt. Credit is hereby given to the
 *      latter two programmers for their wisdom.
 */

#include        <ctype.h>
#include        <stdio.h>
#include        "estruct.h"
#include        "eproto.h"
#include        "edef.h"
#include        "elang.h"

#define            INPUT        system ("(stty -echo;)");printf ("\n%s ",TEXT33);\
                        scanf ("%s", input);system ("(stty echo;)")

/* global variables */

char    bell = '\007', temp[3][200], input[1][NPAT];
int        newkey[NPAT], swap;

/*********************/
/* FILE COPY MODULE  */
/*********************/

int copyfile (orig_file, spot_file)  /* original code by Andrue C. Carr */

char        orig_file[1][40], spot_file[1][40];

{
        int         c, i;
        char        temp[1][2];
        FILE        *end_f, *copy_f;

        if (swap == TRUE)
        for (i = 0; i < strlen (newkey);++i)
            printf ("\n%c  c, %d  d, %s s", newkey[i], newkey[i], newkey);
        end_f = fopen (spot_file, "w");
        copy_f = fopen (orig_file, "r");
        while ( (c = getc (copy_f)) != EOF )
              if (swap == TRUE)
                 {
              printf ("\n%s  c, %d  d, ", &c, c);
                      crypt (&c, 1);
                      putc (c, end_f);
                 }
              else
                 putc (c, end_f);
        fclose (copy_f);
        fclose (end_f);
        return (0);
}

/***************************************************/
/*   VERIFY THAT THE FILE EXIST AND CAN BE OPENED  */
/***************************************************/

int checkfile (poter, message)  /* original code by Andrue C. Carr */

char    *message[1][20];
FILE    *poter;

{
   if ( poter == (FILE *) NULL )
      {
         printf ("\n\n%c", bell);
         printf ("encrypt:  %s cannot be found", message);
         printf ("\n\n");
         exit (1);
      }
}

int checkcrypt (file)  /* original code by Andrue C. Carr */

char    *file[1][20];

{
        int        j, c;
        FILE    *poter;

        sprintf (temp[1], "");
        poter = fopen (file, "r");
        for (j = 0; j < strlen (newkey); ++j)
            {
                    c = getc (poter);
                    sprintf (temp[0], "%c", (toascii (c)));
                strcat (temp[1], temp[0]);
            }
        strcpy (temp[2], temp[1]);
        strcpy (temp[0], temp[1]);
        crypt (temp[2], strlen (temp[2]));
        crypt((char *)NULL, (unsigned int) 847);  /* set my own number */
        crypt(temp[1], (unsigned int) strlen(temp[1]));
        crypt (temp[0],(unsigned int) strlen (temp[0]));

   if  ( ((c = strcmp (input, temp[2])) != 0) /*not encypted or wrong key*/

       || ((c = strcmp (temp[0], temp[1])) != 0) )
      {
         printf ("\n\n%c", bell);
         printf ("encryption keys do not match.");
         printf ("\n\n");
         exit (1);
      }
}




         /* next set of code by Dana Hogatt */

static int mod95();

PASCAL NEAR setlkey(f, n)        /* reset encryption key of current buffer */

int f;                /* default flag */
int n;                /* numeric argument */

{
        char key[NPAT];                /* new encryption string */

        /* get the string to use as an encrytion string */
/*                       "Encryption String: " */

        INPUT;

        /* and encrypt it */
        crypt((char *)NULL, (unsigned int) 847);  /* set my own number */
        crypt(input, (unsigned int) strlen(input));

        /* and save it off */
        strcpy (newkey, input);

        /*decrypt it for sum reason in file.c */

        crypt((char *)NULL, 0);  /* set my own number */
        crypt(newkey, strlen(newkey));

        /*re-encrypt it  seeding it first */
        crypt((char *)NULL, 0);  /* set my own number */
        crypt(newkey, strlen(newkey));


        return(TRUE);
}

/**********
 *
 *        crypt - in place encryption/decryption of a buffer
 *
 *        (C) Copyright 1986, Dana L. Hoggatt
 *        1216, Beck Lane, Lafayette, IN
 *
 *        When consulting directly with the author of this routine,
 *        please refer to this routine as the "DLH-POLY-86-B CIPHER".
 *
 *        This routine was written for Dan Lawrence, for use in V3.8 of
 *        MicroEMACS, a public domain text/program editor.
 *
 *        I kept the following goals in mind when preparing this function:
 *
 *            1.        All printable characters were to be encrypted back
 *                into the printable range, control characters and
 *                high-bit characters were to remain unaffected.  this
 *                way, encrypted would still be just as cheap to
 *                transmit down a 7-bit data path as they were before.
 *
 *            2.        The encryption had to be portable.  The encrypted
 *                file from one computer should be able to be decrypted
 *                on another computer.
 *
 *            3.        The encryption had to be inexpensive, both in terms
 *                of speed and space.
 *
 *            4.        The system needed to be secure against all but the
 *                most determined of attackers.
 *
 *        For encryption of a block of data, one calls crypt passing
 *        a pointer to the data block and its length. The data block is
 *        encrypted in place, that is, the encrypted output overwrites
 *        the input.  Decryption is totally isomorphic, and is performed
 *        in the same manner by the same routine.
 *
 *        Before using this routine for encrypting data, you are expected
 *        to specify an encryption key.  This key is an arbitrary string,
 *        to be supplied by the user.  To set the key takes two calls to
 *        crypt().  First, you call
 *
 *                crypt(NULL, vector)
 *
 *        This resets all internal control information.  Typically (and
 *        specifically in the case on MICRO-emacs) you would use a "vector"
 *        of 0.  Other values can be used to customize your editor to be
 *        "incompatable" with the normally distributed version.  For
 *        this purpose, the best results will be obtained by avoiding
 *        multiples of 95.
 *
 *        Then, you "encrypt" your password by calling
 *
 *                crypt(pass, strlen(pass))
 *
 *        where "pass" is your password string.  Crypt() will destroy
 *        the original copy of the password (it becomes encrypted),
 *        which is good.  You do not want someone on a multiuser system
 *        to peruse your memory space and bump into your password.
 *        Still, it is a better idea to erase the password buffer to
 *        defeat memory perusal by a more technical snooper.
 *
 *        For the interest of cryptologists, at the heart of this
 *        function is a Beaufort Cipher.  The cipher alphabet is the
 *        range of printable characters (' ' to '~'), all "control"
 *        and "high-bit" characters are left unaltered.
 *
 *        The key is a variant autokey, derived from a wieghted sum
 *        of all the previous clear text and cipher text.  A counter
 *        is used as salt to obiterate any simple cyclic behavior
 *        from the clear text, and key feedback is used to assure
 *        that the entire message is based on the original key,
 *        preventing attacks on the last part of the message as if
 *        it were a pure autokey system.
 *
 *        Overall security of encrypted data depends upon three
 *        factors:  the fundamental cryptographic system must be
 *        difficult to compromise; exhaustive searching of the key
 *        space must be computationally expensive; keys and plaintext
 *        must remain out of sight.  This system satisfies this set
 *        of conditions to within the degree desired for MicroEMACS.
 *
 *        Though direct methods of attack (against systems such as
 *        this) do exist, they are not well known and will consume
 *        considerable amounts of computing time.  An exhaustive
 *        search requires over a billion investigations, on average.
 *
 *        The choice, entry, storage, manipulation, alteration,
 *        protection and security of the keys themselves are the
 *        responsiblity of the user.
 *
 **********/

PASCAL NEAR crypt(bptr, len)
register char *bptr;        /* buffer of characters to be encrypted */
register unsigned len;        /* number of characters in the buffer */
{
        register int cc;        /* current character being considered */

        static long key = 0;        /* 29 bit encipherment key */
        static int salt = 0;        /* salt to spice up key with */

        if (!bptr) {                /* is there anything here to encrypt? */
                key = len;        /* set the new key */
                salt = len;        /* set the new salt */
                return;
        }
        while (len--) {                /* for every character in the buffer */

                cc = *bptr;        /* get a character out of the buffer */

                /* only encipher printable characters */
                if ((cc >= ' ') && (cc <= '~')) {

/*      We feed the upper few bits of the key back into itself. This assures us
        that the starting key affects the entire message. We go through some
        effort here to prevent the key from exceeding 29 bits.  This is so the
        aritmetic caluclation in a later statement, which impliments our
        autokey, won't overflow, making the key go negative. Machine behavior
        in these cases does not tend to be portable.        */

                        key = (key & 0x1FFFFFFFl) ^ ((key >> 29) & 0x3l);

/**  Down-bias the character, perform a Beaufort encipherment, and
        up-bias the character again.  We want key to be positive
        so that the left shift here will be more portable and the
        mod95() faster   **/

                        cc = mod95((int)(key % 95) - (cc - ' ')) + ' ';

/**  the salt will spice up the key a little bit, helping to obscure
        any patterns in the clear text, particularly when all the
        characters (or long sequences of them) are the same.  We do
        not want the salt to go negative, or it will affect the key
        too radically.  It is always a good idea to chop off cyclics
        to prime values.  **/

                        if (++salt >= 20857) {        /* prime modulus */
                                salt = 0;
                        }

/**  our autokey (a special case of the running key) is being
        generated by a weighted checksum of clear text, cipher
        text, and salt.

        I had to allow the older broken key calculation to let us decrypt
        old files.  I hope to take this out eventually. **/

                                key = key + key + (cc ^ *bptr) + salt;
                }
                *bptr++ = cc;        /* put character back into buffer */
        }
        return;
}

static int mod95(val)

register int val;

{
        /*  The mathematical MOD does not match the computer MOD  */

        /*  Yes, what I do here may look strange, but it gets the
                job done, and portably at that.  */

        while (val >= 9500)
                val -= 9500;
        while (val >= 950)
                val -= 950;
        while (val >= 95)
                val -= 95;
        while (val < 0)
                val += 95;
        return (val);
}

/***********/
/* MAIN () */
/***********/

main (argc, argv)     /* main is original code by Andrue C. Carr */

char        *argv[];
int        argc;

{

        int        len, i;
        char    *tempdir[1][39], origin[1][28];
        FILE        *origin_file;

        setlkey ();
        for (i = 1; i < argc; ++i)
            {
                strcpy (origin, argv[i]);
                origin_file = fopen (origin, "r");
                checkfile (origin_file, origin);
/*                checkcrypt (origin); */
                printf ("\nEncrypting: %s ", origin);
                sprintf (tempdir, "test.tmp");
                copyfile (origin, tempdir);
                swap = TRUE;
                copyfile (tempdir, origin);
                unlink (tempdir);
            }
        exit (0);
}
 
Last edited:

Rene Descartes

Gold Meritorious Patron
I used to encrypt files, this is what I used to do it:

QUOTE]

I once had to write a program for a group of users in a company that would generate passwords and after looking at your program I am glad I did not have to do it in C.

Again I tip my hat to you.

Rd00
 

Mest Lover

Not Sea Org Qualified
I used to encrypt files, this is what I used to do it:

QUOTE]

I once had to write a program for a group of users in a company that would generate passwords and after looking at your program I am glad I did not have to do it in C.

Again I tip my hat to you.

Rd00

I grabbed most of that code from Emacs source that they had on the system, Emacs was the text editor I used to write my programs with, it had that encryption thing in it and I wanted a standalone program that would encrypt anything I told it to.

I would encrypt files I didn't want them seeing like searches I was doing through the telexes that had anything to do with me or other things I needed to know about.
Keystroke logs and other stuff too.
 

Lohan2008

Gold Meritorious Patron
would encrypt files I didn't want them seeing like searches I was doing through the telexes that had anything to do with me or other things I needed to know about.Keystroke logs and other stuff too.

Nice :thumbsup:

My brother-in-law use to get in trouble with his (wog) company for "overclocking" the computer when he worked arvo/night shifts and wanted to process the warehouse orders quicker...

:roflmao:
 

Mest Lover

Not Sea Org Qualified
Geez I had bad spelling back then but good coding ethics.

BTW: I hereby give written permission to absolutely anyone to copy, spread, rewrite, re-engineer, compile, render to executable, transfer et al, make fun of, or otherwise hereto F WITH THIS!

I have about 40 other programs I am thinking of dumping code for also.
 

Crashed Alien

Patron with Honors
Geez I had bad spelling back then but good coding ethics.

BTW: I hereby give written permission to absolutely anyone to copy, spread, rewrite, re-engineer, compile, render to executable, transfer et al, make fun of, or otherwise hereto F WITH THIS!

I have about 40 other programs I am thinking of dumping code for also.

Mest Lover,

That is cool... I never got into C, just Mainframe stuff, mini stuff and then of course PC stuff, COBOL (A bit), VB, VBA, HTML, JavaScript, etc... My stuff was mainly commercial systems, Banking, manufacturing, etc...

Still love dabbling a bit... If I have time, I might try converting your C (above)... Just to see if I am still capable! If you don't mind?

Happy coding,

Crash
 
Top