前端 使用 crypto-js 對資料進行對稱式加密

來源:互聯網
上載者:User

標籤:div   key   var   bsp   返回   解密   value   前端   logs   

傳送門:

# crypto-js githubhttps://github.com/brix/crypto-js

 

demo1:

// 載入核心加密庫var CryptoJS = require("crypto-js");// 載入des演算法var tripledes = require("crypto-js/tripledes");// 開始加密,並且返回密文var ciphertext  = tripledes.encrypt("fuckyou", ‘123‘).toString();// 解密var plaintext  = tripledes.decrypt(ciphertext, ‘123‘).toString(CryptoJS.enc.Utf8)// 輸出密文和解密後的內容console.log(ciphertext, plaintext)

 

demo2:

// 載入核心加密庫var CryptoJS = require("crypto-js");function encrypt (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 {        key: keyHex,        value: encrypted.toString()    }}function decrypt (message, key) {    var plaintext = CryptoJS.DES.decrypt(message, key, {        mode: CryptoJS.mode.ECB,        padding: CryptoJS.pad.Pkcs7    })    return plaintext.toString(CryptoJS.enc.Utf8)}var a = encrypt(‘mssage123‘, ‘123‘);var b = decrypt(a.value, a.key);console.log(a.value.length)

 

前端 使用 crypto-js 對資料進行對稱式加密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.