以下是加密的代码。首先每隔一个字母设置一次,然后每隔一个字符设置一次(输入:abcdefgh;输出:acegdfh(。我搞不懂怎么解密。我想,把加密后的文本一分为二,然后轮流从中取出一封信就可以了。对代码有什么想法吗?
onEvent("button_encrypt", "click", function(encrypt) {
var text = getText("text_input1");
var encripted = "";
var i = 0;
while ((i < klartext.length)) {
encrypted = encrypted + text.charAt(i);
i = i + 2;
}
i = 1;
while ((i < text.length)) {
encrypted = encrypted + text.charAt(i);
i = i + 2;
}
setText("text_input1", encrypted);
});
我喜欢这种加密风格。我在Code.org应用程序中模拟了这个解决方案,用于测试等等。
加密首先我稍微清理了一下加密代码。我发现了几个稍微偏离的变量名。
onEvent("button_encrypt", "click", function(encrypt) {
var text = getText("text_input1");
var encrypted = "";
var i = 0;
while (i < text.length) {
encrypted = encrypted + text.charAt(i);
i = i + 2;
}
i = 1;
while (i < text.length) {
encrypted = encrypted + text.charAt(i);
i = i + 2;
}
//changed ID, do to design
setText("resultLbl", encrypted);
});
解密我处理解密的方法是将加密的数据一分为二,然后将前面的字母从每一半上撕下,来回交替。
onEvent("decryptBtn", "click", function(){
var text = getText("text_input1");
var decrypt = "";
//break message in half
var half1 = text.substring(0,text.length/2);
var half2 = text.substring(text.length/2);
//Loop through two variables. If one is longer will always be half1.
//Could do this with two index variables, but I did it by shrinking words
while(half1.length > 0){
//add to solution
decrypt = decrypt + half1.substring(0,1);
//cut the letter off the encrypted string once used
half1 = half1.substring(1);
//Repeat for half2
//If protects from edge case of one more letter in half1
if(half2.length > 0){
decrypt = decrypt + half2.substring(0,1);
half2 = half2.substring(1);
}
}
setText("resultLbl", decrypt);
});