Solved

simple encryption/decryption - Please!

Posted on 2000-04-28
10
273 Views
Last Modified: 2008-02-20
I have a routine that uses a hidden frame for displaying certain information based on an access code the user enters.  All the relative data is contained within a 2d array.  The problem is that this file is loaded and visible in the cache after the files are accessed.  I would like to know a simple encrypt/decrypt method so that at least the array information is not readily viewable from a text editor.  I know this method could be easily decoded with a little effort, but I want to keep it away from the basic user.
0
Comment
Question by:daron
10 Comments
 
LVL 22

Expert Comment

by:CJ_S
ID: 2758958
you could use the

s = str.charCodeAt(any place of the string); // get the numerical value of that character

and the

s = str.fromcharAt(any place of the string); // get the character from a value

to code and decode....
0
 

Author Comment

by:daron
ID: 2758992
The data is in a 1d array like so:

st[2]=new Array("Percent","60","20","10","0","10","","NA","NA");

I'm not sure of the method of initially encrypting this when I create the .html file, and what function or method to reference to decrypt it when I want to access a certain index...
0
 

Author Comment

by:daron
ID: 2759451
Adjusted points from 140 to 265
0
 
LVL 1

Expert Comment

by:fbog
ID: 2761283
The command-line script encoder (screnc.exe) provides a simple mechanism for encoding HTML, ASP, SCT, VBScript, and JScript files (http://msdn.microsoft.com/library/periodic/period99/scriptengine.htm)
0
 

Author Comment

by:daron
ID: 2761329
I think I may be getting hold of some of this, but is there a fil I download called screnc.exe??  I don't see where I can do this.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 49

Accepted Solution

by:
DanRollins earned 265 total points
ID: 2763292
This should do the trick.  This file provides an encrypting fn and demonstrates the usage (your encrypt/decrypt can be more sophisticated).

Paste your code to be encrypted into the first textarea, and click the [Encrypt] btn, then copy the text from the lower area into your script (see the SetArrayData()fn)

In this sample, I decrypt then evaluate and show an alert to show that it works.  In your actual application, you will have a function like:

function SetArrayData() {
  var sJsCmds="{fw%xyB%sj|%Fwwf~-.@xy`7bBsj|%Fwwf~-'Ujwhjsy'1';5'1'75'1'65'1'5'1'65'1''1'SF'1'SF'.@%";
  var s= Decode( sJsCmds);
  eval( s );
}

....and you will call SetArrayData() then you will have immediate access to the items in the array.


PS.  Put:
<meta http-equiv="pragma" content="no-cache">
in the <head> section to (usually) avoid having the page cached.

-- dan

<html>
<HEAD>
<SCRIPT>
function Encode( s ) {
  var sRet="";
  for(j=0; j< s.length; j++ ) {
    var n= s.charCodeAt(j);
      if ( n>=32 ) {
      sRet += String.fromCharCode( n + 5 );
      }
  }
  return( sRet );
}
function Decode( s ) {
  var sRet="";
  for(j=0; j< s.length; j++ ) {
    var n= s.charCodeAt(j);
    sRet += String.fromCharCode( n - 5 );
  }
  return( sRet );
}

function DoEncode() {
  var s= document.all.toEncode.value;
  document.all.encoded.value= Encode(s);
}
//--------------- example for you
function SetArrayData() {
  var sJsCmds=" PASTE YOUR ENCODED DATA HERE ";
  var s= Decode( sJsCmds);
  eval( s );
}
function ExecEncoded() {
  var sJsCmds= document.all.encoded.value;
  var s= Decode( sJsCmds);
//  alert( s );
  eval( s );
  alert( "after ExecEncoded variable st[2][2]="+ st[2][2] );
}

</SCRIPT>
</HEAD>
<BODY>
<textarea name="toEncode" COLS=60 ROWS=5>
var st= new Array();
st[2]=new Array("Percent","60","20","10","0","10","","NA","NA");
</textarea>
<br>
<input value="encode" type=button onclick="DoEncode()">
<br>
<textarea name="encoded"  COLS=60 ROWS=5>
</textarea>
<input value="execEncoded" type=button onclick="ExecEncoded()">
</BODY>
</HTML>
0
 

Author Comment

by:daron
ID: 2764592
Thanks very much for your help.
THis is just what I've been looking for.  I have had some confusion about getting it to work in my code.  The array is declared with all of the values placed right after this.  Following this, the array is referenced either from other frames, or from more JavaScript below it.  I have doubts whether to encrypt the declaration of the array or just the index assignments.  Also, I can't seem to know for sure where the decrypt and SetArray data should be called.


I hope I can make this clear.  Here is the javascript code that I have tried with the encrypt/decrypt:


<HTML><HEAD><noscript><meta HTTP-EQUIV='refresh' CONTENT='0;URL=index.html'></noscript>
<SCRIPT LANGUAGE='JavaScript'>
<!-- HIDE FROM OTHER BROWSERS
if((frames.self==frames.parent)){location='index.html';}
var current=1;
var toOut = "";
var ii=0;
var numColumns = 9;

var NumberOfArrayIndexes=81;
var GetDetails=0;
var st = new Array(NumberOfVals);


function Encode( s ) {
  var sRet="";
  for(j=0; j< s.length; j++ ) {
    var n= s.charCodeAt(j);
if ( n>=32 ) {
      sRet += String.fromCharCode( n + 5 );
}
  }
  return( sRet );
}
function Decode( s ) {
  var sRet="";
  for(j=0; j< s.length; j++ ) {
    var n= s.charCodeAt(j);
    sRet += String.fromCharCode( n - 5 );
  }
  return( sRet );
}

function DoEncode() {
  var s= document.all.toEncode.value;
  document.all.encoded.value= Encode(s);
}

function SetArrayData() {
     //I am setting the value of sJsCmds to the encrypted code I got from encrypting all of the array assignments (like st[1]="1","2",...etc).  
  var sJsCmds="xy`6bBsj|%Fwwf~-"

//after the encrypted code the decode is called.
  var s= Decode( sJsCmds);
  eval( s );
}


//I think this is just for the example you gave me
function ExecEncoded() {
  var sJsCmds= document.all.encoded.value;
  var s= Decode( sJsCmds);
  eval( s );
  alert( "after ExecEncoded variable st[2][2]="+ st[2][2] );
}

At this point, I will call the st[index] values from either Javascript here or from another frame.  

At this point, the values of st[] should be accessable to references to it.

When I try to access the array data, It doesn't give me the values....at least the way I have it set up know.

I hope you can clear this up for me. If I need to explain more, let me know. Thanks again for your help.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 2764661
Thanks for acceptin ght answer.  You'll need to be clearer with the additional question.  For instance, show me a complete set of pages, including the frameset, with a parred-down example that should work but does not.  Explain how the program fails and in what cases.

A couple of suggestions:
1) It looks like your  code:
   
  var sJsCmds="xy`6bBsj|%Fwwf~-"
is way too short for defining an 81-element array.  I would expect several pages of encrypted text or a comment like:

var sJsCmds="xy`6bBsj|%Fwwf~-"
 // data removed for brevity

2) To access variable in a different frame, you need to fully-qualify each access:

if (top.frame1.st[0]==60) {
  alert("its 60!");
}

3) Insert an:
 alert(s)
right before the
  eval(s)
to eyeball the decrypted data.  It needs to be a syntactically correct piece of JavaScript code.


0
 

Author Comment

by:daron
ID: 2764711
Thanks for the response.
Here is a more complete copy of the implementation.  You are right, I did cut the encrypted data short.
Below is a more complete layout.  I already have references from other frames that do work without the
encrypted array, but I haven't been successful with any references with the encryption.

The array below is not encrypted, the part I encrypted (with the html encryptor you provided) started just after
the 'for' loop - starting with st[1].  I continued the encrypted part until just before the function called displayOutput().  

You can see in the code below the array assignments where the st[] is referenced.  This piece of code does show
where I placed the encryption functions, though I'm not positive of their placement.


<HTML><HEAD><noscript><meta HTTP-EQUIV='refresh' CONTENT='0;URL=index.html'></noscript>
<SCRIPT LANGUAGE='JavaScript'>
<!-- HIDE FROM OTHER BROWSERS
if((frames.self==frames.parent)){location='index.html';}
var currentStudent=1;
var toOutput = "";
var Total=0;
self.name="grade";
var ii=0;
var aStudent;
var CrystalGrade=0;
Item = new Array(13);
var NumberOfGrades=18;
var NumberofActualGrades=13;
var numColumns = 9          ;
for (Count =1; Count <= NumberofActualGrades; Count++){Item[Count]=0;}
var NumberOfStudents=81     ;
var GetDetails=0;
var st = new Array(NumberOfStudents);

function Encode( s ) {
  var sRet="";
  for(j=0; j< s.length; j++ ) {
    var n= s.charCodeAt(j);
if ( n>=32 ) {
      sRet += String.fromCharCode( n + 5 );
}
  }
  return( sRet );
}
function Decode( s ) {
  var sRet="";
  for(j=0; j< s.length; j++ ) {
    var n= s.charCodeAt(j);
    sRet += String.fromCharCode( n - 5 );
  }
  return( sRet );
}

function DoEncode() {
  var s= document.all.toEncode.value;
  document.all.encoded.value= Encode(s);
}


for (Count =0; Count < NumberOfStudents; Count++){st[Count]=new Array(NumberOfGrades);}
function SetArrayData() {

  var sJsCmds="

//I PLACED THE ENCRYPTED DATA STARTING HERE (ACTUAL ENCRYPTION WAS TOO LONG FOR ANY PURPOSE)
st[1]=new Array(
"ID#",
"2Test",
"Homework",
"Quiz",
"Special Projects",
"Current Topic",
"Absences",
"Grade",
"Letter Grade");
 st[2]=new Array(
"Percent",
"60",
"20",
"10",
"0",
"10",
"",
"NA",
"NA");
 st[3]=new Array(
"Average",
"75",
"79",
"73",
"NA",
"80",
"NA",
"78",
"NA");
 st[4]=new Array(
"#Grades",
"4",
"7",
"10",
"x",
"1",
"x",
"x",
"x");
//I STOPPED ENCRYPTING HERE (END OF ENCRYPTION)

//THE DECODE OF THE STRING I PLACED HERE
 var s= Decode( sJsCmds);
  eval( s );
}


//I THINK ONLY PART OF EXAMPLE
function ExecEncoded() {
  var sJsCmds= document.all.encoded.value;
  var s= Decode( sJsCmds);
  alert( s );
  eval( s );
  alert( "after ExecEncoded variable st[2][2]="+ st[2][2] );
}

//CALLING SETARRAYDATA FUNCTION

SetArrayData();


//CONTINUE AS NORMAL HERE
//SHOULD BE ABLE TO ACCESS AS NORMAL
 //*******************************************************************
function displayOutput(toOutput) {
headout = "<HTML><BODY BGCOLOR='#F2F2E1' text='#000080'><FONT face='Verdana' size='2' color='#000080'><br><br>";
tailout = "</BODY></HTML>";
parent.output.document.open();
parent.output.document.write(headout + toOutput + tailout);
parent.output.document.close();}
function checkAccessCode(AccessCode){
accessCode = AccessCode;
nst= NumberOfStudents + 1;
Update = "";
message="<hr><center><FONT face='Verdana' size='2' color='#000080'><h2>Sorry!</h2><p><b>";
message+="Contact your Instructor about getting an Access Code.<p>"
message+="If you believe you have the correct Access Code, Try again.<p>"
message+="<br></b><hr>"
if (accessCode == "" || accessCode == null) {
alert("Please enter your access code.");
return;}
chaccessCode=st[currentStudent][0];
while(accessCode != chaccessCode && currentStudent < nst){
currentStudent++;
if(currentStudent < nst){chaccessCode=st[currentStudent][0];}
else{
displayOutput(message);
top.frames[2].document.forms[0].AccessCode.value="";
break;}
}
if(currentStudent == nst){
displayOutput(message);
top.frames[2].document.forms[0].AccessCode.value="";
currentStudent = 1;}
else{
if(currentStudent < nst){
var ItemTitle = new Array();
var ItemDetails = new Array();
var ItemGrades = new Array();
var pOut = new Array();
aStudent=currentStudent;
direct10="<center><br><br><table border=1><TR>";
for (Count =1; Count < numColumns; Count++)
{ItemTitle[Count-1] = st[1][Count];
ItemDetails[Count-1] = st[currentStudent][Count];
ItemGrades[Count-1] = st[4][Count];
Total += ItemGrades[Count-1];
direct10+='<tr><TD valign=top><B><FONT SIZE=2 COLOR=#000080>'+ItemTitle[Count-1]+' </FONT></B></td><td><FONT SIZE=2 COLOR=#FF0000><b> ' + ItemDetails[Count-1] + '</b></font></td></tr>';}
Update = st[0][0];
direct10 += "<tr><TD colspan=2 valign=top><center><a href='mailto:name@somewhere.edu?subject=Grade'><FONT face='Verdana' size='1'>Click Here to send e-mail</a></center>";
direct10 += "</TD></TR></TABLE></center>";
toOutput = direct10;
displayOutput(toOutput);
top.frames[2].document.forms[0].AccessCode.value="";
currentStudent = 1;
}}};
// STOP HIDING FROM OTHER BROWSERS -->
</script>

</head>
<body>& Chr(10)<p></p></body></html>
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 2764857
Alas, it is impossible for me to debug your code, since I do not have the complete frameset and the piece you sent cannot be run separately.

Furthmore, you again failed to tell me how and where the program fails.

Please, try placing (for example):

  alert( st[1][2] );

right before the place where in the script where you need to find the value of st[1][2].  If it is undefined, or null, or something, then we need to look at my code.  If it shows the original value (before encryption) then my code works.

If you are calling this from a separate frame, then be sure that the function:
  SetArrayData()
has been called before you try to access the data.  For instance, the other fame may be calling your checkAccessCode() or displayOutput() before this fram has been loaded.  

TRY THIS: Add SetArrayData() as the first command of any of these externall-callable fns.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now