Código: Seleccionar todo

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* @name: ReverseROT1 Encryption example
   @programmer: The Swash
   @date: 24 december 2010
*/   

char * Crypt(char * str, int size);
char * Decrypt(char * str, int size);
char * StrReverse(char * str, int size);


main(){
       FILE * archivo;
       int isize,bytes;
       char * buffer;
       archivo = fopen("C:\Replace.exe","rb");
       if (archivo != NULL){
                   fseek(archivo,0, SEEK_END);
                   isize = ftell(archivo);
                   rewind(archivo);
                   buffer = (char *) malloc(isize * sizeof(char));
                   bytes = fread(buffer,1,isize,archivo);
                   if (bytes = isize){
                             printf("Archivo leido correctamente \n");
                             }
                             else{
                                  printf("Error al leer el archivo \n");
                                 }                       
                   fclose(archivo);
                   }
       else{
            printf("Error al abrir el archivo\n");
            };
    
       archivo = fopen("C:\crypted.exe","wb");
       if (archivo != NULL){
                   fwrite(StrReverse(Crypt(buffer,isize),isize),1,isize,archivo);
                   fclose(archivo);
                   printf("Archivo encryptado escrito correctamente \n");
                   }
       else{
            printf("Error al abrir el archivo \n");

            }
       archivo = fopen("C:\DCrypted.exe","wb");
       if (archivo != NULL){
                   fwrite(StrReverse(Decrypt(StrReverse(Crypt(buffer,isize),isize),isize),isize),1,isize,archivo);
                   fclose(archivo);
                   printf("Archivo desencryptado correctamente");
                   }
       else{
            printf("Error al abrir el archivo \n");

            }                        
                                          
       free(buffer);
       getchar();  
}


       
char * StrReverse(char * string, int size){
     int j , n=0;
     char * temporal;
     
     temporal = (char *) malloc(size);
     for (j = size-1 ; j >= 0 ; j--) {
         temporal[n] = (char) (int) string[j];
         n++;
         }
     return temporal;
}


char * Crypt(char * str, int size){
     int i;
     char * temp;
     
     temp = (char *) malloc(size);
     for (i = 0 ; i < size ; i++){
         if ((int) str[i] != 255){
                   temp[i] = (char) (int) str[i] + 1;
                   }
         else {
              temp[i] = (char) 0;
              }
     }
     return temp;
}                       
       
char * Decrypt(char * str, int size){
     int i;
     char * temp;
     
     temp = (char *) malloc(size);
     for (i = 0 ; i < size ; i++){
         if ((int) str[i] != 0){
                   temp[i] = (char) (int) str[i] - 1;
                   }
         else {
              temp[i] = (char) 255;
              }
     }
     return temp;
} 
Hola que tal, bueno Linkgl me pidió una encryptación y pues yo habia hecho un ROT1 a mi manera en VB y Delphi y Delphi, y decidí pasarlo a C. Admito que me costó mucho más que en los lenguajes anteriores pero ahí esta con un ejemplo en main(), aclaro que funciona perfectamente con archivos y cadenas.

PD: Linkgl te dedico este trabajo y te agradezco toda la ayuda brindada.

Saludos.
En tu ventana
Y en tu ventana, gritas al cielo pero lo dices callada..
The Swash escribió:

Código: Seleccionar todo

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* @name: ReverseROT1 Encryption example
   @programmer: The Swash
   @date: 24 december 2010
*/   

char * Crypt(char * str, int size);
char * Decrypt(char * str, int size);
char * StrReverse(char * str, int size);


main(){
       FILE * archivo;
       int isize,bytes;
       char * buffer;
       archivo = fopen("C:\Replace.exe","rb");
       if (archivo != NULL){
                   fseek(archivo,0, SEEK_END);
                   isize = ftell(archivo);
                   rewind(archivo);
                   buffer = (char *) malloc(isize * sizeof(char));
                   bytes = fread(buffer,1,isize,archivo);
                   if (bytes = isize){
                             printf("Archivo leido correctamente \n");
                             }
                             else{
                                  printf("Error al leer el archivo \n");
                                 }                       
                   fclose(archivo);
                   }
       else{
            printf("Error al abrir el archivo\n");
            };
    
       archivo = fopen("C:\crypted.exe","wb");
       if (archivo != NULL){
                   fwrite(StrReverse(Crypt(buffer,isize),isize),1,isize,archivo);
                   fclose(archivo);
                   printf("Archivo encryptado escrito correctamente \n");
                   }
       else{
            printf("Error al abrir el archivo \n");

            }
       archivo = fopen("C:\DCrypted.exe","wb");
       if (archivo != NULL){
                   fwrite(StrReverse(Decrypt(StrReverse(Crypt(buffer,isize),isize),isize),isize),1,isize,archivo);
                   fclose(archivo);
                   printf("Archivo desencryptado correctamente");
                   }
       else{
            printf("Error al abrir el archivo \n");

            }                        
                                          
       free(buffer);
       getchar();  
}


       
char * StrReverse(char * string, int size){
     int j , n=0;
     char * temporal;
     
     temporal = (char *) malloc(size);
     for (j = size-1 ; j >= 0 ; j--) {
         temporal[n] = (char) (int) string[j];
         n++;
         }
     return temporal;
}


char * Crypt(char * str, int size){
     int i;
     char * temp;
     
     temp = (char *) malloc(size);
     for (i = 0 ; i < size ; i++){
         if ((int) str[i] != 255){
                   temp[i] = (char) (int) str[i] + 1;
                   }
         else {
              temp[i] = (char) 0;
              }
     }
     return temp;
}                       
       
char * Decrypt(char * str, int size){
     int i;
     char * temp;
     
     temp = (char *) malloc(size);
     for (i = 0 ; i < size ; i++){
         if ((int) str[i] != 0){
                   temp[i] = (char) (int) str[i] - 1;
                   }
         else {
              temp[i] = (char) 255;
              }
     }
     return temp;
} 
Hola que tal, bueno Linkgl me pidió una encryptación y pues yo habia hecho un ROT1 a mi manera en VB y Delphi y Delphi, y decidí pasarlo a C. Admito que me costó mucho más que en los lenguajes anteriores pero ahí esta con un ejemplo en main(), aclaro que funciona perfectamente con archivos y cadenas.

PD: Linkgl te dedico este trabajo y te agradezco toda la ayuda brindada.

Saludos.
Es impresionante como avanzas en el lenguaje, yo solo te defino algunas funciones pero tú resuelves los problemas eres muy buen programador, esta muy bien la encriptación, felicidades
//mHmm..
ese ROT1 no me gustó como lo hiciste swashito, es que no necesitas ese if, else tan siquiera

Código: Seleccionar todo

char * Crypt(char * str, int size){
     int i;
     char * temp;
     
     temp = (char *) malloc(size);
     for (i = 0 ; i < size ; i++){
         if ((int) str[i] != 255){
                   temp[i] = (char) (int) str[i] + 1;
                   }
         else {
              temp[i] = (char) 0;
              }
     }
     return temp;
}  
podias haber hecho simplemente:

Código: Seleccionar todo

char * Crypt(char * str, int size){
     int i;
     char * temp;
     
     temp = (char *) malloc(size);
     for (i = 0 ; i < size ; i++)
                   temp[i] = (str[i] + 1)%255; //cuando sea 255.. mod 255 = 0   ;)
     return temp;
}  
y te puedes ahorrar los casting de int a char y viceversa para no liar tanto el code(por lo menos en C)
un ejemplo muy sencillo es que hagas esto:

Código: Seleccionar todo

char c = 0; //caracter ASCII cero
char c = 0x00; //carcter ASCII cero aunque expresado en hex
... etc
también puedes ver el code del anterior post de la encriptación ROT que hizo m4rtyr y que yo traté de mejorar.
Un saludo!
Buenissimo Sr Swash,no puede estár mejor....incluso nos deja un ejemplo de uso.
Como algunas cosas no me gustaban de su code,decidí usar la idea de suma de caracteres a partir del cambio a char para programar un ROT tambien.

Imagen

Código: Seleccionar todo

#include <stdio.h>
#include <stdlib.h>
 char * pCryptDecrypt(char*,int);

int main(int argc, char *argv[])
 {
    printf("%s \n",strrev(pCryptDecrypt(argv[0],1)));
    getchar();	
    return 0;
 }

char * pCryptDecrypt(char * pStr,int action)
{
     //1-Crypt,2-DeCrypt
char * Buffer = (char*)malloc(strlen(pStr));
long xy;
   for(xy=0;xy<strlen(pStr);xy++)
       {
       if(action == 1){                             
           Buffer[xy] = (char)pStr[xy] + 4; 
        }else
        {
           Buffer[xy] = (char)pStr[xy] - 4;
        }                             
        }
          return Buffer;
          free(Buffer);
}
De todas formas,la suya está maravillosa...
Blog técnico dedicado a la seguridad informática y al estudio de nuevas vulnerabilidades.
Blog: http://www.seginformatica.net
Twitter: https://twitter.com/#!/p0is0nseginf
p0is0n-123 escribió:Buenissimo Sr Swash,no puede estár mejor....incluso nos deja un ejemplo de uso.
Como algunas cosas no me gustaban de su code,decidí usar la idea de suma de caracteres a partir del cambio a char para programar un ROT tambien.

Imagen

Código: Seleccionar todo

#include <stdio.h>
#include <stdlib.h>
 char * pCryptDecrypt(char*,int);

int main(int argc, char *argv[])
 {
    printf("%s \n",strrev(pCryptDecrypt(argv[0],1)));
    getchar();	
    return 0;
 }

char * pCryptDecrypt(char * pStr,int action)
{
     //1-Crypt,2-DeCrypt
char * Buffer = (char*)malloc(strlen(pStr));
long xy;
   for(xy=0;xy<strlen(pStr);xy++)
       {
       if(action == 1){                             
           Buffer[xy] = (char)pStr[xy] + 4; 
        }else
        {
           Buffer[xy] = (char)pStr[xy] - 4;
        }                             
        }
          return Buffer;
          free(Buffer);
}
De todas formas,la suya está maravillosa...
Está bien la tuya bro pero el free(Buffer); nunca se va a ejecutar :P así que esa linea no va ahí!, libera a la memoria despues de haber llamado y utilizado la función (:

Código: Seleccionar todo

int main(int argc, char *argv[])
 {
    printf("%s \n",strrev(pCryptDecrypt(argv[0],1)));
    getchar();	
    //aqui
    free(Buffer);
    return 0;
 }
//mHmm..
Responder

Volver a “Fuentes”