用CryptoJS更改密钥 [英] Change the Key with CryptoJS

查看:251
本文介绍了用CryptoJS更改密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CryptoJS加密和解密文本。在这里,我只是采取消息并显示加密和解密消息。



我正在使用DES算法进行加密和解密。



这是我的HTML文件

 <!DOCTYPE html> 
< html>
< head>
< script src =tripledes.js>< / script>
< script src =mode-ecb.js>< / script>
< style type =text / css>
.maindiv {
/ *只是将页面的中心放在页面上* /
margin:0 auto;
width:400px;
/ *查看窗体的大纲* /
padding:1em;
border:1px solid #CCC;
border-radius:1em;
}
div + div {
margin-top:1em;
}
label {
/ *确保所有标签的大小相同,并且正确对齐* /
display:inline-block;
width:90px;
text-align:right;
}
.button {
/ *将按钮放置到文本字段的相同位置* /
padding-left:90px; / *与标签元素的大小相同* /
}

按钮{
/ *这个额外的余额与标签之间的空间
大致相同,他们的文本字段* /
margin-left:.5em;
}
input:focus,textarea:focus {
/ *给活动元素一个亮点* /
border-color:#000;
}
输入,textarea {
/ *确保所有文本字段具有相同的字体设置
默认情况下,textareas具有等宽字体* /
字体:1em sans-serif;
/ *为所有文本字段赋予相同的大小* /
width:300px;
-moz-box-sizing:border-box;
box-sizing:border-box;
/ *协调外观&文字字段边框的感觉* /
border:1px solid#999;
}
< / style>
< script type =text / javascript>

function viewvalue()
{
var message = document.getElementById(msg)。
var key = document.getElementById(key)。
var encrypted = encryptByDES(message,key);
document.getElementById(enctext)。textContent = encrypted;
document.getElementById(dectxt)。textContent = decryptByDES(encrypted,key);;


}

函数encryptByDES(消息,键){

var keyHex = CryptoJS.enc.Utf8.parse(key) ;

var encrypted = CryptoJS.DES.encrypt(message,keyHex,{
mode:CryptoJS.mode.ECB,
padding:CryptoJS.pad.Pkcs7
} );
return encrypted.toString();
}

函数decryptByDES(ciphertext,key){
var keyHex = CryptoJS.enc.Utf8.parse(key);

var decryptpted = CryptoJS.DES.decrypt({
ciphertext:CryptoJS.enc.Base64.parse(ciphertext)
},keyHex,{
mode:CryptoJS .mode.ECB,
padding:CryptoJS.pad.Pkcs7
});

return decryptpted.toString(CryptoJS.enc.Utf8);
}
< / script>
< / head>
< body>

< div class =maindiv>
< div>
< label for =name>消息:< / label>
< input type =textid =msgname =msg/>
< / div>
< div>
< label for =mail> Key:< / label>
< input type =textid =keyname =key/>
< / div>
< div>
< label for =msg>加密文本:< / label>
< textarea id =enctextname =enctxt>< / textarea>
< / div>
< div>
< label for =msg>解密文本:< / label>
< textarea id =dectxtname =dectxt>< / textarea>
< / div>
< div class =button>
< button onclick =viewvalue()> View< / button>
< / div>
< / div>
< / body>
< / html>

这是我的.js文件

  / * 
CryptoJS v3.1.2
code.google.com/p/crypto-js
(c)2009-2013 by Jeff Mott。版权所有。
code.google.com/p/crypto-js/wiki/License
* /
/ **
*电子码本块模式。
* /
CryptoJS.mode.ECB =(function(){
var ECB = CryptoJS.lib.BlockCipherMode.extend();

ECB.Encryptor = ECB.extend({
processBlock:function(words,offset){
this._cipher.encryptBlock(words,offset);
}
});

ECB.Decryptor = ECB.extend({
processBlock:function(words,offset){
this._cipher.decryptBlock(words,offset);
}
} );

返回ECB;
}());

请任何人告诉我在哪里以及如何更改密钥。

解决方案

根据 https://code.google.com/archive/p/crypto-js/#Custom_Key_and_IV ,您需要定义并提供初始化向量(IV)和密钥,如果您希望提供一个自定义键:

  var key = CryptoJS.enc.Hex.parse('000102030405060708090a0b0c0d0e0f'); 
var iv = CryptoJS.enc.Hex.parse('101112131415161718191a1b1c1d1e1f');
var encrypted = CryptoJS.AES.encrypt(Message,key,{iv:iv});


I am using CryptoJS to encrypt and decrypt the text. Here, I am just taking the message and showing the both encryption and decryption messages.

I am using DES algorithm for encrypting and decrypting.

This is my HTML file

<!DOCTYPE html>
<html>
<head>
    <script src="tripledes.js"></script>
    <script src="mode-ecb.js"></script>
    <style type="text/css">
        .maindiv {
            /* Just to center the form on the page */
            margin: 0 auto;
            width: 400px;
            /* To see the outline of the form */
            padding: 1em;
            border: 1px solid #CCC;
            border-radius: 1em;
        }
         div + div {
                margin-top: 1em;
            }
        label {
            /* To make sure that all labels have the same size and are properly aligned */
            display: inline-block;
            width: 90px;
            text-align: right;
        }
        .button {
            /* To position the buttons to the same position of the text fields */
            padding-left: 90px; /* same size as the label elements */
        }

        button {
            /* This extra margin represent roughly the same space as the space
       between the labels and their text fields */
            margin-left: .5em;
        }
        input:focus, textarea:focus {
            /* To give a little highlight on active elements */
            border-color: #000;
        }
        input, textarea {
            /* To make sure that all text fields have the same font settings
       By default, textareas have a monospace font */
            font: 1em sans-serif;
            /* To give the same size to all text field */
            width: 300px;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            /* To harmonize the look & feel of text field border */
            border: 1px solid #999;
        }
    </style>
    <script type="text/javascript">

        function viewvalue()
        {
            var message = document.getElementById("msg").value;
            var key = document.getElementById("key").value;
            var encrypted = encryptByDES(message, key);
            document.getElementById("enctext").textContent = encrypted;
            document.getElementById("dectxt").textContent = decryptByDES(encrypted, key);;


        }

        function encryptByDES(message, key) {

            var keyHex = CryptoJS.enc.Utf8.parse(key);

            var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
                mode: CryptoJS.mode.ECB,
                padding: CryptoJS.pad.Pkcs7
            });
            return encrypted.toString();
        }

        function decryptByDES(ciphertext, key) {
            var keyHex = CryptoJS.enc.Utf8.parse(key);

            var decrypted = CryptoJS.DES.decrypt({
                ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
            }, keyHex, {
                mode: CryptoJS.mode.ECB,
                padding: CryptoJS.pad.Pkcs7
            });

            return decrypted.toString(CryptoJS.enc.Utf8);
        }
    </script>
</head>
<body>

    <div class="maindiv">
        <div>
            <label for="name">Message:</label>
            <input type="text" id="msg" name="msg" />
        </div>
        <div>
            <label for="mail">Key:</label>
            <input type="text" id="key" name="key" />
        </div>
        <div>
            <label for="msg">Encrypted Text:</label>
            <textarea id="enctext" name="enctxt"></textarea>
        </div>
        <div>
            <label for="msg">Decrypted Text:</label>
            <textarea id="dectxt" name="dectxt"></textarea>
        </div>
        <div class="button">
            <button onclick="viewvalue()">View</button>
        </div>
    </div>
</body>
</html>

This is my .js file

/*
CryptoJS v3.1.2
code.google.com/p/crypto-js
(c) 2009-2013 by Jeff Mott. All rights reserved.
code.google.com/p/crypto-js/wiki/License
*/
/**
 * Electronic Codebook block mode.
 */
CryptoJS.mode.ECB = (function () {
    var ECB = CryptoJS.lib.BlockCipherMode.extend();

    ECB.Encryptor = ECB.extend({
        processBlock: function (words, offset) {
            this._cipher.encryptBlock(words, offset);
        }
    });

    ECB.Decryptor = ECB.extend({
        processBlock: function (words, offset) {
            this._cipher.decryptBlock(words, offset);
        }
    });

    return ECB;
}());

Please anyone can tell me where and how to change the key.

解决方案

According to the docs at https://code.google.com/archive/p/crypto-js/#Custom_Key_and_IV, you would need to define and supply both the initialisation vector (IV) and the key if you wish to provide a custom key:

var key = CryptoJS.enc.Hex.parse('000102030405060708090a0b0c0d0e0f'); 
var iv = CryptoJS.enc.Hex.parse('101112131415161718191a1b1c1d1e1f'); 
var encrypted = CryptoJS.AES.encrypt("Message", key, { iv: iv });

这篇关于用CryptoJS更改密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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