Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to encode/encrypt then decode a string

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

Say that I have a script stored in a string like
Code:
static char tcl_script[] =
"# example.tk\n"
"# -------------------------------------------------\n"
"# A simple decimal degree to deg,min,sec converter\n"
"# -------------------------------------------------\n"
"# main window\n"
"#\n"
"wm title . \"DMS convert\"\n"
"\n"
...

I am after two C functions to do basic encrypt/encode and decode later on this string, can someone please help?
 
What is it: basic encrypt/encode and decode?
Apropos: why your mutable strings must be referenced by const pointers? Const means constant, do you forget?..
Warning: if you declare a char array as a const, a compiler can assign read-only memory for it. It seems you have a rather strange manner to deal with const arrays...
 
OK the idea is to turn a Tcl script into C code. Here is the code I have so far:

Code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <assert.h>
#include <unistd.h>

char const Ccode [] =    
"#include <stdio.h>\n"
"#include <stdlib.h>\n"
"#include <math.h>\n"
"#include <tcl.h>\n"
"#include <tk.h>\n"
"\n"
"#define TCLGO     TCL_GLOBAL_ONLY\n"
;


char const Cmaincode [] =
" \n"
"main(int argc, char *argv[])\n"
"{\n"
"    Tcl_Interp *interp;\n"
"    Tk_Window mainWindow;\n"
"    Tk_Window tkmain;\n"
"    int error;\n"
"    char *trace;\n"
" \n"
"/*  first we create a Tcl interpreter */\n"
"\n"
"   interp = Tcl_CreateInterp();\n"
" \n"
"/*  now we create a Tk main window */\n"
"     tkmain = Tk_MainWindow(interp);          \n"
"\n"
" \n"
"/*  now we initialize our interpreter for Tcl,Tk,and our personal\n"
"    example C commands */\n"
"\n"
"    if(Tcl_Init(interp) != TCL_OK) {\n"
"        fprintf(stderr, \"Tcl_Init failed: %s\\n\", interp->result);\n"
"    }\n"
"    if(Tk_Init(interp) != TCL_OK) {\n"
"        fprintf(stderr, \"Tk_Init failed: %s\\n\", interp->result);\n"
"    }\n"
"\n"
"/*  now we tell the interpreter which Tcl/Tk file to use */\n"
"\n"
"    error = Tcl_Eval(interp,tcl_script);\n"
"    if(error != TCL_OK) {\n"
"        fprintf(stderr, \"%s: %s\\n\", \"tcl_script\", interp->result);\n"
"        trace = Tcl_GetVar(interp, \"errorInfo\", TCL_GLOBAL_ONLY);\n"
"        if(trace != NULL) {\n"
"            fprintf(stderr, \"*** TCL TRACE ***\\n\");\n"
"            fprintf(stderr, \"%s\\n\", trace);\n"
"        }\n"
"    }\n"
"\n"
"/* finally we enter the event loop and wait for user input */\n"
"\n"
"    Tk_MainLoop();\n"
"}\n"
"\n"
;



/* process tcl code and store it in const char */
ProcessTCL(const char* filename){

    FILE *in;
    char buf[1024];
    char fix[1024];
    char *ptr1, *ptr2;
    int xor;
    int c;
    
    xor=14;

/* open the file */    
    in = fopen(filename, "r");
    if (!in) {
        perror("fopen"); exit(1);
    }

    printf("/* DO NOT EDIT THIS FILE.. it was generated by tcl2c */\n\n");
    printf("static char tcl_script[] =\n");

    *buf = 0;
    while(fscanf(in, "%[^\n]", buf) != EOF) {
        fgetc(in);

        ptr1 = buf; ptr2 = fix;
        while(*ptr1) {
            if (*ptr1 == '\"') {
               *ptr2++ = '\\';
               *ptr2++ = '\"';
            }
            else if (*ptr1 == '\\') {
                *ptr2++ = '\\';
                *ptr2++ = '\\';
            }
            else {
                *ptr2++ = *ptr1;
            }
            ptr1++;
        }
        *ptr2 = *ptr1; 
        *buf = 0;
    }
    printf(";\n");

}

int main(int argc, char **argv){
char input[200]; /* input tcl filename*/
int shroud = 0;         /* True to encrypt the compiled-in TCL */
/* C code to use tcl file */


/* check cmd line parameters */
 if ( argc < 2 )
    {
        printf("USAGE: %s arg1\n",argv[0]);
        printf("arg1:input tcl file\n");
        exit(1);
    }
    else
    {	
     	printf("%s\n",Ccode);
	printf("\n");
     	/* process the tcl file */
     	ProcessTCL(argv[1]); 
       	
    	printf("%s\n",Cmaincode);
  
 }

}

This program writes out a C file that can be compiled and executed without using Tcl.

The big problem is that the tcl code stored in tcl_script[] can be easily seen when doing a string of the executable.

So I need:
1) a way to encrypt the tcl source code within the C code
2) a way to decrypt it before calling Tcl_Eval

Thanks,
 
It seems as a monkey's work, sorry...
Let's suppose you may encode Tcl script text in C execs...

Now any (evil;) (semi-)profi passes your C launcher to his/her favourite debugger then gets that decoded text after decoding but before it goes to Tcl (buil-in?) interpreter...

Moreover, this C-packed script must have its corresponding decipher key (or get a password interactively - God forbid;). OK, any beginner hacker may extract this key from your code...

Well, take any free crypto soft (starting from DES), encode text and pack it into C literals as hexa chars:
Code:
"\xNN\xNN...."

On bootstrap stage: allocate memory, decode this mangled text then pass the pointer to it to Tcl interpreter...

Why did you seek adventures? However technically it's so simple (and tedious;) task...
 
What you are "proposing" is really monkey's work.. What I want is to automate the process not use different tools. I know that it won't be difficult to break but enough for me. All I want is a simple encrypt.

I am working on a xor route..

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top