Click here to Skip to main content
15,887,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I originally created this whic took me a couple of days to figure out

JavaScript
 var all = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.#-?").split('');
        var position;
        var oKey = "P";

function encrypt() // Encrypt Fixed
        {
            var sEncode = ("HI-MOM").split('');

            for (var i = 0; i < sEncode.length; i++) {
                if (all.indexOf(oKey) < all.indexOf(sEncode[i])) {                    
                    position = all.indexOf(sEncode[i]) - all.indexOf(oKey);
                    //output.value += "oKey: " + oKey + " distance to sEncode[" + i + "]: " + sEncode[i] + " Count: " + position + " Final Char: " + all[position-1] + "\n";
                    oKey = sEncode[i];
                }
                else {                    
                    position = all.length - all.indexOf(oKey) + all.indexOf(sEncode[i]);
                    //output.value += "oKey: " + oKey + " distance to sEncode[" + i + "]: " + sEncode[i] + " Count: " + position + " Final Char: " + all[position-1] + "\n";
                    oKey = sEncode[i];
                }

                
            }
        }


I originally used break statements to see the background activity. Thats whats commented out in the encode()

Now I need to reverse it which is driving me up the wall. I keep thinking about the logic and it doesnt seem to quite add up, because the oKey="P" was the start of my offset and changed from there, but not now I feel like the "P" is going to throw me off.

JavaScript
function decrypt() // Offset fixed
      {
          var sEncode = ("6A4NB#").split('');

          for (var i = 0; i < sEncode.length; i++) {
              if (all.indexOf(oKey) < all.indexOf(sEncode[i])) {
                  position = all.indexOf(sEncode[i]) - all.indexOf(oKey);
                  output.value += "oKey: " + oKey + " distance to sEncode[" + i + "]: " + sEncode[i] + " Count: " + position + " Final Char: " + all[position - 1] + "\n";
                  oKey = sEncode[i];
              }
              else {
                  position = all.length - all.indexOf(oKey) + all.indexOf(sEncode[i]);
                  output.value += "oKey: " + oKey + " distance to sEncode[" + i + "]: " + sEncode[i] + " Count: " + position + " Final Char: " + all[position - 1] + "\n";
                  oKey = sEncode[i];
              }


          }
      }


What I have tried:

I've tried every which way swapping variable positions to even swapping the +/- operators and it just encodes it even more =(
Posted
Updated 13-Jul-16 16:54pm
v3
Comments
Patrice T 13-Jul-16 21:36pm    
your code use something that is not listed.
How are we supposed to guess what it does ?
Herboren 13-Jul-16 21:49pm    
I updated it, sorry forgot that was left out =\
Herboren 13-Jul-16 22:54pm    
I havent tried it yet, but I believe if I reverse the string and find the distance backwards I may be able to find my string

1 solution

First of all I see a few problems in encrypt.
JavaScript
var oKey = "P";

The initial value of oKeyis set from outside of the encrypt function. How do know that the value is always "P" when you encrypt.

After encoding each char in position, you do nothing with it.

The function returns nothing.

How do you know the value of oKey when you call the decrypt function.
 
Share this answer
 
Comments
Herboren 13-Jul-16 23:49pm    
The oKey's initial value is always 'P', however it will change as soon as the first characters offset has been found. Once the distance or position between 'P' and 'H' is found it moves onto the next char in the array being 'H' and find the distance between 'H' and 'I', then 'I' and '-' and so forth. oKey always takes the previous element in the array to find the distance of the element ahead of it.

Regardless oKey's initial value will always be 'P' at the beginning of the encrypt function() but will change through this process
Patrice T 14-Jul-16 0:34am    
What append if you need to call encrypt 2 times ?
Herboren 14-Jul-16 0:37am    
I will have to move oKey inside the ecrypt function, I dont want it to go any deeper than the initial encrypt

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900