home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1166 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  5.5 KB

  1. From: jon@walt.cc.utexas.edu (Jon Boede)
  2. Newsgroups: alt.sources
  3. Subject: [sci.crypt] cryptext.c mix subroutine
  4. Message-ID: <11687@stag.math.lsa.umich.edu>
  5. Date: 11 Apr 90 21:36:18 GMT
  6.  
  7. Archive-name: cryptext/11-Apr-90
  8. Original-posting-by: jon@walt.cc.utexas.edu (Jon Boede)
  9. Original-subject: cryptext.c mix subroutine
  10. Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)
  11.  
  12. [This is an experimental alt.sources re-posting from the newsgroup(s)
  13. sci.crypt. Comments on this service to emv@math.lsa.umich.edu 
  14. (Edward Vielmetti).]
  15.  
  16.  
  17. I'd posted a message a while back looking for an encryption routine that had
  18. a number of less-than-usual characteristics.  Istvan Mohos was kind enough to
  19. send me his "mix" program suite which I am enjoying quite a bit.  All I needed
  20. out of the whole program suite was just a subroutine to encrypt a string.  I
  21. whipped something up and am posting it here for anyone who might be interested.
  22.  
  23. The most charming characteristics of the cryptext subroutine are the fact that
  24. the resultant string is the same length and of the same character set as the
  25. input string.  In what follows, "text" is a NUL terminated string of any
  26. length, "kstring" is the key also of any length, and "encrypt" is a flag where
  27. 1=encrypt and 0=decrypt.
  28.  
  29. The only odd thing is FUDGE, which had to be added to the length of the input
  30. string as not to corrupt the malloc() heap... apparently unmix() runs off the
  31. end of the string by a little (I suspect sizeof(struct keychar) but have not
  32. investigated it further).
  33.  
  34. #    This is a shell archive.
  35. #    Remove everything above and including the cut line.
  36. #    Then run the rest of the file through sh.
  37. -----cut here-----cut here-----cut here-----cut here-----
  38. #!/bin/sh
  39. # shar:    Shell Archiver
  40. #    Run the following text with /bin/sh to create:
  41. #    cryptext.c
  42. # This archive created: Wed Apr 11 14:33:57 1990
  43. echo shar: extracting cryptext.c '(3132 characters)'
  44. sed 's/^XX//' << \SHAR_EOF > cryptext.c
  45. XX#include <stdio.h>
  46. XX
  47. XXstruct keychar {       /* links to form chain of key bytes */
  48. XX    int val;
  49. XX    struct keychar *next;
  50. XX};
  51. XX
  52. XX/* this seems necessary to keep the heap from being corrupted by unmix */
  53. XX#define FUDGE 10
  54. XX
  55. XX/* should be in a header file */
  56. XX#define CRYPTKEYLEN 40
  57. XX
  58. XXcryptext(text,kstring,encrypt)
  59. XXchar *text, *kstring;
  60. XXint encrypt;
  61. XX{
  62. XX    int len, loop, klen;
  63. XX    char *out_text, *malloc();
  64. XX    register char *cs;
  65. XX    struct keychar key[CRYPTKEYLEN];
  66. XX
  67. XX    len = strlen(text);
  68. XX    klen = strlen(kstring);
  69. XX
  70. XX    if ((out_text = malloc(len + FUDGE)) == NULL) {
  71. XX        printf("\nOut of memory... couldn't crypt.\n");
  72. XX        return(1);
  73. XX    }
  74. XX
  75. XX    for (loop=klen, cs=kstring+klen; --loop; ) {
  76. XX        key[loop].next = key + loop - 1;
  77. XX        key[loop].val = *--cs;
  78. XX    }
  79. XX    key[0].next = key + klen - 1;
  80. XX    key[0].val = *--cs;
  81. XX
  82. XX    if (encrypt)
  83. XX        mix(key,text,out_text);
  84. XX    else {
  85. XX        for (loop=0; loop < len; loop++)
  86. XX            out_text[loop] = '\0';
  87. XX        unmix(key,text,out_text);
  88. XX    }
  89. XX    strncpy(text,out_text,len);
  90. XX    text[len] = '\0';
  91. XX    free(out_text);
  92. XX    return(0);
  93. XX}
  94. XX
  95. XX/*  mix:
  96. XX    Author          : Istvan Mohos, March 1987
  97. XX    Rev A Apr 14 87 : Do not mix last byte
  98. XX    Rev B Apr 27 87 : by Ira Chayut; R & D Associates
  99. XX
  100. XX    given key byte ASCII values "a b c ... n" in circularly linked list,
  101. XX    and plaintext char pointer ptr initially at plaintext[0],
  102. XX
  103. XX    advance ptr "a" bytes;
  104. XX    install its value as the first byte of cyphertext;
  105. XX    zero out plaintext byte pointed to by ptr;
  106. XX    decrement value of "a";
  107. XX
  108. XX    while (not done) {
  109. XX        advance to next link of key;
  110. XX        advance ptr by the value of the link;
  111. XX        install ptr value as the next byte of cyphertext;
  112. XX        zero out plaintext byte pointed to by ptr;
  113. XX        decrement value of link;
  114. XX    }
  115. XX
  116. XX    Throughout, plaintext is assumed to be a circular list of bytes:
  117. XX    ptr increments beyond the buffer continue from the beginning.
  118. XX    If ptr value is null (byte already assigned to cyphertext), ptr is
  119. XX    incremented until the next non-null plaintext byte.
  120. XX    When key link value reaches zero, the just assigned plaintext byte
  121. XX    is swapped into its position.
  122. XX*/
  123. XX
  124. XXmix(key,plain,cipher)
  125. XXstruct keychar key[];
  126. XXchar *plain, *cipher;
  127. XX{
  128. XX    register struct keychar *K;
  129. XX    register char *here, *cip;
  130. XX    char *head, *tail;
  131. XX    int regi, loop, flen;
  132. XX
  133. XX    flen = strlen(plain);
  134. XX
  135. XX    head = plain;
  136. XX    here = head + flen - 1;
  137. XX    tail = here - 1;
  138. XX    loop = head - tail - 1; /* always negative */
  139. XX    cip = cipher;
  140. XX    K = key;
  141. XX
  142. XX    for (regi=flen-1; --regi >= 0; ) {
  143. XX        if ((here = here + K->val) > tail)
  144. XX            here += loop;
  145. XX        while (!*here)
  146. XX            if (++here > tail)
  147. XX                here = head;
  148. XX        if (!--K->val)
  149. XX            K->val = *here;
  150. XX        K = K->next;
  151. XX        *cip++ = *here;
  152. XX        *here = 0;
  153. XX    }
  154. XX    *cip = *++tail;
  155. XX}
  156. XX
  157. XXunmix(key,cipher,plain)
  158. XXstruct keychar key[];
  159. XXchar *cipher, *plain;
  160. XX{
  161. XX    register struct keychar *K;
  162. XX    register char *here, *cip;
  163. XX    char *head, *tail;
  164. XX    int regi, flen, loop;
  165. XX
  166. XX    flen = strlen(cipher);
  167. XX
  168. XX    head = plain;
  169. XX    K = key;
  170. XX    here = head + K->val;
  171. XX    tail = head + flen -2;
  172. XX    loop = head - tail -1; /* always negative */
  173. XX    cip = cipher;
  174. XX
  175. XX    for (regi = flen -1; --regi >= 0; ) {
  176. XX        while (*here)
  177. XX            if (++here > tail)
  178. XX                here = head;
  179. XX        *here = *cip++;
  180. XX        if (!--K->val)
  181. XX            K->val = *here;
  182. XX        K = K->next;
  183. XX        if ((here = here + K->val) > tail)
  184. XX            here += loop;
  185. XX    }
  186. XX    *(tail +1) = *cip;
  187. XX}
  188. SHAR_EOF
  189. if test 3132 -ne "`wc -c cryptext.c`"
  190. then
  191. echo shar: error transmitting cryptext.c '(should have been 3132 characters)'
  192. fi
  193. #    End of shell archive
  194. exit 0
  195. Jon Boede    jon@bodedo.ucm.org   ...!{uunet,texbell}!cs.utexas.edu!bodedo!jon
  196. 7117 Wood Hollow #726, People's Republic of Austin, TX  78731  +1 512 346-3142
  197.