📄 enigma4j.c
字号:
}
if(ret>2){
ret=0;
}
switch(ret){
case 0:
putxy(TRI,ROTOR,50,8);
break;
case 1:
putxy(TRI,ROTOR,40,8);
break;
case 2:
putxy(TRI,ROTOR,30,8);
break;
}
return(ret);
}
void r_unmark() /* cancel marking of rotor */
{
putxy(' ',BG,30,8);
putxy(' ',BG,40,8);
putxy(' ',BG,50,8);
return;
}
void tty(c) /* simulates a tty connected to */
/* the Enigma */
char c;
{
int n,shift;
if(teletype){ shift=1;
fivecount=modulo(fivecount+1,5);
if(!fivecount){
shift=2;
ttystrip[78]=' ';
ttystrip[79]='\0';
}
for(n=0; n<78; n++){
ttystrip[n]=ttystrip[n+shift];
}
ttystrip[77]=c;
ttystrip[78]='\0';
writexy(ttystrip,KEY_NOR,1,2);
}
}
/* -------------------Testroutinen ----------------------------------*/
void test() /* shows some basic data */
/* (for debugging) */
{
int mode,n;
/*cls(MENU);*/
for(n=0;n<ROWS;n++)
printf("\r\n");
ScreenSetCursor(0,0);
mputs("*** ENIGMA Info ***");
#ifdef __STDC__
#ifdef NEEDSCR
printf("compiled at %s\r\n",__DATE__);
#else
printf("compiled at %s\n",__DATE__);
#endif
#endif
#ifdef DEBUG
printf("0 mod 26 = %d \n", modulo(0,26));
printf("-1 mod 26 = %d \n", modulo(-1,26));
printf("25 mod 26 = %d \n", modulo(25,26));
printf("26 mod 26 = %d \n", modulo(26,26));
printf("27 mod 26 = %d \n", modulo(27,26));
puts("translate: ");
printf("0 trans %s\n",translate(0));
printf("1 trans %s\n",translate(9));
printf("10 trans %s\n",translate(10));
printf("90 trans %s\n",translate(90));
#endif
#ifdef MSDOS
mputs("Compiled for DOS");
#ifdef ANSI_COL
mputs("ANSI.SYS Version");
#endif
#ifdef MSDOSBW
mputs("b/w Version (uses ANSI.SYS)");
#endif
#else
#ifdef VT
mputs("vt100 Version");
#endif
#ifdef VT320
mputs(" uses extended control sequences described");
mputs(" in a vt320 terminal manual");
#endif
#ifdef VT52
mputs("vt52 Version (tested on Atari)");
#endif
#ifdef UNIX
mputs("Compiled for UNIX");
#endif
#ifdef MSDOSBW
mputs(" Uses inverse b/w");
#endif
#ifdef ATARI_ST
mputs("Compiled for Atari ST");
#endif
#endif
#ifdef __GNUC__
mputs("Compiled with GCC");
#ifdef __GO32__
mputs("Go32 Version");
#endif
#ifdef __EMX__
mputs("EMX Version");
#endif
#endif
#ifdef MSDOS
#ifdef __GNUC__
mputs("Assuming 80*25 text mode");
#else
mputs("Assuming 80*25 text mode");
#endif
#else
mputs("Assuming 80*25 text mode");
#endif
#ifdef CURSES
#ifndef SWEARWORDS
mputs("Using Curses for getch(),");
mputs(" initialization of the screen");
mputs(" and cleaning up after leaving");
#else
mputs("Using curses for input and output!");
#ifdef BSDCURSES
mputs("Using the BSD version ");
#else
mputs("Using the System V version");
#endif
#endif
#endif
#ifdef SUN
mputs("Using additional <CntrlL> for cls()");
#endif
#ifdef NEEDSCR
mputs("Using additional <CR> for output");
#endif
mputs("<Return>");
fflush(stdout);
getchar();
}
/* ------------------- Encryption ----------------------------------- */
int modulo(n,mod) /* simple modulo function */
int n,mod;
{
int ret;
#ifdef DEBUG
writexy("modulo() ",BG,0,3);
#endif
ret=n;
if(mod==0)
return(0);
while(ret>=mod){
#ifdef DEBUG
writexy("modulo: Operand>= Modul ",BG,0,3);
/*fprintf(stderr,"Operand %d RET %d Modul %d ",n,ret,mod);*/
#endif
ret -=mod;
}
while(ret<0){
#ifdef DEBUG
writexy("modulo: Operand<0 ",BG,0,3);
#endif
ret+=mod;
}
#ifdef DEBUG
/* drawface(); */
writexy("modulo() finished ",BG,0,3);
#endif
return(ret);
}
int order(c) /* maps a...z -> 0..25 ab */
int c;
{
if(c<((int)'a')||c>((int)'z')){
writexy("order:UNKNOWN CHARACTER ",BG,0,4);
putxy(c,BG,40,4);
printf("--- %d ---\n",c);
/*puts("<Enter>"); */
getchar();
exit(-1);
}
return(c-(int)'a');
}
int chaos(n) /* inverse function to order() */
int n;
{
if((n<0)||n>25){
writexy("chaos:UNKNOWN CHARACTER ",BG,0,4);
putxy(n+'a',BG,40,4);
getchar();
exit(-1);
}
return(n+(int)'a');
}
void r_turn(n,width) /* move rotor n */
/* width times */
int n,width;
{
int x;
x=order(rotor[n]);
rotor[n]=chaos(modulo(x+width,26));
x=order(window[n]);
window[n]=chaos(modulo(x+width,26));
return;
}
void turn() /* move rotors */
{
int doit[4],n;
/* first calculate */
/* stepwidth for each rotor */
doit[0]=stepwidth;
if((rot_notch[0]==window[0])||(rot_notch[1]==window[1])){
doit[1]=stepwidth;
}else{
doit[1]=0;
}
if(rot_notch[1]==window[1]){
doit[2]=stepwidth;
}else{
doit[2]=0;
}
/* turn rotors simultanously */
for(n=0;n<3;n++){
r_turn(n,doit[n]);
}
return;
}
int c_back(c,r) /* encrypts backward */
int c; /* character */
int r; /* which rotor */
{
int n,m,offset,index,newchar;
char *currotor;
currotor=rot_wir[r];
#ifdef DEBUG
writexy("back: encrypt ",BG,0,3);
putxy(c,BG,24,3);
#endif
n=order(c);
offset=order(rotor[r]);
n=modulo(n+offset,26);
newchar=chaos(n);
index=-1;
for(m=0;m<25;m++){
if(currotor[m]==newchar){
index=m;
}
}
index -= offset;
index=modulo(index,26);
if(index > -1){
return(chaos(index));
}else{
writexy("back: couldn't map index back",BG,0,4);
getchar();
exit(-1);
}
}
int c_forth(c,r) /* encrypt forward */
int c; /* character */
int r; /* which rotor */
{
int n,offset,index,ret;
char *currotor;
currotor=rot_wir[r];
#ifdef DEBUG
writexy("c_forth: encrypt ",BG,0,3);
putxy(c,BG,24,3);
#endif
n=order(c);
#ifdef DEBUG
writexy("forth:calculating offset... ",BG,0,3);
#endif
offset=order(rotor[r]);
index=modulo(n+offset,26);
if(index>25){
writexy("forth: modulo returns rubbish ",BG,0,4);
printf("%d + %d = %d mod 26\n",n,offset,index);
getchar();
exit(-1);
}
ret=order(currotor[index]);
ret=modulo(ret-offset,26);
ret=chaos(ret);
return(ret);
}
int deflekt(c) /* simulates reflector */
int c;
{
int n,ret;
/*return(c);*/
#ifdef DEBUG
writexy("deflekt(): encrypting ",BG,0,3);
putxy(c,BG,30,3);
#endif
n=order(c);
ret=refl[n];
return(ret);
}
int steck(c) /* simulates plugboard */
int c;
{
int n,ret;
/*return(c);*/
#ifdef DEBUG
writexy("steck(): encrypting ",BG,0,3);
putxy(c,BG,30,3);
#endif
n=order(c);
ret=stecker[n];
return(ret);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -