凯撒密码作品 [英] Caesar Cipher work

查看:62
本文介绍了凯撒密码作品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对不起我的英语,我很抱歉,我是法语(是的,你可以怪我ahah:P),这是我第一次在这个论坛上,我进入了stackoverflow,因为很多人都说我如果需要帮助,请转到stackoverflow".因此,我需要在Javascript上寻求帮助,在学校里,老师问我和其他人,创建"Le chiffre deCésar",我认为这是用英语"Caesar_cipher"的意思,然后:

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:

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()
{
}

我在这里完全被挡住了,我们没有上Java课,他们只是说下周工作"

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

我认为Caesar_cipher具有加密功能,此链接对此进行了解释: http://en.wikipedia.org/wiki/Caesar_cipher

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

如果您对var名称有疑问,我将尝试给出答案.

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:为什么我在帖子的开头不能说你好"?哦

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

推荐答案

您必须为每个字符添加一个数字,但随后必须注意不要超过26个字母.我认为您不会碰任何符号.这是加密代码,尽管我看不懂法语,所以我真的不知道您的界面如何工作.

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.

注意:小写字母"a"等于97,"z"-> 122,"A"-> 65,"Z"-> 90.

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;
}

这篇关于凯撒密码作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆