2016-06-10 1 views
0

나는 CryptoJS를 사용하여 텍스트를 암호화하고 해독합니다. 여기에서는 메시지를 가져 와서 암호화 및 암호 해독 메시지를 보여줍니다.CryptoJS로 키 변경

저는 암호화 및 암호 해독에 DES 알고리즘을 사용하고 있습니다.

이 내 HTML 파일

<!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> 

인이 내의 .js가

/* 
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; 
}()); 

이 사람이 어디서 어떻게 키를 변경하는 말해 줄 수주십시오 파일입니다.

+0

당신이 무슨 뜻인지 설명 할 수는? '''''var key = document.getElementById ("key"). value;'''라고하는''''viewvalue()''함수에 한 줄이 있습니다. #key 입력 필드에 쓰거나, 단지''''encryptByDES()'''를 당신의 메시지와 키로 직접 호출함으로써 가능합니다. –

+0

암호화 및 암호 해독에 다른 키가있는 경우 사용합니다 ... 키를 사용하지 않을 경우 다른 키를 사용합니다. 기본 키를 사용합니다. – shv22

+0

@ShubhamVashishtha 여기에서 묻는 것을 이해하지 못합니다. 당신의 열쇠는 무엇이며 왜 그것을 바꾸고 싶습니까? 마주 치게되는 문제는 무엇입니까? 키를 보유해야하는 입력 필드가 있습니다. –

답변

1

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 }); 
+0

var key = CryptoJS.enc.Hex.parse ('000102030405060708090a0b0c0d0e0f'); 여기 (000102030405060708090a0b0c0d0e0f ')가 바로 열쇠입니까? 대신에 광산을 사용해야합니다. – shv22

+0

위와 같이 16 진수 문자열 대신 일반 단어를 사용하려면 다음과 같이하십시오 : 'var salt = CryptoJS.lib.WordArray.random (128/8); var key128Bits = CryptoJS.PBKDF2 ("Secret Passphrase", 소금, {keySize : 128/32}); var key256Bits = CryptoJS.PBKDF2 ("Secret Passphrase", 소금, {keySize : 256/32}); var key512Bits = CryptoJS.PBKDF2 ("Secret Passphrase", 소금, {keySize : 512/32}); var key512Bits1000Iterations = CryptoJS.PBKDF2 (비밀 암호문, 소금, {keySize : 512/32, iterations : 1000}), ' https://code.google.com/archive/p/crypto-js/를 참조하십시오. 참조 용 # PBKDF2 – Monty

+0

죄송합니다. 제대로 이해하지 못했습니다 ...여기 "Secret Passphrase"는 비밀 키 텍스트를 쓰는 곳입니다. 맞습니까? – shv22