Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Protecting my JavaScript Code

 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not JavaScript guru, but I've recently developed a fairly hefty application in JavaScript that my company is planning on distributing. Of course, as this application is written in JavaScript, the source code is readily available to whomever we distribute it to.
We'd like to have some way of securing the code that I've written. I'm considering running it through some sort of obfuscation program to help protect the source code. Anyone have any suggestions on good applications that I might use for such a purpose, or have any other ideas on how I might protect my source?
Thanks,
Corey
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
obfuscation is the only way that you can go about doing this
I can not recommend any programs since I never used any....
Another trick that I have used is to add a js file that links to your site. This js file can be left blank, you can check your sites logs and you can find out sites that are using the file. If you find anyone using it that is not supposed to then you can add code in that js file that can stop the program from running. This can easily be deleted by the person that took the code, but it can work...
Eric
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Eric. I'ev been googling about this morning and I've found a number of obfuscators out there - I guess I'll just have to weed through them and see what I can find.
Thanks,
Corey
 
Yuriy Fuksenko
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally speaking, there is no way to protect your code in JavaScript.
You try my Web Code Expert tool just to test that statement.
There is a trick I did. It affects the initial perfomance, but it is up to you to use it or not.
Let say you have a way to encrypt a string and to decrypt a string with javascript, so you have to functions:
function encrypt(str){
}
function decrypt(str){
}
Now, let say function with a name "mySecretFunction" is the one to be hidden.
So, in a separate file, you use encrypt function, to encrypt that function definition, basically something like
encrypt("function mySecretFunction(){here goes the body}")
the string you get you use to create the following line instead of usual function definition:
var a="that Encrypted String";eval(decrypt(a));
Now you can encrypt that line too - and so on. I did three iteration.
You can make that more sofisticated, if you want.
 
Yuriy Fuksenko
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example of script viewing:
Type the following in IE address bar to see all event handlers on a page:

After that, just copy the name of a function you ineterested in, and type in address bar:
javascript:alert(functionName)
or
javascript:self.open("","").document.writeln(functionName);
and you will see that function source code. Last variant is better if you need to copy/paste that function body.
 
Manish Hatwalne
Ranch Hand
Posts: 2595
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frankly speaking, you can't do it. There is always a way that allows an experienced programmer to view your client-side javascript.
- Manish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic