Caesar Cipher work

久未见 提交于 2021-02-08 12:05:02

问题


firstly, i'm sorry for my horrible english, i'm french (yeah you can blame me ahah :P), it's my first time on this forum, i'm coming in stackoverflow because lot of people said me "go to stackoverflow if you need help". So, i need help on Javascript, in school, teachers asking to me and others, to create "Le chiffre de César", i think it's means in english "Caesar_cipher", then:

HTML:

<!DOCTYPE html>

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Chiffrement</title> 
    <meta name="author" content="ISN_Robespierre" />
    <link href="chiffrer.css" rel="stylesheet" type="text/css" media="screen"/>
  </head>
  <body>
    <h1>Le chiffre de César.</hi>
        <p>Message à traiter:</p>
        <textarea id="message1"></textarea>
        </br>
        clef:<input id="clef" type="text" value="3">
            <button type="button" id="boutonChiffrer" > Chiffrer </button>
            <button type="button" id="boutonDechiffrer" > Dechiffrer </button>
        <p>Message traité:</p>
            <div id="message2"><div>

    <script type="text/javascript" src="chiffrer.js" >  </script>   
  </body>
</html>

Javascript:

var clef;
var message1;
var message2;

var setupEvents = function() {

    clef = document.getElementById("clef");  
        message1 = document.getElementById("message1");
    message2 = document.getElementById("message2");

    var boutonChiffrer = document.getElementById("boutonChiffre");
    var boutonDechiffrer = document.getElementById("boutonDechiffre");
    boutonChiffrer.addEventListener("click", chiffrer);
    boutonDechiffrer.addEventListener("click", dechiffrer);
}

window.addEventListener("load", setupEvents);

var actualiserMessage2 = function() 
{
    var nombreClef = parseFloat(clef.value);
    var onChiffre = chiffrer(nombreClef);
    message2.value = onChiffre;

}

var decaler = function (texte, clef)
{
 message2.innerHTML = "";
 var taille = texte.lenght();
 for(var i=0, i<taille, i++)
 {
 var code = texte.charCodeAt(i);
 var codeDecale=decaleCode(code, clef);
 message2.innerHTML+=String.fromCharCode(codeDecale);.
 }

}

var chiffrer = function()
{
}

var dechiffrer = function()
{
}

i'm totally block here, we haven't got a lesson on Javascript, they just said "work for next week"

Caesar_cipher have encryption function i think, this link explain it: http://en.wikipedia.org/wiki/Caesar_cipher

If u have questions about var name, i'll try to give a answer.

Thank you for your help, bye and sorry for my english :/

ps: why i can't say "hello" at the first of the post ? Oo


回答1:


You have to add a number to each character, but then you have to be careful that you stay within the 26 letters. I assume you won't touch any symbols. Here is the encryption code, although I can't read french so I don't really know how your interface works.

Note: the lower-case 'a' is equal to 97, 'z' -> 122, 'A' -> 65, 'Z' -> 90.

var encrypt = function(plaintext, shiftAmount) {
    var ciphertext = "";
    for(var i = 0; i < plaintext.length; i++) {
        var plainCharacter = plaintext.charCodeAt(i);
        if(plainCharacter >= 97 && plainCharacter <= 122) {
            ciphertext += String.fromCharCode((plainCharacter - 97 + shiftAmount) % 26 + 97);
        } else if(plainCharacter >= 65 && plainCharacter <= 90) {
            ciphertext += String.fromCharCode((plainCharacter - 65 + shiftAmount) % 26 + 65);
        } else {
            ciphertext += String.fromCharCode(plainCharacter);
        }
    }
    return ciphertext;
}

var decrypt = function(ciphertext, shiftAmount) {
    var plaintext = "";
    for(var i = 0; i < ciphertext.length; i++) {
        var cipherCharacter = ciphertext.charCodeAt(i);
        if(cipherCharacter >= 97 && cipherCharacter <= 122) {
            plaintext += String.fromCharCode((cipherCharacter - 97 - shiftAmount + 26) % 26 + 97);
        } else if(cipherCharacter >= 65 && cipherCharacter <= 90) {
            plaintext += String.fromCharCode((cipherCharacter - 65 - shiftAmount + 26) % 26 + 65);
        } else {
            plaintext += String.fromCharCode(cipherCharacter);
        }
    }
    return plaintext;
}


来源:https://stackoverflow.com/questions/20445039/caesar-cipher-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!