base 64 encoding via c
Posted 07-25-2013 at 09:04 AM by sumeet inani
I came across some anti-virus in windows that detect programs like cmdow , fgdump etc. as threat & quarantine/remove it without asking.
So I wrote this c program that takes a byte from input-binary & writes it to ascii-hex-text-file while encoding .
In decoding takes two alphanumeric characters from input-text & writes it as single char to output-binary.
Compile it in your distro using
gcc program-file-name.c
Here is the code
This way I keep my original binary & double-sized-text-backup in my USB stick
USAGE
./application input-file output-file switch
e=encoding
d=decoding
TAKE CARE
use <=8 character filenames without absolute paths for safety
So I wrote this c program that takes a byte from input-binary & writes it to ascii-hex-text-file while encoding .
In decoding takes two alphanumeric characters from input-text & writes it as single char to output-binary.
Compile it in your distro using
gcc program-file-name.c
Here is the code
Code:
#include"sys\types.h" #include<fcntl.h> #include<stdio.h> #include"sys\stat.h" #include<process.h> #include<string.h> char out_buffer[1028]; void encode(char *inp_buffer,int bytes) { int i; unsigned char b; for (i=0;i<bytes;i++) { b=inp_buffer[i]; switch((b/16)) { case 10:out_buffer[i*2]='a';break; case 11:out_buffer[i*2]='b';break; case 12:out_buffer[i*2]='c';break; case 13:out_buffer[i*2]='d';break; case 14:out_buffer[i*2]='e';break; case 15:out_buffer[i*2]='f';break; default:out_buffer[i*2]=(b/16)+48; } switch((b%16)) { case 10:out_buffer[(i*2)+1]='a';break; case 11:out_buffer[(i*2)+1]='b';break; case 12:out_buffer[(i*2)+1]='c';break; case 13:out_buffer[(i*2)+1]='d';break; case 14:out_buffer[(i*2)+1]='e';break; case 15:out_buffer[(i*2)+1]='f';break; default:out_buffer[(i*2)+1]=(b%16)+48; } printf("-%u=%c%c-",b,out_buffer[i*2],out_buffer[(i*2)+1]); } } void decode(char *inp_buffer,int bytes) { char ch; int i,tmp; printf("\n decoding begins"); for(i=0;i<(bytes/2);i++) { tmp=0; switch((char)inp_buffer[2*i]) { case 'a':inp_buffer[2*i]=10;break; case 'b':inp_buffer[2*i]=11;break; case 'c':inp_buffer[2*i]=12;break; case 'd':inp_buffer[2*i]=13;break; case 'e':inp_buffer[2*i]=14;break; case 'f':inp_buffer[2*i]=15;break; default://printf("\n:%d:%c:=inp_buffer[%d]\n",(char)inp_buffer[2*i],(char)inp_buffer[2*i],i); inp_buffer[2*i]=(char)inp_buffer[2*i]-'0'; //printf("\nnow it has become %d",(char)inp_buffer[2*i]); } tmp=16*(char)inp_buffer[2*i]; switch(inp_buffer[2*i+1]) { case 'a':inp_buffer[2*i+1]=10;break; case 'b':inp_buffer[2*i+1]=11;break; case 'c':inp_buffer[2*i+1]=12;break; case 'd':inp_buffer[2*i+1]=13;break; case 'e':inp_buffer[2*i+1]=14;break; case 'f':inp_buffer[2*i+1]=15;break; default:inp_buffer[2*i+1]=(char)inp_buffer[2*i+1]-'0'; } tmp=tmp+inp_buffer[2*i+1]; printf("\n%d=tmp",tmp); out_buffer[i]=(char)tmp; //printf("-%c%c=%u-",(char)inp_buffer[2*i],(char)inp_buffer[2*i+1],tmp); //out_buffer[i]=(char)tmp; } out_buffer[i]=0; } void main( int argc, char *argv[ ] ) { char inp_buffer[ 512 ], source [ 128 ], target [ 128 ] ; int inhandle, outhandle, bytes; if(argc<4) { printf("\nCommand should be \"program source destination <switch>\"\nswitch=e,d\nBye!"); exit(0); } printf ( "\nsource file name:%s:",argv[1] ) ; inhandle = open (argv[1], O_RDONLY | O_BINARY ) ; if ( inhandle == -1 ) { puts ( "\nCannot open input file" ) ; exit(0) ; } printf ( "\ntarget file nam:%s:",argv[2] ) ; outhandle = open ( argv[2], O_CREAT | O_BINARY | O_WRONLY,S_IWRITE ) ; if ( outhandle == -1 ) { puts ( "Cannot open output file" ) ; close ( inhandle ) ; exit(0) ; } do { bytes = read ( inhandle, inp_buffer, 512 ) ; if ( bytes > 0 ) { if(!strcmp(argv[3],"e")) { encode(inp_buffer,bytes); write ( outhandle, out_buffer, (bytes*2) ) ; } else { if(!strcmp(argv[3],"d")) { decode(inp_buffer,bytes); write ( outhandle, out_buffer, (bytes/2) ) ; } else { printf("\ninvalid switch :%s:",argv[3]); close ( inhandle ) ;close ( outhandle ) ; exit(0); } } } else break ; }while(1); close ( inhandle ) ; close ( outhandle ) ; }
USAGE
./application input-file output-file switch
e=encoding
d=decoding
TAKE CARE
use <=8 character filenames without absolute paths for safety
Total Comments 0