Sunday, February 13, 2011

Encrypt-a-file

    //**************************************
    //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);
    }

Okay so I have been checking out this code and all I can say is wow and I can't wait until I get to this level. The formatting got nuked when I copied it into the post editor but looks great in Visual Studio 10. It provides 256 bit encryption and while I am not up on this enough to go into how secure it is I have really enjoyed examining the code. File goes in with a key and out comes the encrypted form. Much thanks goes out to the author.

42 comments:

  1. Looks good! About how long has it taken?

    ReplyDelete
  2. That's just a wall of matrix to me, buddy. Looks cool, though.

    ReplyDelete
  3. very cool. i used to program java a couple years ago, and im getting back into it when i go to college.

    ReplyDelete
  4. Dont really understand programming but thanks!

    ReplyDelete
  5. ah good old c brings back memories would have done that in C++ if I had the choice or Python!

    ReplyDelete
  6. is this C or C++? It looks like C++ to me but I'm not very experienced xD

    ReplyDelete
  7. intense, i've only taken a few c++ classes...

    ReplyDelete
  8. Is it sad that I can understand whats going on here.

    ReplyDelete
  9. I used BASIC in high school and made connect 4 but thats about it lolz.

    ReplyDelete
  10. Nice script, will test it out!

    ReplyDelete
  11. imma try this and let you know how it goes haha

    ReplyDelete
  12. Very cool code. Encryption is something everyone needs more of to get any privacy these days.

    ReplyDelete
  13. Nice. Too bad i don't understand nothing of Mathematics, programming is so cool.

    ReplyDelete
  14. i like the programming but this very long.

    ReplyDelete
  15. wow! one day i will programming like that xD

    ReplyDelete
  16. On an unrelated note: reminded me I wanted to buy a cup with sign. Sigh :[

    ReplyDelete
  17. kinda makes me want to get back into programming.

    ReplyDelete
  18. I started taking a programming and source class in college and quickly got confused. If you can make heads and tails of that post, I applaud you!

    Following/contributing!

    Garage Zoku

    ReplyDelete
  19. Thanks for sharing this, I'll be testing this out !

    ReplyDelete
  20. I love C however I love not to programming C anymore haha

    ReplyDelete
  21. My knowledge of this level of encryption only goes as far as what I learned about the WikiLeaks cables encryption. I understand it is almost impossible to be cracked (imagine the extremely unlikely scenario of it being cracked on the first attempt!).

    ReplyDelete
  22. Encryption is such an interesting part of the programming world, it goes to show how versatile of a tool computers can be.

    ReplyDelete
  23. This must have taken a while to code, nice share.

    ReplyDelete
  24. i took some C++ back in HS, but this now looks confusing to me lol

    ReplyDelete
  25. i've never been able to really understand coding but find it fascinating nonetheless.

    ReplyDelete
  26. haven't written any code in awhile, looks pretty sweet though. I usually use truecrypt for encryption though.

    ReplyDelete
  27. Im writing on C++ this times too. A bit easier codes though.

    ReplyDelete
  28. really look forward to doing stuff like this in the future

    ReplyDelete
  29. Very handy code snippet, thanks for sharing, I really wish I knew C coding better than I do :(

    ReplyDelete
  30. I have always been fascinated by encryption technology and this provides a wonderful insight into the process. Thank you.

    ReplyDelete
  31. thaha i would like to understand this

    ReplyDelete
  32. Now is it really meaningfull to encrypt your information? Do people actually try to read this all?
    Ah well I'm just a little too lazy I don't mind being watched by ''big brother''
    :)

    ReplyDelete
  33. This code is so much more complex than the temp converter... :/

    ReplyDelete