home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
ddjmag
/
ddj8605.arc
/
CRYPTO.MAY
next >
Wrap
Text File
|
1986-05-31
|
19KB
|
833 lines
è################### L I S T I N G 1 ####################
/*
** cypher.c File Cypher Program by F.A.Scacchitti 9/11/85
**
** Written in Small-C Version 2.10 or later
**
** Copies from original file to encrypted file
** using cypher key(s) passed to encode or decode.
*/
#include <stdio.h>
#define BUFSIZE 16384
int fdin, fdout; /* file i/o channel pointers */
int n, count;
char *inbuf, *key;
main(argc,argv) int argc, argv[]; {
inbuf = malloc(BUFSIZE);
/*
** Open file streams
*/
if(argc < 4) {
printf("\ncypher usage: cypher <source file> <new file>
<key1> <key2> . . . <keyN> <CR>\n");
exit();
}
if((fdin = fopen(argv[1],"r")) == NULL) {
printf("\nUnable to open %s\n", argv[1]);
exit();
}
if((fdout = fopen(argv[2],"w")) == NULL) {
printf("\nUnable to create %s\n", argv[2]);
exit();
}
/*
** Read file - encode it - write new file
*/
do {
printf("-reading file\n");
count = read(fdin,inbuf,BUFSIZE);
n=3;
while(n++ <argc){
key = argv[n-1];
cypher(inbuf,count,key);
}
printf("-writing %d byte file\n\n", count);è
write(fdout,inbuf,count);
} while(count == BUFSIZE);
/* close up shop */
fclose(fdin);
fclose(fdout);
}
.paè################### L I S T I N G 2 ####################
/* cypher1.c Cypher module by F.A.Scacchitti
** 10/10/85
**
** Simple cypher module - encodes directly with user keys
**
*/
#include <stdio.h>
static int i, n, keylength;
cypher1(buffer, num, code) char *buffer, *code; int num;{
/*
** get keylength for each key
*/
keylength = 0;
while(code[keylength++] != NULL);
keylength--;
/*
** encrypt the file with each key
*/
printf("-encoding/decoding buffer\n");
for(i=0; i<=num; i++)
buffer[i] = buffer[i] ^ code[i % keylength];
}
.paè################### L I S T I N G 3 ####################
/* cypher2.c Cypher module by F.A.Scacchitti
** 10/11/85
**
** Complex cypher module - generates a key of some prime length
** between 1024 and 2028 bytes then
** encrypts the buffer with this key
**
*/
#include <stdio.h>
#define NEWBUF 2000
#define NUMPRIMES 50
static int i, n, index, length, sum, keylength;
static char *newkey;
static int prime[] = {1009, 1999, 1013, 1997, 1019,
1993, 1021, 1987, 1031, 1979,
1033, 1973, 1039, 1951, 1049,
1949, 1051, 1933, 1061, 1931,
1063, 1913, 1069, 1907, 1087,
1901, 1091, 1889, 1093, 1879,
1097, 1877, 1103, 1873, 1109,
1871, 1117, 1867, 1123, 1861,
1129, 1847, 1151, 1831, 1153,
1823, 1163, 1813, 1171, 1803};
cypher1(buffer, num, code) char *buffer, *code; int num;{
/*
** allocate a buffer for the generated key
*/
newkey = malloc(NEWBUF);
/*
** get keylength and sumcheck for each key
*/
keylength = sum = 0;
while((n = code[keylength]) != NULL){
sum += n;
keylength++;
}
/*
** Select a prime and generate a new key that length
*/
length = prime[sum % NUMPRIMES];
printf("-generating a %d byte key\n",length);
for(i=0; i<length; i++){è index = i % keylength;
sum = code[index] + sum & 255;
newkey[i] = code[index] ^ sum;
}
/*
** encrypt the file with the generated key
*/
printf("-encoding/decoding buffer\n");
for(i=0; i<=num; i++)
buffer[i] = buffer[i] ^ newkey[i % length];
/*
** get rid of the buffer
*/
cfree(newkey);
}
.paè################### L I S T I N G 4 ####################
/* cypher3.c Cypher module by F.A.Scacchitti
** 11/09/85
**
** Complex cypher module - generates a key of some prime length
** between 1024 and 2028 bytes then
** encrypts the buffer with this key
** or
** if key starts with a '-' (dash)
** calculate a transposition block size
** and invert (transpose) the file in
** this size blocks
**
*/
#include <stdio.h>
#define DASH 45
#define NEWBUF 2000
#define NUMPRIMES 50
static int i, j, n, index, length, sum, keylength;
static char *tbuff, c;
static int prime[] = {1009, 1999, 1013, 1997, 1019,
1993, 1021, 1987, 1031, 1979,
1033, 1973, 1039, 1951, 1049,
1949, 1051, 1933, 1061, 1931,
1063, 1913, 1069, 1907, 1087,
1901, 1091, 1889, 1093, 1879,
1097, 1877, 1103, 1873, 1109,
1871, 1117, 1867, 1123, 1861,
1129, 1847, 1151, 1831, 1153,
1823, 1163, 1813, 1171, 1803};
cypher(buffer, num, code) char *buffer, *code; int num;{
/*
** allocate a buffer for the new key or transposition
*/
tbuff = malloc(NEWBUF);
/*
** get keylength and sumcheck for each key
*/
keylength = sum = 0;
while((n = code[keylength]) != NULL){
sum += n;
keylength++;
}
/*
** do we transpose or encode ?
*/è
if((c = *code) == DASH)
transpose(buffer, num, code);
else
encode(buffer, num, code);
/*
** get rid of the buffer
*/
cfree(tbuff);
}
/*
** Here's where we transpose
*/
transpose(buffer, num, code) char *buffer, *code; int num;{
length = (((sum + keylength) % 16) & 15) + 2;
printf("-transposing file by %d\n",length);
index = 0;
do{
for(i = 0; i < length; i++){
j = length - i - 1;
tbuff[j] = buffer[index + i];
}
for(i = 0; i < length; i++){
buffer[index + i] = tbuff[i];
}
index += length;
}while(index < count);
}
/*
** Here's where we encode
*/
encode(buffer, num, code) char *buffer, *code; int num;{
/*
** Select a prime and generate a new key that length
*/
length = prime[sum % NUMPRIMES];
printf("-generating a %d byte key\n",length);
for(i=0; i<length; i++){è index = i % keylength;
sum = code[index] + sum & 255;
tbuff[i] = code[index] ^ sum;
}
/*
** encrypt the file with the generated key
*/
printf("-encoding/decoding buffer\n");
for(i=0; i<=num; i++)
buffer[i] = buffer[i] ^ tbuff[i % length];
}
.paè################### L I S T I N G 5 ####################
/*
** fv.c File View/Compare Program by F.A.Scacchitti 9/11/85
**
** Written in Small-C Version 2.10 or later
**
** Dumps contents of single file to screen
** or
** Dumps contents of 2 files and xored difference
**
** Displays in hex and ascii form
*/
#include <stdio.h>
#define BUFSIZE 1024
int fdin1, fdin2; /* file i/o channel pointers */
int i, j, k, val, count, total, offset, numdisp;
char *inbuf1, *inbuf2, *difbuf, c;
main(argc,argv) int argc, argv[]; {
switch (argc) {
case 2:
case 3:
inbuf1 = malloc(BUFSIZE);
if((fdin1 = fopen(argv[1],"r")) == NULL) {
printf("\nUnable to open %s\n", argv[1]);
exit();
}
numdisp = 1;
if(argc == 2) break;
case 3:
inbuf2 = malloc(BUFSIZE);
difbuf = malloc(BUFSIZE);
if((fdin2 = fopen(argv[2],"r")) == NULL) {
printf("\nUnable to open %s\n", argv[2]);
exit();
}
numdisp = 3;
break;
default:
printf("\nfv usage: fv <file1> - dump file\n");
printf(" fv <file1> <file2> - compare 2 files\n");
exit();
}
total = offset = 0;
printf("\n");
do {
count = read(fdin1,inbuf1,BUFSIZE);
if(argc >= 3){è read(fdin2,inbuf2,BUFSIZE);
for(i=0; i< count; i++)
difbuf[i] = inbuf1[i] ^ inbuf2[i];
}
for(i=0; i< count; i+=16){
for(k=1; k <= numdisp; k++){
switch (k) {
case 2:
offset = BUFSIZE;
break;
case 3:
offset = 2 * BUFSIZE;
break;
default:
offset = 0;
break;
}
if(k < 3)
printf("f-%d", k);
else
printf("dif");
printf(" %04x ",i+total);
for(j=0; j<=15; j++){
val = inbuf1[i + j + offset];
printf("%02x ",val < 0 ? val - 65280 : val);
if((c = bdos(6,255)) == 19){ /* hold on ^S */
if((c = getchx()) == 3)
exit(); /* exit on ^C */
} /* continue on ^Q */
}
printf(" ");
for(j=0; j<=15; j++){
c = inbuf1[i + j + offset];
if(c > 31)
putchar(c);
else
if(c==0)
putchar(61);
else
putchar(94);
}
printf("\n");
if(k == 3) printf("\n");
}
}è total += count;
} while(count == BUFSIZE);
/* close up shop */
fclose(fdin1);
if(argc == 3)
fclose(fdin2);
}
.paè################### L I S T I N G 6 ####################
/*
** fstat.c File Statistics Program by F.A.Scacchitti 10/8/85
**
** Written in Small-C Version 2.10 or later
**
** Scans file and displays distribution of
** characters.
** Calculates and displays mean, mode, median
** and range of file.
** Displays histogram of distribution.
*/
#include <stdio.h>
#define BUFSIZE 16384
int fdin; /* file pointer */
int i, j, temp, value, count, total, *file, *sorted;
int sum, hisum, meansum, himeansum, mean, eflag, changing;
int median, oddmedian, range, min, max, mode;
int *data, scale;
char c, *inbuf;
main(argc,argv) int argc, argv[]; {
if(argc < 2) {
printf("\nfstat usage: fstat <input file>\n");
exit();
}
if((fdin = fopen(argv[1],"r")) == NULL) {
printf("\nUnable to open file %s\n",argv[1]);
exit();
}
inbuf = calloc(BUFSIZE,1);
file = calloc(256,2);
sorted = calloc(256,2);
data = calloc(17,2);
eflag = FALSE;
sum = hisum = meansum = himeansum = mean = mode = j = 0;
printf("reading the file-");
do {
count = read(fdin,inbuf,BUFSIZE);
for(i=0; i< count; i++){
value = inbuf[i];
if(value < 0)
value = 256 + value;è file[value]++;
if(++sum == 10000){
hisum++;
sum =0;
}
if((meansum += value) >= 10000){
himeansum++;
meansum -= 10000;
}
}
} while(count == BUFSIZE);
/*
** Calculate the mean
*/
printf("calculating mean-");
do{
if((meansum -= sum) < 0)
if(himeansum > 0){
himeansum--;
meansum += 10000;
}else{
meansum += sum;
eflag = TRUE;
mean--;
}
if((himeansum -= hisum) < 0){
himeansum += hisum;
eflag = TRUE;
}else{
mean++;
}
}while(eflag == FALSE);
/*
** Calculate range, find mode min and max, fill the sorted array
*/
printf("calculating range-");
min = max = file[0];
for(i = 0; i <= 255; i++){
sorted[i] = file[i];
if(file[i] > max){
max = file[i];
mode = i;
}
if(file[i] < min)
min = file[i];
}
range = max - min + 1;
è/*
** Sort the sorted array to calculate median
*/
printf("sorting the array");
changing = TRUE;
while(changing){
changing = FALSE;
for(i = 0; i <= 254; i++)
if(sorted[i] > sorted[i+1]){
temp = sorted[i];
sorted[i] = sorted[i+1];
sorted[i+1] = temp;
changing = TRUE;
}
}
median = (sorted[128] + sorted[127]) / 2;
oddmedian = (sorted[128] + sorted[127]) % 2;
/*
** Display the results
*/
printf("\n 0 1 2 3 4 5 6 7
8 9 A B C D E F\n");
for(i = 0; i <= 255; i++) {
printf("%5d", file[i]);
chkkbd();
}
printf("\n %d%04d characters read from file %s\n",
hisum, sum, argv[1]);
printf("file mean = %d ",mean);
if((himeansum || meansum) > 0)
printf("%d%04d/%d%04d",himeansum,meansum,hisum,sum);
printf(" mode = %d ( %x hex)", mode, mode);
printf("\n");
printf("file median = %d", median);
if(oddmedian)
printf(" 1/2 ");
else
printf(" ");
printf(" file range = %d [ min = %d max = %d ]\n",
range, min, max,);
printf("\nDepress spacebar to display histogram ");
getchar();
/*
** Sum the data in 16 groups of 16 elements and find max. value
*/
max = 0;è for(i = 1; i <= 16; i++){
for(j = 0; j <= 15; j++)
data[i] += file[(i - 1) * 16 + j];
if(data[i] > max)
max = data[i];
}
/*
** Calculate scaling for plot
*/
scale = max / 50;
temp = max % 50;
if(temp / scale > 7)
scale++;
printf(" scale = %4d\n\n", scale);
/*
** Print data and plot of histogram
*/
for(i = 0; i <= 15; i++){
printf(" %3d to %3d = %5d ||",i * 16, (i * 16) + 15,
data[i + 1]);
temp = data[i + 1] / scale;
if(data[i + 1] % scale > 0)
temp++;
while(temp-- > 0)
printf("*");
printf("\n");
}
/*
** close up shop
*/
fclose(fdin);
}
chkkbd(){ char c;
if((c = bdos(6,255)) == 19) /* hold on ^S */
if((c = getchx()) == 3)
exit(); /* exit on ^C */
} /* continue */
.paè################### L I S T I N G 7 ####################
/*
** makef.c File Generator Program by F.A.Scacchitti 10/7/85
**
** Written in Small-C Version 2.10 or later
**
** Creates a seqential file of characters from
** 0 to 255 in blocks of 255 bytes.
** The sequential characters can be replaced
** with any single character desired.
*/
#include <stdio.h>
#define BUFSIZE 256
int fdout; /* file i/o channel pointers */
int i, n, num;
char *outbuf, value;
main(argc,argv) int argc, argv[]; {
/*
** Allocate memory for buffer
*/
outbuf = malloc(BUFSIZE);
/*
** Check arguments passed and open file stream
*/
if(argc < 3) {
printf("\nmakef usage: makef <new file> <nnnn> [ddd]\n");
printf(" nnnn = file size in 256 byte blocks\n");
printf(" ddd = optional alternate value in decimal\n");
exit();
}
if((fdout = fopen(argv[1],"w")) == NULL) {
printf("\nUnable to create %s\n", argv[1]);
exit();
}
/*
** Convert file size argument to integer
*/
if((n = atoi(argv[2])) == NULL) exit();
/*
** Fill the buffer with 0 to 255 sequence
*/
for(i = 0; i <=255; i++)
outbuf[i] = i;
/*è** Refill the buffer with a single character if directed by argument
*/
if(argc == 4)
if((value = atoi(argv[3])) < 256)
for(i = 0; i <=255; i++)
outbuf[i] = value;
/*
** Write blocks to file
*/
for(i=1; i <= n; i++)
if((num = write(fdout,outbuf,BUFSIZE)) < BUFSIZE) exit();
/*
** Close up shop
*/
fclose(fdout);
}
.paè################### L I S T I N G 8 ####################
/*
** sp.c Search Pattern Program by F.A.Scacchitti 10/15/85
**
** Written in Small-C Version 2.10 or later
**
** Searches file for repetitive pattern.
**
**
*/
#include <stdio.h>
#define BUFSIZE 16384
int fdin; /* file i/o channel pointers */
int i, j, n, block, start, depth, count, limit;
char *c, *inbuf;
main(argc,argv) int argc, argv[]; {
/*
** Set defaults
*/
block = 128;
depth = 4;
start = 0;
/*
** Allocate memory for buffer
*/
inbuf = malloc(BUFSIZE);
/*
** Check arguments passed and open file stream
*/
if(argc < 2) {
printf("\nsp usage: sp <source file> [nnnn] [dddd] [ssss] [x]\n");
printf(" nnnn = block size to search default = 128\n");
printf(" dddd = minimum depth of comparison default = 4\n");
printf(" ssss = starting point in buffer default = 0\n");
printf(" x - any char. gen's difference buffer ((n+1)-n)\n");
exit();
}
if((fdin = fopen(argv[1],"r")) == NULL) {
printf("\nUnable to open %s\n", argv[1]);
exit();
}
/*
** Convert optional inputs to integer and implement
*/
if(argc > 2)è if((n = atoi(argv[2])) != NULL)
block = n;
if(argc > 3)
if((n = atoi(argv[3])) != NULL)
depth = n;
if(argc > 4)
if((n = atoi(argv[4])) != NULL)
start = n;
/*
** Fill the buffer with as much as possible
*/
count = read(fdin,inbuf,BUFSIZE);
limit = count - depth;
/*
** If there's a sixth argument, convert the file to numerical sequence
*/
if(argc > 5){
for(i = 0; i < count - 1; i++)
inbuf[i] = inbuf[i + 1] - inbuf[i];
}
for(i = start; i < block; i++){
printf("%c", (i % 10 == 0) ? '|' : '.');
chkkbd();
for(j = i + 1; j <= limit; j++)
if(inbuf[i] == inbuf[j]){
if((n = chkdepth(i, j, 0)) >= depth)
printf("\nmatch at %d (%x hex) and %d (%x hex) -
%d deep\n", i, i, j, j, n);
if(n >= block) exit();
}
}
/*
** Close up shop
*/
printf("\n"); /* Flush print buffer */
fclose(fdin);
}
chkdepth(pointer, offset, k)int pointer, offset, k;{
while(inbuf[pointer] == inbuf[offset] && k < count){
pointer++;
offset++;
k++;
}
return(k);è}
chkkbd(){
char c;
if((c = bdos(6,255)) == 19){ /* hold on ^S */
if((c = getchx()) == 3)
exit(); /* exit on ^C */
} /* continue */
}