#define _CRT_SECURE_NO_WARNINGS
#define XOR(x, y) (x ^ y)
#define CASES 2
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <windows.h>
#include <string.h>
int Error(int);
int encriptar(char[] ,char[], char[]);
int Error(int error)
{
switch(error)
{
case 1:
fprintf(stderr,"No has introducido una clave!, es necesaria, tanto para desencriptar, como para encriptar\n");
system("pause");
return error;
}
return error;
}
int encriptar (char uno[],char dos[],char tres[])
{
int error=0;
unsigned int x,y;
if(strlen(uno)==strlen(dos))
{
for(x=0;x<strlen(uno);x++)
{
if(uno[x]==dos[x])
dos[x]=uno[x]+1;
tres[x]=XOR(uno[x],dos[x]);
}
}
else if(strlen(uno)>strlen(dos))
{
for(x=0;x<strlen(dos);x++)
{
if(uno[x]==dos[x])
dos[x]=uno[x]+1;
tres[x]=XOR(uno[x],dos[x]);
}
while(x<strlen(uno))
{
for(y=0;x<strlen(uno),y<strlen(dos);x++,y++)
{
if(uno[x]==dos[x])
dos[x]=uno[x]+1;
tres[x]=XOR(uno[x],dos[y]);
}
}
}
else if(strlen(uno)<strlen(dos))
for(x=0;x<strlen(uno);x++)
tres[x]=XOR(uno[x],dos[x]);
tres[x]='\0';
return 0;
}
int main(int argc, char *argv[])
{
int i,error=0;/*Variables numéricas de control*/
int length;
char *mensaje,*clave,*secreto;
FILE *archivo=stdout;
for (i=1;i<argc;i++)
{
char *op,opt;
op=argv[i];
if(*argv[i]=='$')/*He hecho que el simbolo para poner argumentos sea $ porque no se usa*/
{
opt=*(++op);
if(islower(opt))
opt=toupper(opt);
switch(opt)
{
case 'C':
i++;
clave=(char *)calloc((strlen(argv[i])),sizeof(char));
strcpy(clave,argv[i]);
break;
case 'M':
i++;
mensaje=(char *)calloc((strlen(argv[i])),sizeof(char));
secreto=(char *)calloc((strlen(argv[i])),sizeof(char));
strcpy(mensaje,argv[i]);
length=(strlen(argv[i]));
break;
}
}
else if(strstr(op,".txt"))
archivo=fopen(op,"w");
}
error=encriptar(mensaje,clave,secreto);
fprintf(archivo,"El mensaje es:\n%s\n",mensaje);
fprintf(archivo,"La clave de encriptacion es:\n%s\n",clave);
fprintf(archivo,"El mensaje codificado es:\n%s\n",secreto);
fclose(archivo);
return 0;
}