Don't like ads? PRO users don't see any ads ;-)
Guest

Txomon

By: a guest on Jan 14th, 2010  |  syntax: C  |  size: 2.22 KB  |  hits: 133  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define XOR(x, y) (x ^ y)
  3. #define CASES 2
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <malloc.h>
  8. #include <windows.h>
  9. #include <string.h>
  10.  
  11. int Error(int);
  12. int encriptar(char[] ,char[], char[]);
  13.  
  14. int Error(int error)
  15. {
  16.         switch(error)
  17.         {
  18.                 case 1:
  19.                         fprintf(stderr,"No has introducido una clave!, es necesaria, tanto para desencriptar, como para encriptar\n");
  20.                         system("pause");
  21.                         return error;
  22.         }
  23.         return error;
  24. }
  25.  
  26. int encriptar (char uno[],char dos[],char tres[])
  27. {
  28.         int error=0;
  29.         unsigned int x,y;
  30.         if(strlen(uno)==strlen(dos))
  31.         {
  32.                 for(x=0;x<strlen(uno);x++)
  33.                 {
  34.                         if(uno[x]==dos[x])
  35.                                 dos[x]=uno[x]+1;
  36.                         tres[x]=XOR(uno[x],dos[x]);
  37.                 }
  38.         }
  39.         else if(strlen(uno)>strlen(dos))
  40.         {
  41.                 for(x=0;x<strlen(dos);x++)
  42.                 {
  43.                         if(uno[x]==dos[x])
  44.                                 dos[x]=uno[x]+1;
  45.                         tres[x]=XOR(uno[x],dos[x]);
  46.                 }
  47.                 while(x<strlen(uno))
  48.                 {
  49.                         for(y=0;x<strlen(uno),y<strlen(dos);x++,y++)
  50.                         {
  51.                                 if(uno[x]==dos[x])
  52.                                         dos[x]=uno[x]+1;
  53.                                 tres[x]=XOR(uno[x],dos[y]);
  54.                         }
  55.                 }
  56.         }
  57.         else if(strlen(uno)<strlen(dos))
  58.                 for(x=0;x<strlen(uno);x++)
  59.                         tres[x]=XOR(uno[x],dos[x]);
  60.         tres[x]='\0';
  61.         return 0;
  62. }
  63.  
  64. int main(int argc, char *argv[])
  65. {
  66.         int i,error=0;/*Variables numéricas de control*/
  67.         int length;
  68.         char *mensaje,*clave,*secreto;
  69.  
  70.         FILE *archivo=stdout;
  71.  
  72.         for (i=1;i<argc;i++)
  73.         {
  74.                 char *op,opt;
  75.                 op=argv[i];
  76.                 if(*argv[i]=='$')/*He hecho que el simbolo para poner argumentos sea $ porque no se usa*/
  77.                 {
  78.                         opt=*(++op);
  79.                         if(islower(opt))
  80.                                 opt=toupper(opt);
  81.                         switch(opt)
  82.                         {
  83.                                 case 'C':
  84.                                         i++;
  85.                                         clave=(char *)calloc((strlen(argv[i])),sizeof(char));
  86.                                         strcpy(clave,argv[i]);
  87.                                         break;
  88.  
  89.                                 case 'M':
  90.                                         i++;
  91.                                         mensaje=(char *)calloc((strlen(argv[i])),sizeof(char));
  92.                                         secreto=(char *)calloc((strlen(argv[i])),sizeof(char));
  93.                                         strcpy(mensaje,argv[i]);
  94.                                         length=(strlen(argv[i]));
  95.                                         break;
  96.                         }
  97.                 }
  98.                 else if(strstr(op,".txt"))
  99.                                 archivo=fopen(op,"w");
  100.         }
  101.         error=encriptar(mensaje,clave,secreto);
  102.         fprintf(archivo,"El mensaje es:\n%s\n",mensaje);
  103.         fprintf(archivo,"La clave de encriptacion es:\n%s\n",clave);
  104.         fprintf(archivo,"El mensaje codificado es:\n%s\n",secreto);
  105.         fclose(archivo);
  106.         return 0;
  107. }