我想使用javascript进行自定义加密,但是我陷入了解密功能

时间:2019-02-27 22:21:07

标签: javascript html html5

我想用javascript创建新的自定义加密引擎,但是当我进行解密功能时遇到问题。在我的解密功能中,我不了解如何将3个字符切换为1个字符。在解密功能部分,我不希望的情况下的3个字符更改为返回的字符。

如果您需要我的完整代码,我可以在这里分享。

所以请帮助我解决这个问题。对不起,我的英语不好:)

<body>
	<h3>Encrypt and Decrypt</h3>
	<!-- Encrypt -->
<!-- 	<input placeholder="Ketikan sesuatu disini, pasti bisa:v" id="input"><br>
	<button onclick="encrypt()">Encrypt</button> -->
	<!-- Decrypt -->
	<br><input placeholder="Ketikan sesuatu disini, pasti bisa:v" id="input2"><br>
	<button onclick="decrypt()">Decrypt</button>
	<!-- Result -->
	<div id="result"></div>
	<!-- Enginenya -->
	<script>
		function encrypt(){
			var rawtext = document.getElementById("input").value;
			var temptext = "";
			for(i = 0; i < rawtext.length; i++){
				temptext += enc(rawtext[i]);
			}
			document.getElementById("result").innerHTML = temptext;
		}
		function decrypt(){
			var rawtext = document.getElementById("input2").value;
			var temptext = "";
			for(i = 0; i < rawtext.length; i++){
				temptext += dec(rawtext[i]);
			}
			document.getElementById("result").innerHTML = temptext;
		}
		function enc(x){
			switch(x){
				case " " :
				return " ";
				break;

				case "A" :
				return "+/=";
				break;

				case "B" :
				return "36=";
				break;
			}
		}
		function dec(x){
			switch(x){
				case "+/=" :
				return "A";
				break;

				case "36=" :
				return "B";
				break;
			}
		}
	</script>
</body>

3 个答案:

答案 0 :(得分:1)

您正在遍历单数字符并将它们传递给dec(),例如。如果输入“ + / =“,则实际上是在呼叫dec('+')然后呼叫dec('/')然后呼叫dec('=')

解密输入的值时,必须将它们分成3组,然后再传递。

function decrypt(){
    var rawtext = document.getElementById("input2").value;
    var temptext = "";

    for(i = 0, charsLength = rawtext.length; i < charsLength; i += 3){
        temptext += dec(rawtext.substring(i, i + 3));
    }

    document.getElementById("result").innerHTML = temptext;
}

答案 1 :(得分:0)

您可以使用三个角色来解密加密的字符串。

while (i < rawtext.length) {
    temptext += dec(rawtext.slice(i, i += 3)); // take from index i and increment i by 3
}

function encrypt() {
    var rawtext = document.getElementById("input").value,
        temptext = "",
        i;
    for (i = 0; i < rawtext.length; i++) {
        temptext += enc(rawtext[i]);
    }
    document.getElementById("result").innerHTML = temptext;
}

function decrypt() {
    var rawtext = document.getElementById("input2").value,
        temptext = "",
        i = 0;
    while (i < rawtext.length) {
        temptext += dec(rawtext.slice(i, i += 3));
    }
    document.getElementById("result").innerHTML = temptext;
}

function enc(x) {
    switch (x) {
        case " ":
            return " ";
        case "A":
            return "+/=";
        case "B":
            return "36=";
    }
}

function dec(x) {
    switch (x) {
        case "+/=":
            return "A";
        case "36=":
            return "B";
    }
}
<h3>Encrypt and Decrypt</h3>
<input placeholder="Ketikan sesuatu disini, pasti bisa:v" id="input"><br>
<button onclick="encrypt()">Encrypt</button>
<!-- Decrypt -->
<br><input placeholder="Ketikan sesuatu disini, pasti bisa:v" id="input2"><br>
<button onclick="decrypt()">Decrypt</button>
<!-- Result -->
<div id="result"></div>

答案 2 :(得分:0)

好像您要遍历文本以逐个字符解密,但是 using UnityEngine; using UnityEngine.UI; using System.Collections; public class PlayerHealth : MonoBehaviour { [SerializeField] GameObject deathFX; [SerializeField] Transform parent; public Image Bar; public Text Text; public float max_health = 100f; public float cur_health = 0f; //Use this for initialization void Start() { // Initialize the health that is given cur_health = max_health; InvokeRepeating("decreaseHealth", 0f, 2f); } void Update() { if (cur_health <= 0) { Destroy(gameObject); //if the player has no health point left, destroy the player. } } void OnCollisionEnter(Collision collision) { if (collision.gameObject.GetComponent<Projectiles>()) { max_health -= cur_health; //if the collision object has a homing script, minus player health by damageToPlayer } } void decreaseHealth() { //Subtract the health at the following rate //Check if the health is 0 before we do any damage if (cur_health < 0) { cur_health = 0; } //make a new variable and divide the current health my the maximum health //this is because the fill value goes from 0 to 1 float calc_health = cur_health / max_health; // 70 / 100 = 0.7 SetHealth(calc_health); //Change the color of the health bar if (cur_health != 0 && cur_health <= max_health / 1.6 && cur_health > max_health / 2.9) // on the scale of 1000, if health <= 625 and is greater than 345, do the following { Bar.color = new Color32(171, 162, 53, 255); } else if (cur_health != 0 && cur_health <= max_health / 2.9) // on the scale of 1000, if health <= 625, do the following { Bar.color = new Color32(158, 25, 25, 255); } } void SetHealth(float myHealth) { //defill the bar based on the current health Bar.fillAmount = myHealth; //change the text to display the amount of health Text.text = cur_health.ToString("f0") + "/100"; } } 函数需要三个字符。这永远不会发生,因此dec返回未定义。

示例:

dec()

您应该更改解密功能以避免这种情况。此外,一些指针:

  • 您没有在加密/解密功能中初始化decrypt("36=") -> dec("3") + dec("6") + dec("=") -> undefined + undefined + undefined undefinedundefinedundefined
  • 您的case语句中的i之后不需要break;,因为return会结束执行。

编辑:这是map的示例,因为其他一些答案中有一些for循环。而且因为我怀疑可以用一行完成(我是对的!)

return