[英]How do you decipher the rail fence cipher with a simple method for beginners?
這是加密的代碼。 首先設置每隔一個字母,然后每隔一個字母(輸入: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);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.