Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

vue cryptojs

作者:◇∞柰旳ゝ僦湜沵   发布日期:2026-01-11   浏览:97

// 引入 CryptoJS 库
import CryptoJS from 'crypto-js';

// 示例 1: 使用 AES 加密和解密
function encryptWithAES(message, secretKey) {
    // 加密
    const encrypted = CryptoJS.AES.encrypt(message, secretKey).toString();
    return encrypted;
}

function decryptWithAES(encryptedMessage, secretKey) {
    // 解密
    const bytes = CryptoJS.AES.decrypt(encryptedMessage, secretKey);
    const decryptedData = bytes.toString(CryptoJS.enc.Utf8);
    return decryptedData;
}

// 示例 2: 使用 MD5 进行哈希
function hashWithMD5(message) {
    // 哈希
    const hash = CryptoJS.MD5(message).toString();
    return hash;
}

// 示例 3: 使用 HMAC SHA256 签名
function signWithHmacSHA256(message, secretKey) {
    // 签名
    const signature = CryptoJS.HmacSHA256(message, secretKey).toString();
    return signature;
}

// 测试代码
const secretKey = 'my-secret-key';
const message = 'Hello, CryptoJS!';

console.log('Original Message:', message);

const encryptedMessage = encryptWithAES(message, secretKey);
console.log('Encrypted Message:', encryptedMessage);

const decryptedMessage = decryptWithAES(encryptedMessage, secretKey);
console.log('Decrypted Message:', decryptedMessage);

const hashedMessage = hashWithMD5(message);
console.log('Hashed Message (MD5):', hashedMessage);

const signedMessage = signWithHmacSHA256(message, secretKey);
console.log('Signed Message (HMAC SHA256):', signedMessage);

解释说明:

  1. 引入 CryptoJS 库:首先需要安装并引入 crypto-js 库。可以通过 npm 安装 (npm install crypto-js) 或者直接在 HTML 文件中通过 <script> 标签引入。

  2. AES 加密和解密

    • encryptWithAES 函数使用 AES 算法对消息进行加密。
    • decryptWithAES 函数使用 AES 算法对加密后的消息进行解密。
  3. MD5 哈希

    • hashWithMD5 函数使用 MD5 算法对消息进行哈希处理,生成固定长度的哈希值。
  4. HMAC SHA256 签名

    • signWithHmacSHA256 函数使用 HMAC SHA256 算法对消息进行签名,生成一个基于消息和密钥的签名。
  5. 测试代码

    • 创建了一个秘密密钥和消息,并展示了如何使用上述函数进行加密、解密、哈希和签名操作。

上一篇:vue+element

下一篇:vue3 definestore

大家都在看

vue.js devtools用法

vue js for循环

highlight.js vue

vue.config.js 配置

vue.config.js 配置代理

vue.config.js configu

node.js vue

vue3 写法

vue3组件传值的方式

vue3 子路由

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站