如何修正解密公式以使整数[需要帮助]

时间:2019-01-30 20:27:50

标签: javascript encryption numbers

我的意思是我的代码在一定程度上可以正常工作。 直到达到一定的加密级别,公式才是正确的[加密密钥的更多组合]

<html>
<script>// Simple array of chars where their index is the 'code'



let lexicon = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", ">", "<", "-", "_", "+", "=", "{", "\\", "[", "]", "|", ",", ".", "?", "/", "'", '"', "", ";", " "];


//decryptor proof
//var y = 26; var a = 1; var b = 1; var c = 4; var d = 6;
//var x = ((a*y)+(c*a)-(d*a))/(1+(b*a)+(2*a)); console.log(x);
//for 12 bit code, hi = 


// Function to take a string and encode it using a simple map.
const encodeData = (data, lex) => data.split('').map(c => lex.indexOf(c) + 0);
// Decode does the reverse
const decodeData = (data, lex) => data.map(d => lex[d]);
// For making conversion constant "hi" correct
const roundHi = (y, x) => Math.round(y/x);



var pointValue = [];
var toVector = 0;
var toNoVector = 0;
var vector = [];
var noVector = [];
var encryption = [];
var decryption = [];
var cvalue = 0;
var dvalue = 0;
var evalue = 0;
var hi = 0;
var a = 0;
var b = 0;
var c = 0;
var d = 0;
var x = 0;
var n = 0;
var aa = 0;
var bb = 0;
var cc = 0;
var dd = 0;
var bool1 = 0;
var bool2 = 0;





















var code = prompt("Type In Encryption Password [Beta Encryptor]");
//testing while loop!
while (bool1 + bool2 < 2) {
    if (code.length === 0) {
        code = prompt("Type in Password more than 0 Characters Long");
    }
    else {
        bool1 = 1;
    }

    
    
    if (code.length % 4 != 0) {
         code = prompt("Type in Password Length That is Perfectly divisible by 4");
    }
    else {
        bool2 = 1;
    }
    
    
    
}
//tested while loop!
code = encodeData(code, lexicon);
for (var j = 0; j < code.length; j+=4) {
    a += code[j];
}

for (var j = 1; j < code.length; j+=4) {
    b += code[j];
}

for (var j = 2; j < code.length; j+=4) {
    c += code[j];
}

for (var j = 3; j < code.length; j+=4) {
    d += code[j];
}

n = code.length / 4;
aa = a / n;
bb = b / n;
cc = c / n;
dd = d / n;


 
 
//document.write('<div id="key" style="background-color: lightblue; display: table;">' + code + '</div>');
//document.write(code);
document.write('<div id="key" style="background-color: lightblue; display: table;">' + code);


















    function a () {
        var a = vector.join('.');
        var aa = vector.join('');
        var b = a.split('.')
        
        
        
        
        function b () {
            for (var t = 0; t < aa.length; t++) {
                if (aa[t] === ".") {
                    pointValue.push(".");
                }
                
                if (aa[t] === ",") {
                    pointValue.push(",");
                }
            
            }
        }
        b();
        
        
        
        
        
        
    }

















function runner() {
    var abc = document.getElementById('input').value;
    encryption = encodeData(abc, lexicon);
    if (encryption.length % 2 === 1) {
        encryption.push(93);
        alert("The Key was Edited Because The Key needs to be even");
    }   
    var a = code.length * 0.5;
    var b = 1;
    var c = 0;

    //makes vector

    function vectorMaker (p) {
        p = 0; 
        for (var k = 0; k < encryption.length; k++) {
            toVector = 0;
            for (var j = 0; j < code.length; j++) {
            
                if (j % 4 === 0) {
                    toVector += encryption[p] / code[j];
                }
        
                else if (j % 4 === 1) {
                    toVector += encryption[p] * code[j];
                }
        
                else if (j % 4 === 2) {
                    toVector += encryption[p] - code[j];
                }
        
                else if (j % 4 === 3) {
                    toVector += encryption[p] + code[j];
                }
            }
            vector.push(toVector);
            p+=1; 
        }
        p = 0;
    }
    vectorMaker();
    
        
    
    document.getElementById('runEncryption').innerHTML = vector;
       
}







    
function runback () {
    
    
    





    function vectorBreaker (e) {
        e = 0; 
        decryption = vector;
         
        
        for (var m = 0; m < vector.length; m++) {
            y = vector[m]
            
            
            decryption[m] = ((aa*y)+n*(cc+aa)-n*(dd+aa))/((n*1)+n*(bb*aa)+((n*2)*aa));
            decryption[m] = Math.round(decryption[m]);
            
            e+=1;
        }
        e = 0;
    }
    vectorBreaker();
    
    
    decryption = decodeData(decryption, lexicon);
    cvalue = decryption.join('');
    document.getElementById('runDecryption').innerHTML = cvalue; 

   
}


//decryptor proof
//var y = 26; var a = 1; var b = 1; var c = 4; var d = 6;
//var x = ((a*y)+(c*a)-(d*a))/(1+(b*a)+(2*a)); console.log(x);









function clearCode () {
    document.getElementById('runDecryption').innerHTML = "";
    document.getElementById('runEncryption').innerHTML = "";
    pointValue = [];
    toVector = 0;
    toNoVector = 0;
    vector = [];
    noVector = [];
    encryption = [];
    decryption = [];
    cvalue = 0;
    dvalue = 0;
    evalue = 0;
    hi = 0;
    a = 0;
    b = 0;
    c = 0;
    d = 0;
    x = 0;
    n = 0;
    aa = 0;
    bb = 0;
    cc = 0;
    dd = 0;
    bool1 = 0;
    bool2 = 0;
}





// cod for tomorrow | var a = vector.join('.'); var b = a.split('.');

</script>








<input type="text" id="input">
<button onclick="runner();">Add Text to Encrypt</button></div>
<br>
<div id="runEncryption" style="background-color: yellow; padding: 30px; display: table; margin: 15px;"></div>

<button onclick="runback();">Decode Text</button>
<br>
<div id="runDecryption" style="background-color: lightgreen; padding: 30px; display: table; margin: 15px;"></div>

<button onclick="clearCode();">Clear Text Boxes</button>
</html>

我的问题是在函数runBack()中称为vectorBreaker()的函数中 如您所见,我在解密公式下有一个取整器。这是因为: 例如,“解密的数据[至少放置数字]”将为47,解密公式将等于47.01244423 我以为乘以错误是一个鸿沟,所以我尝试加进来使解密公式的常数。 即使为较小的数字常量提供整数,它仍然没有给出整数。如下图所示 enter image description here

任何人都可以帮助解决此问题[使解密公式总是吐出整数]吗? 在此先感谢:)

0 个答案:

没有答案