Click to See Complete Forum and Search --> : JavaScript Decoding/Comparing


SystemCrash
05-02-2003, 05:58 PM
/**
*/
function base64ToAscii(c)
{
var theChar = 0;

if (0 <= c && c <= 25)
{
theChar = String.fromCharCode(c + 65);
}
else if (26 <= c && c <= 51)
{
theChar = String.fromCharCode(c - 26 + 97);
}
else if (52 <= c && c <= 61)
{
theChar = String.fromCharCode(c - 52 + 48);
}
else if (c == 62)
{
theChar = '+';
}
else if( c == 63 )
{
theChar = '/';
}
else
{
theChar = String.fromCharCode(0xFF);
}

return theChar;
}


/**
* A helper function for converting a hex digit into the corresponding
* integer value. I really don't know much about JavaScript, so the
* the conversion is a little laborious. There probably is an easier
* way (including a built-in function), but I don't know it.
*/
function hexval(c) {
if (String('0').charCodeAt(0) <= c && c <= String('9').charCodeAt(0))
return c - String('0').charCodeAt(0);
if (String('A').charCodeAt(0) <= c && c <= String('F').charCodeAt(0))
return c - String('A').charCodeAt(0) + 10;
if (String('a').charCodeAt(0) <= c && c <= String('f').charCodeAt(0))
return c - String('a').charCodeAt(0) + 10;
return 0;
}

/**
* Decodes a string that has been previously encoded according to the
* myUrlDecode() function.
*@see myUrlEncode
*/
function myBase64Decode(str, is_binary) {
var result = "";
var i = 0;
var x;
var shiftreg = 0;
var count = -1;


for (i=0; i < str.length; i++) {
c = str.charAt(i);
if ('A' <= c && c <= 'Z')
x = str.charCodeAt(i) - 65;
else if ('a' <= c && c <= 'z')
x = str.charCodeAt(i) - 97 + 26;
else if ('0' <= c && c <= '9')
x = str.charCodeAt(i) - 48 + 52;
else if (c == '+')
x = 62;
else if (c == '/')
x = 63;
else
continue;

count++;

switch (count % 4)
{
case 0:
shiftreg = x;
continue;
case 1:
v = (shiftreg<<2) | (x >> 4);
shiftreg = x & 0x0F;
break;
case 2:
v = (shiftreg<<4) | (x >> 2);
shiftreg = x & 0x03;
break;
case 3:
v = (shiftreg<<6) | (x >> 0);
shiftreg = x & 0x00;
break;
}

if (!is_binary && (v < 32 || v > 126) && (v != 0x0d) && (v != 0x0a))
{
result = result + "<";
result = result + "0123456789ABCDEF".charAt((v/16)&0x0F);
result = result + "0123456789ABCDEF".charAt((v/1)&0x0F);
result = result + ">";
}
else
result = result + String.fromCharCode(v);

}
return result.toString();
}


Hello Everyone!

I'm in need of assistance. The above js is used to decode a base64 string. A user may enter the encoded string in the,

<TEXTAREA name=text1 size=100 style="FONT-SIZE: smaller" cols=60 rows=10></textarea>, and press the button

<INPUT name=button2 onclick="myUrlDecode(this.form.text1.value, this.form.text1, document.links[0]);" type=button value=Decode> to find the decoded value of a base64 string.

What I would like to do is have the encoded value set inside the code as a var (no users involved). I want the script to decode that value automatically (on page load perhaps). Then... I would like the decoded value to be saved as a different var.

I've been trying for days to convert this code to get it to do what I want it to. I haven't had any luck in doing so. Could any of you please help me out? Thank you for your time.

Halo.

SystemCrash
05-02-2003, 09:22 PM
Will that make the code do what I want it to? where do I put the encoded string?

SystemCrash
05-03-2003, 04:30 PM
I'll try to explain my intentions again. Sorry... I should have been more descriptive.

If you save that code as an html page, you'll notice when you load the page that there is a text box and a button. The was the javascript is written, one has to enter an encoded string into the textbox and click the button to get the decoded value of a string. I would like to change this code so that that is not an option. I want to put the encoded string into the script (set it as a var(variable). I want the script to decode the encoded string everytime the page loads. Then I want the code to save the decoded string as a totally different variable.

I have no idea how to do this^. Can anyone help me out?

Again... I'm sorry. Thank you so much for your time. I'm new at this.

SystemCrash
05-03-2003, 04:37 PM
Thanks for cleaning up the code for the hex-dec btw. That was very nice of you.

SystemCrash
05-03-2003, 04:53 PM
Originally posted by Dave Clark
Patience, bud. :D I understood your intentions/request from the start. I first wanted to address this part of what you said:

Dave


Sorry If I sounded a bit hasty. I really appreciate what you did. "You da man!" :D

**Slaps himself***

SystemCrash
05-03-2003, 08:03 PM
<HTML>
<HEAD>
<TITLE>BASE64/RADIX64 Coder</TITLE>
<META content="text/html; charset=windows-1252" http-equiv=Content-Type>
<SCRIPT><!--

/**
*/
function base64ToAscii(c)
{
var theChar = 0;

if (0 <= c && c <= 25)
{
theChar = String.fromCharCode(c + 65);
}
else if (26 <= c && c <= 51)
{
theChar = String.fromCharCode(c - 26 + 97);
}
else if (52 <= c && c <= 61)
{
theChar = String.fromCharCode(c - 52 + 48);
}
else if (c == 62)
{
theChar = '+';
}
else if( c == 63 )
{
theChar = '/';
}
else
{
theChar = String.fromCharCode(0xFF);
}

return theChar;
}


/**
* A helper function for converting a hex digit into the corresponding
* integer value. I really don't know much about JavaScript, so the
* the conversion is a little laborious. There probably is an easier
* way (including a built-in function), but I don't know it.
*/
function hexval(c) {
if (String('0').charCodeAt(0) <= c && c <= String('9').charCodeAt(0))
return c - String('0').charCodeAt(0);
if (String('A').charCodeAt(0) <= c && c <= String('F').charCodeAt(0))
return c - String('A').charCodeAt(0) + 10;
if (String('a').charCodeAt(0) <= c && c <= String('f').charCodeAt(0))
return c - String('a').charCodeAt(0) + 10;
return 0;
}

/**
* Decodes a string that has been previously encoded according to the
* myUrlDecode() function.
*@see myUrlEncode
*/
function myBase64Decode(str, is_binary) {
var result = "";
var i = 0;
var x;
var shiftreg = 0;
var count = -1;


for (i=0; i < str.length; i++) {
c = str.charAt(i);
if ('A' <= c && c <= 'Z')
x = str.charCodeAt(i) - 65;
else if ('a' <= c && c <= 'z')
x = str.charCodeAt(i) - 97 + 26;
else if ('0' <= c && c <= '9')
x = str.charCodeAt(i) - 48 + 52;
else if (c == '+')
x = 62;
else if (c == '/')
x = 63;
else
continue;

count++;

switch (count % 4)
{
case 0:
shiftreg = x;
continue;
case 1:
v = (shiftreg<<2) | (x >> 4);
shiftreg = x & 0x0F;
break;
case 2:
v = (shiftreg<<4) | (x >> 2);
shiftreg = x & 0x03;
break;
case 3:
v = (shiftreg<<6) | (x >> 0);
shiftreg = x & 0x00;
break;
}

if (!is_binary && (v < 32 || v > 126) && (v != 0x0d) && (v != 0x0a))
{
result = result + "<";
result = result + "0123456789ABCDEF".charAt((v/16)&0x0F);
result = result + "0123456789ABCDEF".charAt((v/1)&0x0F);
result = result + ">";
}
else
result = result + String.fromCharCode(v);

}
return result.toString();
}

/**
*
*/
function myNbtDecode(str) {
var result = "";
var i = 0;
var x;
var shiftreg = 0;
var count = -1;


for (i=0; i < str.length; i++) {
c = str.charAt(i);
if ('A' <= c && c <= 'Z')
x = str.charCodeAt(i) - 65;
else if ('a' <= c && c <= 'z')
x = str.charCodeAt(i) - 97;
else
continue;

count++;

switch (count % 2)
{
case 0:
shiftreg = x;
continue;
case 1:
v = (shiftreg<<2) | (x);
shiftreg = 0;
break;
}

result = result + String.fromCharCode(v);

}
return result.toString();
}

function myUrlDecode(str, fld) {
fld.value = myBase64Decode(str, false);
}


var PacketTags = new Array(
"Reserved - a packet tag must not have this value",
"Public-Key Encrypted Session Key Packet",
"Signature Packet",
"Symmetric-Key Encrypted Session Key Packet",
"One-Pass Signature Packet",
"Secret Key Packet",
"Public Key Packet",
"Secret Subkey Packet",
"Compressed Data Packet",
"Symmetrically Encrypted Data Packet",
"Marker Packet",
"Literal Data Packet",
"Trust Packet",
"User ID Packet",
"Public Subkey Packet",
"Private or Experimental Values"
);

var HashAlgorithm = new Array(
"UNKNOWN",
"MD5",
"SHA1",
"RIPEMD160",
"double-width SHA",
"MD2",
"TIGER192",
"HAVAL-5-160"
);

var CompressionAlgorithm = new Array(
"Uncompressed",
"ZIP (RFC 1951)",
"ZLIB (RFC 1950)"
);

function hexcode(x) {
var result = "";
result = result + "0123456789ABCDEF".charAt((x/16)&0x0F);
result = result + "0123456789ABCDEF".charAt((x/1)&0x0F);
return result;
}

</SCRIPT>
</HEAD>
<BODY>
<h1>BASE64/RADIX64 Coder</h1>
<FORM name=myForm>
<TEXTAREA name=text1 size=100 style="FONT-SIZE: smaller" cols=60 rows=10></textarea>


<P><INPUT name=button2 onclick="myUrlDecode(this.form.text1.value, this.form.text1, document.links[0]);" type=button value=Decode>
</BODY></HTML>


This script should work. And as far as an encoded string... "ZGF2ZQ==". The decoded value of that is "dave." I think that is what you were asking for. Thank you again. I owe you one.

SystemCrash
05-04-2003, 07:11 PM
I really appreciate it. That was a lot easier than I thought it would be. Thank you much.