Cita:
Empezado por rretamar
No me resistí a colocar un trozo de código fuente en C++ (sacado de http://www.cprogramming.com ), creo que una imagen vale más que mil palabras:
Código:
//**************************************
//INCLUDE files for :crypt.c
//**************************************
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//**************************************
// Name: CryptCode.c
// Description:Encrypts file and outputs it to a file or stdout
// By:Praveena M
//
//
// Inputs:CryptCode.exe <infile> <outfile> <key>
//
//**************************************
#define cypherbits 256 /* encryption strength in bits */
#define cypher cypherbits/(sizeof(int)*8)
int seed[cypher];
void modseed() {
int x;
for(x=1;x<cypher;x++)seed[x]=seed[x]*0x81772012+seed[x]+0x49122035+seed[x+1];
for(x=1;x<cypher;x++)seed[0]=seed[0]^seed[x];
}
int seedsum() {
int n,x;
n=0x80379251;
for(x=0;x<cypher;x++)n=n^seed[x];
return((n>>24)^((n>>16)&255)^((n>>8)&255)^(n&255));
}
char strequ(char *s1,char *s2) {
int p;
p=0;
while((s1[p]==s2[p])&&(s1[p]!=0)&&(s2[p]!=0))p++;
if(s1[p]==s2[p])return(1); else return(0);
}
int main(int argc,char *argv[]) {
char
banner[]="\x43\x6f\x64\x65\x64\x20\x62\x79\x20\x50\x72\x61\x76\x65\x65\x6e\x61"
"\x20\x6f\x66\x20\x49\x6e\x64\x69\x61"
"\x20\x28\x70\x76\x6e\x63\x61\x64\x40\x6b\x72\x65\x63\x2e\x65\x72\x6e\x65\x74\x2e\x69\x6e\x29";
char buf[2048];
int p,r,l,i,t,s,x;
char b,c,pct,lpct;
FILE *infile=NULL,*outfile=NULL;
fprintf(stderr, "%s\n", banner);
if(argc!=4){
fprintf(stderr,"use: %s <infile> <outfile> <key>\n",argv[0]);
return -1;
}
if(strequ(argv[1],"stdin"))infile=stdin; else
if((infile=fopen(argv[1],"r"))==NULL){
fprintf(stderr,"failed to open %s\n",argv[1]);
return -1;
}
if(strequ(argv[2],"stdout"))outfile=stdout; else
if((outfile=fopen(argv[2],"w"))==NULL){
fprintf(stderr,"failed to create %s\n",argv[2]);
return -1;
}
if(infile!=stdin) {
fseek(infile,0,SEEK_END);
l=ftell(infile);
rewind(infile);
} else l=0;
s=l;
t=0;
pct=0;
if(l<1)fprintf(stderr,"Encrypting data.. (%d bit cypher)\n",cypher*sizeof(int)*8);
else fprintf(stderr,"Encrypting %d bytes.. (%d bit cypher)\n",l,cypher*sizeof(int)*8);
/* bzero(seed,sizeof(seed)); */
modseed();
p=0;
while(argv[3][p]!=0){
modseed();
seed[0]=seed[0]+argv[3][p];
modseed();
p++;
}
i=0;
if(l>0){
fputc('[',stderr);
x=(l/sizeof(buf));
if(l-x*sizeof(buf)!=0)x+=1;
if(x>38)x=38;
for(p=0;p<x;p++) fputc(32,stderr);
fputc(']',stderr);
fputc(13,stderr);
fputc('[',stderr);
fflush(stderr);
}
c=1;
while(c){
r=fread(&buf,1,sizeof(buf),infile);
if(r>0) {
t+=r;
if(l>0){
lpct=pct;
pct=t/(l/x);
if(pct>lpct) {
fputc(88+32*i,stderr);
fflush(stderr);
i=1-i;
}
} else {
fputc(88+32*i,stderr);
fflush(stderr);
i=1-i;
}
p=0;
while(p<r) {
modseed();
buf[p]=buf[p]^seedsum();
p++;
}
if(fwrite(&buf,1,r,outfile)!=r) {
fprintf(stderr,"\nerror writing data\n");
return -1;
}
} else c=0;
}
if(l>0)fputc(']',stderr);
fprintf(stderr,"\nDone. Wrote %d bytes.\n",t);
}
Una pesadilla. Realmente un trabajo para dar a un programador que uno quiere despedir.
Aquí otro código más legible:
Código:
/*
XOR.cpp
Matthew Costuros
snoborder420@yahoo.com
AIM: Rpi Matty
Proper usage is XOR.exe filename Key
Encrypt the file with a key 324
XOR.exe plain.txt 324
Decrypt that file
XOR.exe plain.enc 324
Encrypt a top secret file
XOR.exe keepsafe.txt 56765
Decrypt that secret file
XOR.exe keepsafe.end 56765
*/
#include <iostream.h>
#include <string.h>
#include <stdio.h>
int XOR(char * filename, unsigned long key);
int main(int argv, char ** argc)
{
unsigned long key;
char filename[100];
//if they used command line arguments pass the filename and key to xor function
if( argv == 3)
{
if( XOR(argc[1],(unsigned int)atol(argc[2]) ) )
{
cout << "There was an error trying to encrypt/decrypt the file " << argc[1] << endl;
}
}
//other wise prompt for the key and filename
else
{
cout << "What is the filename?" << endl;
cin.getline(filename,99,'\n');
cout << "What is the key?" << endl;
cin >> key;
//tell the user about command line then call xor function
cout << "Next time you can use the command line, the format is " << argc[0] << " filename key" << endl;
if( XOR(filename,key) )
{
cout << "There was an error trying encrypt/decrpyt the file " << filename << endl;
}
}
return 0;
}
int XOR(char * filename, unsigned long key)
{
FILE * input = NULL , *output = NULL;
char * outfilename = NULL;
int len = strlen(filename);
unsigned char buffer;
if( (filename[len-4] == '.') && (filename[len-3] == 'e') && (filename[len-2] == 'n') && (filename[len-1] == 'c') )
{
// our input file is encoded then we will create a file without the .end extension
outfilename = new char[len+1]; //make room for the name+\0
strcpy(outfilename,filename); //copy the string name
outfilename[len-4] = '\0'; //put the \0 before the .enc extension to cut it off
}
else
{
outfilename = new char[len+5]; //make room for the name + .enc + \0
strcpy(outfilename,filename); //copy the file name
strncat(outfilename,".enc",4); //add the .enc extension
}
input = fopen(filename,"rb");
if( input == NULL)
{
cout << "Error opening file " << filename << endl;
delete [] outfilename; //free the memory before leaving
outfilename = NULL;
return 1;
}
output = fopen(outfilename,"wb");
if( output == NULL )
{
cout << "Error creating output file " << outfilename << endl;
delete [] outfilename; //free the mem before leaving
outfilename = NULL;
return 1;
}
while( ! feof(input) )
{
//get some data
if( fread(&buffer,sizeof(unsigned char),1,input) != 1 )
{
//if we didnt get any data, but we are not at the eof, then an error has occured
if( ! feof(input) )
{
delete [] outfilename;
outfilename = NULL;
fclose(input);
fclose(output);
return 1;
}
}
else
{
//xor that data
buffer ^= key;
//write some data
fwrite(&buffer,sizeof(unsigned char),1,output);
}
}
//close the files and free that memory
fclose(input);
fclose(output);
delete [] outfilename;
return 0;
}
Mucho mejor.
(igual sigo prefiriendo la sintaxis de Object Pascal, la encuentro mucho más amigable "programación para seres humanos" ).
|
Dos cosas , primero eso código horrible es porque primero no eta ordenado y segundo porque colocas una fuente de un cripter, eso es pura matemática compleja en el lenguaje que sea se va a ver complicado, un encriptador de por si es complejo, para mi ahí no demuestra lo bonito de c++, en todo caso quizá fue un error mencionar c++, yo hablo mas directamente con c#. :P
|