Source Listing 9-SEP-1997 11:10:02 DEC C V5.0-003 Page 1 30-SEP-1996 09:27:37 X$SRC:[CSO.PH]CRYPTIT.C;1 1 /***********************************************************************/ 2 /********************************************************************* 3 * This software is Copyright (C) 1988 by Steven Dorner and the 4 * University of Illinois Board of Trustees, and by CSNET. No warranties of 5 * any kind are expressed or implied. No support will be provided. 6 * This software may not be redistributed without prior consent of CSNET. 7 * You may direct questions to dorner@garcon.cso.uiuc.edu. 8 **********************************************************************/ 9 10 #define ROTORSZ 256 11 #define MASK 0377 12 13 #ifdef VMS 14 #include 53 #else X 54 #include X 55 #include X 56 #endif 57 58 #include 226 #include 595 #include 824 /*# include 825 #include */ 826 827 char t1[ROTORSZ]; 828 char t2[ROTORSZ]; 829 char t3[ROTORSZ]; 830 831 int Encrypting = 0; 832 static int ccnt, nchars, n1, n2; 833 char *Visible(); 834 835 crypt_start(pass) 836 char *pass; 837 { 838 839 n1 = 0; 840 n2 = 0; 841 ccnt = 0; 842 nchars = 0; 843 Encrypting = 1; 844 crypt_init(pass); 845 } 846 847 848 char * 849 decrypt(to, from) 850 char *to; 851 char *from; 852 { 853 char scratch[4096]; 854 char *sp; 855 int count; 856 857 count = decode((unsigned char *)scratch, (unsigned char *)from); 858 for (sp = scratch; count--; sp++) Source Listing 9-SEP-1997 11:10:02 DEC C V5.0-003 Page 2 30-SEP-1996 09:27:37 X$SRC:[CSO.PH]CRYPTIT.C;1 859 { 860 *to++ = t2[(t3[(t1[(*sp + n1) & MASK] + n2) & MASK] - n2) & MASK] - n1; 861 862 n1++; 863 if (n1 == ROTORSZ) 864 { 865 n1 = 0; 866 n2++; 867 if (n2 == ROTORSZ) 868 n2 = 0; 869 } 870 } 871 *to = '\0'; /* null-terminate */ 872 return (0); 873 } 874 875 decrypt_end() 876 { 877 Encrypting = 0; 878 } 879 880 /* single character decode */ 881 #define DEC(c) (((unsigned char)(c) - '#') & 077) 882 883 decode(to, from) 884 unsigned char *to; 885 unsigned char *from; 886 { 887 int n; 888 int chars; 889 890 chars = n = DEC(*from++); 891 892 while (n > 0) 893 { 894 /* 895 * convert groups of 3 bytes (4 Input characters). 896 */ 897 *to++ = DEC(*from) << 2 | DEC(from[1]) >> 4; 898 *to++ = DEC(from[1]) << 4 | DEC(from[2]) >> 2; 899 *to++ = DEC(from[2]) << 6 | DEC(from[3]); 900 from += 4; 901 n -= 3; 902 } 903 return (chars); 904 } 905 906 crypt_init(pw) 907 char *pw; 908 { 909 int ic, i, k, temp; 910 unsigned random; 911 char buf[13]; 912 int seed; 913 char *crypt(); 914 915 /* must reinitialize the arrays */ Source Listing 9-SEP-1997 11:10:02 DEC C V5.0-003 Page 3 30-SEP-1996 09:27:37 X$SRC:[CSO.PH]CRYPTIT.C;1 916 for (i = 0; i < ROTORSZ; i++) 917 t1[i] = t2[i] = t3[i] = 0; 918 919 strncpy(buf, crypt(pw, pw), 13); 920 921 seed = 123; 922 for (i = 0; i < 13; i++) 923 seed = seed * buf[i] + i; 924 for (i = 0; i < ROTORSZ; i++) 925 t1[i] = i; 926 for (i = 0; i < ROTORSZ; i++) 927 { 928 seed = 5 * seed + buf[i % 13]; 929 random = seed % 65521; 930 k = ROTORSZ - 1 - i; 931 ic = (random & MASK) % (k + 1); 932 random >>= 8; 933 temp = t1[k]; 934 t1[k] = t1[ic]; 935 t1[ic] = temp; 936 if (t3[k] != 0) 937 continue; 938 ic = (random & MASK) % k; 939 while (t3[ic] != 0) 940 ic = (ic + 1) % k; 941 t3[k] = ic; 942 t3[ic] = k; 943 } 944 for (i = 0; i < ROTORSZ; i++) 945 t2[t1[i] & MASK] = i; 946 } 947 948 /*********************************************************************** 949 * encrypts a string 950 * first byte of string is encoded length of string 951 * returns length of encoded string 952 ***********************************************************************/ 953 encryptit(to, from) 954 char *to; 955 char *from; 956 { 957 char scratch[4096]; 958 char *sp; 959 960 sp = scratch; 961 for (; *from; from++) 962 { 963 *sp++ = t2[(t3[(t1[(*from + n1) & MASK] + n2) & MASK] - n2) & MASK] - n1; 964 n1++; 965 if (n1 == ROTORSZ) 966 { 967 n1 = 0; 968 n2++; 969 if (n2 == ROTORSZ) 970 n2 = 0; 971 } 972 } Source Listing 9-SEP-1997 11:10:02 DEC C V5.0-003 Page 4 30-SEP-1996 09:27:37 X$SRC:[CSO.PH]CRYPTIT.C;1 973 return (encode(to, scratch, sp - scratch)); 974 } 975 976 /* 977 * * Basic 1 character encoding function to make a char printing. 978 */ 979 #define ENC(c) (((unsigned char)(c) & 077) + '#') 980 981 encode(out, buf, n) /* output a line of binary (up to 45 chars) 982 * in */ 983 int n; /* a readable format, converting 3 chars to 4 */ 984 char *buf; 985 char *out; 986 { 987 int i; 988 char *outStart; 989 990 outStart = out; 991 *out++ = ENC(n); 992 993 for (i = 0; i < n; buf += 3, i += 3, out += 4) 994 threecpy((unsigned char *)out, (unsigned char *)buf); 995 996 /* null terminate */ 997 *out = '\0'; 998 return (out - outStart); 999 } 1000 1001 threecpy(to, from) 1002 unsigned char *to; 1003 unsigned char *from; 1004 { 1005 int c1, c2, c3, c4; 1006 1007 c1 = *from >> 2; 1008 c2 = (*from << 4) & 060 | (from[1] >> 4) & 017; 1009 c3 = (from[1] << 2) & 074 | (from[2] >> 6) & 03; 1010 c4 = from[2] & 077; 1011 *to++ = ENC(c1); 1012 *to++ = ENC(c2); 1013 *to++ = ENC(c3); 1014 *to = ENC(c4); 1015 } Command Line ------- ---- CC/NOANSI_ALIAS/ANALYSIS_DATA/ASSUME=(ACCURACY_SENSITIVE,ALIGNED_OBJECTS,WRITABLE_STRING_LITERALS) /CROSS_REFERENCE/NOCOMMENTS/DIAGNOSTICS/DEBUG=(SYMBOLS,TRACEBACK/ENDIAN=LITTLE /EXTERN_MODEL=RELAXED_REFDEF/FLOAT=(G_FLOAT)/GRANULARITY=QUADWORD/INSTRUCTION_SET=FLOATING_POINT /LINE_CONTROL/L_DOUBLE_SIZE=128/LINE_DIRECTIVES/LIST/NOMACHINE_CODE/MEMBER_ALIGNMENT /NAMES=UPPERCASE/NESTED_INCLUDE_DIRECTORY=INCLUDE_FILE/OBJECT/NOOPTIMIZE /PREFIX=(ALL_ENTRIES)/PSECT_MODEL=NOMULTILANGUAGE/ROUNDING_MODE=NEAREST /SHOW=(HEADER,SOURCE)/SIGNED_CHAR/STANDARD=VAXC/REENTRANCY=TOLERANT/NOTRAP_UNINITIALIZED_VARIABLES /WARNINGS .