javascript - Change the Key with CryptoJS -
this question has answer here:
i using cryptojs encrypt , decrypt text. here, taking message , showing both encryption , decryption messages.
i using des algorithm encrypting , decrypting.
this html file
<!doctype html> <html> <head> <script src="tripledes.js"></script> <script src="mode-ecb.js"></script> <style type="text/css"> .maindiv { /* center form on page */ margin: 0 auto; width: 400px; /* see outline of form */ padding: 1em; border: 1px solid #ccc; border-radius: 1em; } div + div { margin-top: 1em; } label { /* make sure labels have same size , aligned */ display: inline-block; width: 90px; text-align: right; } .button { /* position buttons same position of text fields */ padding-left: 90px; /* same size label elements */ } button { /* margin represent same space space between labels , text fields */ margin-left: .5em; } input:focus, textarea:focus { /* give little highlight on active elements */ border-color: #000; } input, textarea { /* make sure text fields have same font settings default, textareas have monospace font */ font: 1em sans-serif; /* give same size text field */ width: 300px; -moz-box-sizing: border-box; box-sizing: border-box; /* harmonize & 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 .js file
/* cryptojs v3.1.2 code.google.com/p/crypto-js (c) 2009-2013 jeff mott. 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 can tell me , how change key.
according docs @ https://code.google.com/archive/p/crypto-js/#custom_key_and_iv, need define , supply both initialisation vector (iv) , key if wish provide 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 });
Comments
Post a Comment