[Code] Encryption of ASCII rent files.

From: Michael Scott (scottm@workcomm.net)
Date: 10/07/96


(same as my format for #.obj files in the lib/world/obj/ dir) and then i made
it so that characters could rent their mail.. but for security purposes I 
wanted to write an encryption routine so that noisy ftper's could d/l 
someone's rent file and read their mail (the letter text is still stored 
in the action desc) and ALSO for another reason:
	If a player were to send a solitary '~' character on a line it would
cause the recipients obj file to get outta wack (the mud uses that to 
deliminate the end of the initial object text strings) .. so what i was 
left with writing was a routine which would encode a persons mail in a 
nice even format which didnt include the '~' character in the output.
and here's what i came up with.. NOTE: this took me about 2 hours to 
write and I personally have never written an encoder/encryption program..
so the routines below are VERY wasteful of space.. they double the size 
of the original text (although in cases of repitition they do better).
	I would be obliged if someone could post a better version of what 
i've done or point me in the direction of some 'real' encryption sources. 
(something a little more space worthy).. anyways what you send to these 
routines is a buffer large enough to store double the size of the 
original text which is also sent.  (I also return the buffer pointer for 
utility purposes).  The routine itself DOES NOT ALLOCATE memory for that 
buffer, it needs to be already created..

----------------------- cut here ----------------------------------
/*
 * File: encode.c
 * Usage: used to generate a coded version of an ASCII buffer. which
 * is formatted to 70 width and doesnt have an empty line with a ~ on it
 */

#include <strings.h>
#include <stdlib.h>
#include <stdio.h>

#define CHAR_TAB 9
#define CHAR_NL  10
#define CHAR_CR  13

#define TAB_LENGTH		4
#define OFFSET			29
#define PRINT_BYTE_OFFSET	65
#define PRINTABLE_SET_LENGTH	26
#define NL_LINE_LENGTH		70
#define MAX_REPEAT		3

#define SET_CONTROL_CHAR(r,s) \
	(coded_bytes[0] = (r) + ((s)<<2) + PRINT_BYTE_OFFSET)

char *msg_encode(char *buffer, char *orig) {
   int i, total;
   char j, k, l;
   char coded_bytes[3] = "\0\0\0";

   total = 0;
   *buffer = '\0';
   for (i = 0; (orig[i] != '\0'); i++) {
      j = 0;
      while ((orig[i+j+1] == orig[i]) && (j < MAX_REPEAT)) {
	 j++;
      }
      if (orig[i] == CHAR_TAB) {
	 l = ' ' - OFFSET;
	 k = l / PRINTABLE_SET_LENGTH;
	 l -= k*PRINTABLE_SET_LENGTH;
	 l += PRINT_BYTE_OFFSET;
	 j = 3;
      }
      else if (orig[i] == CHAR_NL) { l = PRINT_BYTE_OFFSET+1; k = 0; }
      else if (orig[i] == CHAR_CR) { l = PRINT_BYTE_OFFSET+2; k = 0; }
      else {
	 l = orig[i] - OFFSET;
	 k = l / PRINTABLE_SET_LENGTH;
	 l -= k*PRINTABLE_SET_LENGTH;
	 l += PRINT_BYTE_OFFSET;
      }
      SET_CONTROL_CHAR(j,k);
      coded_bytes[1] = l;
      strcat(buffer, coded_bytes);
      total += 2;
      i += j;
      if ((total > 0) && ((total % NL_LINE_LENGTH) == 0)) strcat(buffer, "\n");
   }
   strcat(buffer, "*\n");
   return buffer;
}


char *msg_decode(char *buffer, char *orig) {
   int i, rep, set, k, n;
   char l;

   n = 0;
   for (i = 0; ((orig[i] != '*') && (orig[i] != '\0')); i++) {
      if ((orig[i] >= PRINT_BYTE_OFFSET) && (orig[i] < (PRINT_BYTE_OFFSET + PRINTABLE_SET_LENGTH))) {
	 l = orig[i] - PRINT_BYTE_OFFSET;
	 rep = (int)(l & 3);		/* get the # of repeats */
	 set = (int)((l & 12)>>2);	/* get the set # */
	 while ((orig[++i] < PRINT_BYTE_OFFSET) && (orig[i] >= (PRINT_BYTE_OFFSET + PRINTABLE_SET_LENGTH)) && (orig[i] != '*') && (orig[i] != '\0'));
	 if ((orig[i] == '\0') || (orig[i] == '*')) break;
	 l = orig[i] - PRINT_BYTE_OFFSET;
	 l += (set * PRINTABLE_SET_LENGTH);
	 if (i == 0) {
	    rep = TAB_LENGTH;
	    l = ' ';
	 }
	 else if (l == 1) l = CHAR_NL;
	 else if (l == 2) l = CHAR_CR;
	 else l += OFFSET;
	 for (k = 0; k <= rep; k++)
	   buffer[n++] = l;
      }
   }
   buffer[n] = '\0';
   return buffer;
}
---------------------------------- cut here -------------------------

Sorry for the code spam.

                      Michael Scott -- "Living in the Eye of the Hurricane."
		      FLAMES/COMMENTS to scottm@workcomm.net
+-----------------------------------------------------------+
| Ensure that you have read the CircleMUD Mailing List FAQ: |
|   http://cspo.queensu.ca/~fletcher/Circle/list_faq.html   |
+-----------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/18/00 PST