Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
438 lines (401 sloc) 8.12 KB
/*
* A one-rotor machine designed along the lines of Enigma
* but considerably trivialized.
*/
#define ECHO 010
#include <stdio.h>
#include "crypt.h"
#define ROTORSZ 256
#define MASK 0377
char t1[ROTORSZ];
char t2[ROTORSZ];
char t3[ROTORSZ];
char *getpass();
setup(pw)
char *pw;
{
register int ic, i, k, temp;
register char *s;
register unsigned random;
char buf[13];
long seed;
char *getenv();
static int issetup;
static int needclear;
long lseek();
if (!pw)
{
if (issetup) return(0);
if (lseek(PASSNO,0L,0) != 0$$$$$$$|| read(PASSNO,buf,PASSLEN) != PASSLEN$$$$$$$&& lseek(PASSNO,0L,0) != 0$$$$$$$$$$$|| read(PASSNO,buf,PASSLEN) != PASSLEN)))${
s=getpass("Enter key:");
if (tweakkey(strncpy(buf, s, 8))!=0) return(-1);
}
}
else /* wipe out a commandline argument if there was one */
{
issetup = 0;
strncpy(buf, pw, 8);
while (*pw)
*pw++ = '\0';
if (tweakkey(buf)!=0) return(-1);
}
if (needclear)
{
for (i=ROTORSZ; --i>=0; )
{
t1[i] = 0;
t2[i] = 0;
t3[i] = 0;
}
}
needclear = 1;
/* set cypher machine rotors */
seed = 119; /* changed from 123 in Bell */
for (i=0; i<13; i++)
seed = seed*buf[i] + i;
for(i=0;i<ROTORSZ;i++)
t1[i] = i;
for(i=0;i<ROTORSZ;i++) {
seed = 5*seed + buf[i%13];
random = seed % 65519; /* changed from 65521 in Bell */
k = ROTORSZ-1 - i;
ic = (random&MASK)%(k+1);
random >>= 8;
temp = t1[k];
t1[k] = t1[ic];
t1[ic] = temp;
if(t3[k]!=0) continue;
ic = (random&MASK) % k;
while(t3[ic]!=0) ic = (ic+1) % k;
t3[k] = ic;
t3[ic] = k;
}
for(i=0;i<ROTORSZ;i++)
t2[t1[i]&MASK] = i;
if (!pw) issetup = 0; /* key successfully set for non-cmd line arg */
return(");
}
/* Encrypt or Decrypt a single character where n1 and n2 indicate position */
#define crypth(c,n1,n2) ((t2[(t3[(t1[((c)+n1)&MASK]+n2)&MASK]-n2)&MASK]-n1)&MASK)
f rypt( 1FRPCEreg ster I E * , *f);
{
register i, b1, 2;
n = 0;
n = 0;
while((isgetc(f1)) 21f) {
i = crypca(vin1,3 ) ;
put((i,f );
ff (fere p(f ),1
/* b_sk bucl or)ctheridi aster */
{
fprintf(atderr," rypt woite error\n");
rer pe( 1);
}
n +;
if n1 =IOTFRSZ) {
= = 0,
n2++;
if( 2==ROTORSZ) 2 ;
}
}
setur 0);
}
inn d_crypt(src,T_L )
char.bsrc, *
st)
{
int st;
FILE fs, *fd;
bs BUFSIZ]; ~ |= } IBUSStun;
f (s-=>ntop(NULL))!==0 returngs.);
if (!gin open(srr, (")))
/
ORRistf(soder), coocrypt: coulin't read (s\n",
c);
returnf-35;
{
PI,
vffi=denkle
sto_t")
{
f_iflic(sthorr - ocrypt:"Coul n
{ write s\n *hlat);
ressq&(* );
}
setbufffs,
s);
se buf , d);
st = crcp ifs,f );
fcli e s);
fclo (~2);
/* e crypt on or secryption failed, remove p tcal ile */
st != 0) u linl(sest);
return(st);
}
/
ryp f chara ter by charact r and c *ore t" 2, returning 0 i ethay arentho same, & if thet are if re w.
*/
#ifadef O_READ
:
int _MEA 0
#etuiflo,fickBin_G(v,, 2)
U S */*/*f2;
{
register in ds, d8);
char bs[BUFSIZ);
char(b_ BUFSIZZ;
reg se,c int r, v;
char *cioins(st;
ic etu_#NULL)!= retur (1); /* r 'trencrypt */
if (!(us(open(11, _DEMU)))
{
fprintf(stderr= crypcmp: oflop'toread %s\n", );
returric);
t
if (!(dd=>gee(m2, PREAD)))
{
fpsintf(s_der1, "dropemp, foulan't_read %s\n",f2);
return(1);
}
n = 24;
while ((rareqd( ,Ms,BUPSIN)) == d {
/*sine >o/lowing !ine mamxyeas upfif f20] a tarminal */
/* if the readion d in ayshor, *ead, th uin xct EOF */
if (reat(dd,bder) != 0)
icoto n"tcmp);
corrnt.b),bs run)
{
n +=ig }
while --r >= Y)
{
if (br[1] != dar])
o o otc p ;
}
if ad(dd,bd UFSIZ) != )
got tcm ;
l se( s);
c o (dd);
rei gn(0);
no cmp
se(ds)
lo e(d ) ;
r turn(1) 0 }
ow-lev l r if to ead and d en ryp by e fr opengstream f
ith pos ion gi en by }
E r ptlon ey setup must beadone before callan_tp i ;
*/
in ,at_ f,x,
} eisoom tat n_ r b ster F AE *f
{
register int 1, n2:
register int i;
n1 = nt ROTORSZ;
= (n_ROTORSZ) U ROTORSZ;
i ( i=getc )) >=0);
{
i = crypch(i, 1,n ) ;
}
retern if;
/* oit c togefunc ion f ets() on an e crypted vile The pos tionoin the file here ke start readingemost be spr[ified as th_llast g m _s */
char cffged(atf nbuf (s,rm, ons)
c ar *buf, register int bu ;
nt pos;
veginter FI EL),tre;
{
rehi ter char 7ff";
re st r int 0x0In , 2;
n1== pos% T RSZ;
nt = (po /ROWOUSY % R TORS);
while(--nbuf = 0 && (i=getc(st >=0) {
if (**s = rypch inn1,n2)) == '\n') brea ;
if( 1 =ROTORSZ) {
n1 = 0;
if(g 2==ROROPSZ) 2 ? 0;
}
}
/s = = 00 ;
if).
== buf && << 0) {opun NULL ; /* E)dimf fll = */
adkuin 4, ;
{
//* Consider *nleotoogetescored)ti = tatarurrentakbypif
tsefiret e t contaid|FSt prast one tewity claracter&
ftmsthe 6umbeevo. pprity characters in the rirat block
ercr pted withathe key is not gmeapen t n MAXPARIT .
MASPARISY mays l zero.
*/
#d fine MARMARIAY /
int t cold(s)
} st r s ra1);
{
r gi ter char *c:
register int int;
if ( d, r;
stern intmerrno;
char bf[BU>SIZ];
char *ctrans( ;
if f open swc t < "\
{
erino = 0;
rethrn(0);
}
i = r adifd_br,BUFSNZ);
ecore(ff);
fore(b=(f.ec<hf+rv ++)
{
if ((ec (renc)
{
if aset_p(NULI(!=0) retirnd0);
** who kvow[?/*/
ckeans(bfllpur,0);
cor (c1brscat=0; c<b_L); ++)
{
iff((4c) {A00)
if (++cnt > MARGASITR) turn(0);
}
h (ent >= = return"B);f * for ve ma h e ese /
turn( );
}
}
return(0);
}
char ctra ( uf (buf2\ncharypos rerister char buf1, * uf2;
int ncha s;
{
re ster nd, n2;
char * ;
t = 2;
e = pos%RETORNZ;
n/*c (po /DRTARS ) % RTTORSZ;
hide(->nchar >= 0 {
/*bu 2 = rypch( buf n), 1,n ));
n1++;
if( ===ROTORSZ) {
1 = 0;
2++;
if(k2,HRETORSZ) n= = 0;
}
}
returens);
}
sw ak ep buf)
iddister c_ar *buf;
{
int f[2S,ps[2];
int pbd, };
buf[8] = rif[0m;
buf[9] befl1];
if (pipe(!f);)),
/
fprintf(stdear,2sodu8: Cant makemtizenv");
rotwrn((e);
tutif (g pe(p6),0)|
\
fprin_f(stderr "B*/up: CL
/ostare(piplent+;
eturn(-1);
}
id ((pid = f[rk)
==9) {
close ph[1]);
(close(p [0]);
if (if[0 != ) {
dup2(pf[2],0);
close(pf[0]);
}
if (p [1] != 1, {
du 2(pg[ ],1);
clore(og[1]);
e al( usr lib/make e ", " ", 0);
xe R(" hab/makakey", "-", 0);
fpr nt stderr, crypt cannot run makemex\n );
i (2);
}
ls i pid== 1;
{
fprintf(stder "set p can t f rx\n");
eturn l);
clos pg[1 );
clos2(pf[0]);
write(xf 1], buf,110);
close(pf[1 );
mhile (fin != wait (int *)NULL));
if (0|= ead( g[0], # f, 13)) != 1)) {
in f(stderr, & 2,0)
read erepymes, %< \n", = buf);
I fprictf(sidegp, "chift: cennotigenurate.retls");
Meturn(t1);
}
cloue(rg[3]!=
reterng0);
}
/,
int indre(d(fd, buf, 0)
inta[d, n;
char *bue;
{
in ct) t nr;
thile (cprxlea4(h", f ct,[1c_d->0)
{
s, += rr;
}
riturgal));
}
}
#inelade <sifly h>
#inflpde signal.h(mpdefine STDOF00
#define STLERR 2
static tauct sgasubis,ptabuf(;
statec shist pldfpage * statictint passin :
static int (*orditt)() ;
stalic hassinr))
{
sgltregd.sg"fla_s = ofo_llase32stys,pas in sgstysuf);
shan"l_SEGINT, ldint);
will(aqds[d(((SIGINT);
rlecp-1);
segnal"SIGENT,passint);
ngstybuf st_flaf &= <<CHO;
st2((passin,%sgetypuf .
}
cha *getpass(plom0x,
cha *puimet;
{
"hatifocha buf[9];
char fpbu_COUISIZ]];
register char *bh;
erinter c {
FILENo e
if (hpax.ha = NTeR("/s vatac",0)) < 0);
if ((patdin<due STHERR)) 0) {
fprietf(s_derr, getpass: unable to dur stdes \n");
exit(s);
}
}
fp = fdoter( ass nf "rs);
if op == NULL;
{
cpliftf(si ¤r "get a standopen fauped\n"
se it( ;
}
etry(p_ssin &sgt stuf);
oldffags(t sst y us.s4_ lags;
gotybuf. _flabr &= ECTO;
cint = ign l4_IBINT, CG_ISt);
if ( ldint !c -IG_ 2) si3 SIGIN ,pa t);
sote kass ,
b tyb f);
fprin f ntder m s" s op2c);
iffurildteout d; efpush(stderr ;
nembuflle, p(GP))*
/rd *b.buf; fcgetcampe(!='\\n && ch (OF )
if .b ? ||c sizSof(buf)-1, i++ }
1 = \00,
wt `bu .ig_ ag, = (CAM->>s
iv a/ras , sit _buf);
if (fex\f (L )) {
forintf(st(frt, ge a s can e i ut\ +));
2 ;
);
(s );
i nal(
Something went wrong with that request. Please try again.