凯撒密码算法 Javascript实现

信息安全概论
凯撒密码算法 ----Javascript实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>古典算法</title>
    <script src="./index.js"></script>
</head>
<body>
    <div class="all">
        <div class="one">
            <ul>
                <li class="first">
                    凯撒密码
                </li>
                <li> <span>输入密匙:</span> <input type="text" name="" id="secretKeyId"></li>
                <li> <span>输入明文:</span> <input type="text" name="" id="expressId"></li>
                <li> <span>输出密文:</span> <input type="text" name="" id="secretId" disabled></li>
                <li><button onclick="caesar()">加密</button> </li>
            </ul>
        </div>
    </div>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        html,
        body,
        .all {
            height: 100%;
        }

        .all {
            position: relative;

        }

        .one {
            width: 270px;
            height: 150px;
            background-color: rgb(138, 138, 138, 0.5);
            position: absolute;
            left: 50%;
            top: 100px;
            transform: translate(-50%, -50%);
            border-radius: 10px;
        }


        span {
            display: inline-block;
            font-size: 10px;
        }


        .first {
            text-align: center;
            margin: 8px 0px;
        }

        li {
            padding: 0 10px;
        }

        li>button {
            float: right;
            margin-top: 15px;
            margin-right: 25px;
        }
    </style>
</body>

</html>
function caesar() {

    var secretKey, expressWords, secretWords;
    expressWords = document.getElementById("expressId").value;
    secretKey = document.getElementById("secretKeyId").value;
    //这里记得把拿到的密钥值从字符串类型转为Number类型
    secretKeyNum = Number(secretKey);

    var start1 = "A".charCodeAt(0);
    var end1 = "Z".charCodeAt(0);
    var start2 = "a".charCodeAt(0);
    var end2 = "z".charCodeAt(0);

    var strList = expressWords.split("");
    var judge, replace;
    for (var i = 0; i < expressWords.length; i++) {
        judge = strList[i].charCodeAt(0);
        if (judge <= end1 && judge >= start1) {
            replace = start1 + (judge - start1 + secretKeyNum) % 26;
            strList[i] = String.fromCharCode(replace);
        }
        else if (judge <= end2 && judge >= start2) {
            replace = start2 + (judge - start2 + secretKeyNum) % 26;
            strList[i] = String.fromCharCode(replace);
        }
    }
    secretWords = strList.join("");
    document.getElementById('secretId').value = secretWords
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值