试图扭转我的算法 [英] Trying to reverse my algorithm

查看:75
本文介绍了试图扭转我的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初创建这个时候花了几天才弄明白



  var  all =(  ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890。# - ?)。split(' '); 
var 的位置;
var oKey = P;

function encrypt() // 加密固定
{
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 +距离sEncode [+ i +]:+ sEncode [i] +计数:+位置+最终字符:+所有[位置-1] +\ n;
oKey = sEncode [i ]。
}
else {
position = all.length - all.indexOf(oKey)+ all.indexOf(sEncode [i] );
// output.value + =oKey:+ oKey +距离sEncode [+ i +]:+ sEncode [i] +计数:+位置+最终字符:+所有[位置-1] +\ n;
oKey = sEncode [i ]。
}


}
}





我最初使用break语句来查看后台活动。多数民众赞成在编码中注释掉了()



现在我需要扭转它,这让我起了作用。我一直在思考这个逻辑,它似乎并没有完全相加,因为oKey =P是我偏移的开始并且从那里改变了,但现在我不觉得P会让我失望。



  function  decrypt() //  抵消固定 
{
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 + 到sEncode的距离[ + i + ]: + sEncode [i] + 计数: + position + 最终字符: + all [position - 1 ] + \ n;
oKey = sEncode [i];
}
else {
position = all.length - all.indexOf(oKey)+ all.indexOf(sEncode [i] );
output.value + = oKey: + oKey + 到sEncode的距离[ + i + ]: + sEncode [i] + 计数: + position + 最终字符: + all [position - 1 ] + \ n;
oKey = sEncode [i];
}


}
}





我尝试了什么:



我已尝试过将变量位置换成甚至交换+/-运算符并且只是编码它甚至更多=(

解决方案

首先,我在加密中看到一些问题。

  var  oKey =   P; 



oKey 的初始值是从外部设置的加密功能。加密时怎么知道值总是P。



编码后每个在位置中的char,你什么也不做。



该函数什么都不返回。



当你调用解密函数时,你怎么知道 oKey 的价值?


I originally created this whic took me a couple of days to figure out

 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.

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 =(

解决方案

First of all I see a few problems in encrypt.

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.


这篇关于试图扭转我的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆