sexta-feira, 27 de novembro de 2009

XOR encryption looks realy funny (: -> encrypt.c

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

#define crpt(byte,key) (byte^key)

int
main (int argc, char *argv[])
{
 int f1, f2, n;
 char unbuf;
 
 if (argc < 4) 
  error (-1, EINVAL, "Error, argument fault",
     argv[0]);
 
 if ((strlen (argv[3])) <= 2)
  error (-1, EINVAL, "Error, encyrpt key is "
    "too short (less then 3 characters)");
 
 if ((f1 = open (argv[1], O_RDONLY)) == -1) 
  err (-1, "Error, while trying to open "
    "file %s for read", argv[1]);

 if ((f2 = creat (argv[2], 0644)) == -1)
  err (-1, "Error, while trying to create/open "
    "file %s for write", argv[2]);

 while ((n = read (f1, &unbuf, 1)) > 0) 
 {
  char *key = argv[3];
  
  if ((key+1) == '\0')
   key = argv[3];
  unbuf = crpt(unbuf, *key);

  if ((write (f2, &unbuf, 1)) == -1)
   err(-1, "Error while trying to write "
    "in file %s", argv[2]);
  key++; 
 }
  
 return 0;
}

usage: ./ecrypt source_file encrypted_file crypt_key
This little program will take the source_file, encrypt it, and write
the result in the encrypted_file using the crypt_key

You can decrypt the encrypted_file using the same crypt_key.. like
./encrypt encrypted_file decrypted_file same_crypt_key

this is funny! :-)

Nenhum comentário:

Postar um comentário