/****************************************************************************/
/* Simulacion del cifrado con el cilindro de Jefferson */
/* */
/* Jaime Suarez <mcripto@bigfoot.com> 2003 */
/* en http://www.matematicas.net */
/* */
/* Gracias a Andrés J. Díaz que me advirtió de un par de fallos */
/* en el programa. Jun 2004 */
/****************************************************************************/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRUEDAS 10
#define NLETRAS 26
#define MAXFICH 200
#define MODPOS(x,m) ( (x)%(m)>=0 ? ((x)%(m)) : ((x)%(m)+(m)) )
char cil[NRUEDAS][NLETRAS]={
"amnbvcxzpoiuytrewqlkjhgfds",
"azqsxwdcefvrgbthnyjmukilop",
"aqpkmijnulohbygvtfcrdxeszw",
"aisompfnbzcxvqdwgehrjtkylu",
"aeqswmpkbijvuhcygnxlotfzrd",
"anothwukvbemxpydrglqicjzfs",
"adtqkbnurszgmhepioflycwvxj",
"amzclyxqndtjofhipvswuekrbg",
"aegwokdvciqpfbnslutyjhzrmx",
"aylgwenqujxsbdcrmokthzfipv"
};
int main()
{
int ch,op,rueda,desp,lugar;
FILE *fi,*fo;
char noment[MAXFICH],nomsal[MAXFICH];
printf("Cifrar(c) o descifrar(d) ");
op=tolower(getchar());
getchar();
while(op!='c' && op!='d') {
printf("Pulsa c para cifrar o d para descifrar. ");
op=tolower(getchar());
getchar();
}
printf("Nombre del fichero de entrada: ");
fgets(noment,MAXFICH,stdin); noment[strlen(noment)-1]='\0';
printf("Nombre del fichero de salida : ");
fgets(nomsal,MAXFICH,stdin); nomsal[strlen(nomsal)-1]='\0';
if ((fi=fopen(noment,"r"))==NULL) {
printf("Error de lectura abriendo %s\n",noment);
exit(1);
}
if ((fo=fopen(nomsal,"w"))==NULL) {
printf("Error de escritura abriendo %s\n",nomsal);
exit(1);
}
rueda=0;
if (op=='c') desp=2; else desp=-2;
while ((ch=tolower(fgetc(fi))) != EOF) {
if (isalpha(ch)) {
lugar = index(cil[rueda],ch) - cil[rueda];
ch = cil[rueda][MODPOS(lugar+desp,NLETRAS)];
}
fputc(ch,fo);
rueda = (rueda+1)%NRUEDAS;
}
fclose(fi);
fclose(fo);
return 0;
}