|
|
1.1 ! root 1: /* ! 2: * Routine to convert old style encrypted files into new (1983) format. ! 3: * ! 4: * cvcrypt [-r] oldfile newfile ! 5: * ! 6: * Prompts for key. ! 7: * -r option converts from new to old. ! 8: * ! 9: */ ! 10: ! 11: #include <stdio.h> ! 12: ! 13: #define BSIZE 1024 ! 14: ! 15: char *getpass(); ! 16: char iobuf[BSIZE]; ! 17: char perm[1024]; ! 18: ! 19: main(argc, argv) ! 20: int argc; ! 21: char *argv[]; ! 22: { ! 23: int ninbuf; ! 24: long count; ! 25: int fdi, fdo; ! 26: int reverse = 0; ! 27: int i; ! 28: int warning = 0; ! 29: ! 30: if((argv[1][0] == '-') && (argv[1][1] == 'r')){ ! 31: reverse = 1; ! 32: argc--; ! 33: argv++; ! 34: } ! 35: ! 36: if (argc != 3){ ! 37: fprintf(stderr, "Usage: %s [-r] oldfile newfile\n", argv[0]); ! 38: exit(1); ! 39: }else{ ! 40: crinit(getpass("Enter key:"), perm); ! 41: } ! 42: ! 43: fdi = open(argv[1], 0); ! 44: if(fdi <0){ ! 45: fprintf(stderr, "Cannot open %s for reading.\n", argv[1]); ! 46: exit(1); ! 47: } ! 48: fdo = open(argv[2], 1); ! 49: if(fdo != -1){ ! 50: fprintf(stderr, "File %s already exists.\n", argv[2]); ! 51: exit(1); ! 52: } ! 53: fdo = creat(argv[2], 0666); ! 54: if(fdo < 0){ ! 55: fprintf(stderr, "Cannot open %s for writing.\n", argv[2]); ! 56: exit(1); ! 57: } ! 58: ! 59: count = 0; ! 60: while(1){ ! 61: ninbuf = read(fdi, iobuf, BSIZE); ! 62: if(ninbuf == 0) exit(0); ! 63: if(ninbuf < 0){ ! 64: error("crypt: read error.\n"); ! 65: exit(1); ! 66: } ! 67: if(reverse == 0){ ! 68: ocrblock(perm, iobuf, ninbuf, count); ! 69: }else{ ! 70: crblock(perm, iobuf, ninbuf, count); ! 71: } ! 72: if(warning == 0){ ! 73: for(i=0; i<ninbuf; i++){ ! 74: if((iobuf[i] & 0200) != 0){ ! 75: warning = 1; ! 76: fprintf(stderr, "Wrong key or non-ascii file; conversion continued.\n"); ! 77: break; ! 78: } ! 79: } ! 80: } ! 81: if(reverse == 0){ ! 82: crblock(perm, iobuf, ninbuf, count); ! 83: }else{ ! 84: ocrblock(perm, iobuf, ninbuf, count); ! 85: } ! 86: count += ninbuf; ! 87: if(write(fdo, iobuf, ninbuf) != ninbuf){ ! 88: error("crypt: write error.\n"); ! 89: exit(1); ! 90: } ! 91: } ! 92: } ! 93: ! 94: crblock(permp, buf, nchar, startn) ! 95: char *permp; ! 96: char *buf; ! 97: long startn; ! 98: { ! 99: register char *p1; ! 100: int n1; ! 101: int n2; ! 102: int n3; ! 103: register char *t1, *t2, *t3; ! 104: char *t4; ! 105: ! 106: t1 = permp; ! 107: t2 = &permp[256]; ! 108: t3 = &permp[512]; ! 109: t4 = &permp[768]; ! 110: ! 111: n1 = startn&0377; ! 112: n2 = (startn>>8)&0377; ! 113: p1 = buf; ! 114: while(nchar--) { ! 115: n3 = t4[n1]; ! 116: *p1 = t2[(t3[(t1[(*p1+n3)&0377]+n2)&0377]-n2)&0377]-n3; ! 117: n1++; ! 118: if(n1==256){ ! 119: n1 = 0; ! 120: n2++; ! 121: if(n2==256) n2 = 0; ! 122: } ! 123: p1++; ! 124: } ! 125: } ! 126: ! 127: ! 128: /* ! 129: * Besides initializing the encryption machine, this routine ! 130: * returns 0 if the key is null, and 1 if it is non-null. ! 131: */ ! 132: crinit(keyp, permp) ! 133: char *keyp, *permp; ! 134: { ! 135: register char *t1, *t2, *t3; ! 136: char *t4; ! 137: register i; ! 138: int ic, k, temp, pf[2]; ! 139: unsigned random; ! 140: char buf[13]; ! 141: long seed; ! 142: ! 143: t1 = permp; ! 144: t2 = &permp[256]; ! 145: t3 = &permp[512]; ! 146: t4 = &permp[768]; ! 147: if(*keyp == 0) ! 148: return(0); ! 149: strncpy(buf, keyp, 8); ! 150: while (*keyp) ! 151: *keyp++ = '\0'; ! 152: buf[8] = buf[0]; ! 153: buf[9] = buf[1]; ! 154: if (pipe(pf)<0) ! 155: pf[0] = pf[1] = -1; ! 156: if (fork()==0) { ! 157: close(0); ! 158: close(1); ! 159: dup(pf[0]); ! 160: dup(pf[1]); ! 161: execl("/usr/lib/makekey", "-", 0); ! 162: execl("/lib/makekey", "-", 0); ! 163: exit(1); ! 164: } ! 165: write(pf[1], buf, 10); ! 166: if (wait((int *)NULL)==-1 || read(pf[0], buf, 13)!=13) ! 167: error("crypt: cannot generate key"); ! 168: close(pf[0]); ! 169: close(pf[1]); ! 170: seed = 123; ! 171: for (i=0; i<13; i++) ! 172: seed = seed*buf[i] + i; ! 173: for(i=0;i<256;i++){ ! 174: t1[i] = i; ! 175: t3[i] = 0; ! 176: } ! 177: for(i=0; i<256; i++) { ! 178: seed = 5*seed + buf[i%13]; ! 179: random = seed % 65521; ! 180: k = 256-1 - i; ! 181: ic = (random&0377) % (k+1); ! 182: random >>= 8; ! 183: temp = t1[k]; ! 184: t1[k] = t1[ic]; ! 185: t1[ic] = temp; ! 186: if(t3[k]!=0) continue; ! 187: ic = (random&0377) % k; ! 188: while(t3[ic]!=0) ic = (ic+1) % k; ! 189: t3[k] = ic; ! 190: t3[ic] = k; ! 191: } ! 192: for(i=0; i<256; i++){ ! 193: t2[t1[i]&0377] = i; ! 194: t4[i] = (t1[i] + t3[i]) & 0377; ! 195: } ! 196: return(1); ! 197: } ! 198: ! 199: ! 200: error(s) ! 201: char *s; ! 202: { ! 203: fprintf(stderr, s); ! 204: } ! 205: ocrblock(permp, buf, nchar, startn) ! 206: char *permp; ! 207: char *buf; ! 208: long startn; ! 209: { ! 210: register char *p1; ! 211: int n1; ! 212: int n2; ! 213: int n3; ! 214: register char *t1, *t2, *t3; ! 215: char *t4; ! 216: ! 217: t1 = permp; ! 218: t2 = &permp[256]; ! 219: t3 = &permp[512]; ! 220: t4 = &permp[768]; ! 221: ! 222: n1 = startn&0377; ! 223: n2 = (startn>>8)&0377; ! 224: p1 = buf; ! 225: while(nchar--) { ! 226: *p1 = t2[(t3[(t1[(*p1+n1)&0377]+n2)&0377]-n2)&0377]-n1; ! 227: n1++; ! 228: if(n1==256){ ! 229: n1 = 0; ! 230: n2++; ! 231: if(n2==256) n2 = 0; ! 232: } ! 233: p1++; ! 234: } ! 235: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.