如何在 JavaScript 中使用 CryptoJS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51005488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:43:51  来源:igfitidea点击:

how to use CryptoJS in javascript

javascriptnode.jscryptojs

提问by zhaowweny

I used CryptoJSinstead of nodejscrypto module because I just use the native JavaScript,but some codes can't work:

我使用CryptoJS而不是nodejscrypto 模块,因为我只是使用原生 JavaScript,但有些代码无法运行:

function aesEncrypt(text, secKey) {
  const _text = text
  const lv = new Buffer('0102030405060708', 'binary')
  const _secKey = new Buffer(secKey, 'binary')
  const cipher = crypto.createCipheriv('AES-128-CBC', _secKey, lv)
  let encrypted = cipher.update(_text, 'utf8', 'base64')
  encrypted += cipher.final('base64')
  return encrypted
}

So how should I modify these codes?

那么我应该如何修改这些代码呢?

回答by Melchia

Here's a sample on how to use CryptoJs in webclient:

以下是有关如何在 webclient 中使用 CryptoJs 的示例:

// INIT
var myString   = "blablabla Card game bla";
var myPassword = "myPassword";

// PROCESS
var encrypted = CryptoJS.AES.encrypt(myString, myPassword);
var decrypted = CryptoJS.AES.decrypt(encrypted, myPassword);
document.getElementById("demo0").innerHTML = myString;
document.getElementById("demo1").innerHTML = encrypted;
document.getElementById("demo2").innerHTML = decrypted;
document.getElementById("demo3").innerHTML = decrypted.toString(CryptoJS.enc.Utf8);
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
</head>


  
  <strong><label>Original String:</label></strong>
  <span id="demo0"></span>

  <br>
  <br>
  
  <strong><label>Encrypted:</label></strong>
  <span id="demo1"></span>

  <br>
  <br>
  
  <strong><label>Decrypted:</label></strong>
  <span id="demo2"></span>

  <br> 
  <br>

  <strong><label>String after Decryption:</label></strong>
  <span id="demo3"></span>

  
  <br />
  <br />
 

NB:

注意:

You might want to use CDN if you don't want to use node modules.

如果您不想使用节点模块,您可能想使用 CDN。

回答by Murtaza Hussain

How about CryptoJS?

CryptoJS 怎么样?

It's a solid crypto library, with a lot of functionality. It implements hashers, HMAC, PBKDF2 and ciphers. In this case ciphers is what you need. Check out the quick-start quide on the project's homepage.

这是一个可靠的加密库,具有很多功能。它实现了哈希器、HMAC、PBKDF2 和密码。在这种情况下,密码就是您所需要的。查看项目主页上的快速入门指南。

 var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
//U2FsdGVkX18ZUVvShFSES21qHsQEqZXMxQ9zgHy+bu0=

var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");
//4d657373616765


document.getElementById("demo1").innerHTML = encrypted;
document.getElementById("demo2").innerHTML = decrypted;
document.getElementById("demo3").innerHTML = decrypted.toString(CryptoJS.enc.Utf8);
Full working sample actually is:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>

<br><br>
<label>encrypted</label>
<div id="demo1"></div>
<br>

<label>decrypted</label>
<div id="demo2"></div>

<br>
<label>Actual Message</label>
<div id="demo3"></div>