Skip to content

加密工具

本文档介绍加密工具的使用方法。

EncryptUtils

加密工具类,支持 MD5、AES、SHA256、Base64 等加密方式。

MD5 加密

typescript
import { EncryptUtils } from '@vben/utils';

// MD5 加密
const hash = EncryptUtils.md5('password123');
console.log(hash); // 482c811da5d5b4bc6d497ffa98491e38

AES 加密/解密

typescript
import { EncryptUtils } from '@vben/utils';

const secretKey = 'my-secret-key-123';

// 加密
const encrypted = EncryptUtils.aesEncrypt('敏感数据', secretKey);
console.log(encrypted); // U2FsdGVkX1...

// 解密
const decrypted = EncryptUtils.aesDecrypt(encrypted, secretKey);
console.log(decrypted); // 敏感数据

SHA256 加密

typescript
import { EncryptUtils } from '@vben/utils';

const hash = EncryptUtils.sha256('password123');
console.log(hash);

Base64 编码/解码

typescript
import { EncryptUtils } from '@vben/utils';

// 编码
const encoded = EncryptUtils.base64Encode('Hello World');
console.log(encoded); // SGVsbG8gV29ybGQ=

// 解码
const decoded = EncryptUtils.base64Decode(encoded);
console.log(decoded); // Hello World

使用 crypto-js

typescript
import CryptoJS from 'crypto-js';

// MD5
const md5Hash = CryptoJS.MD5('message').toString();

// SHA256
const sha256Hash = CryptoJS.SHA256('message').toString();

// AES 加密
const encrypted = CryptoJS.AES.encrypt('message', 'secret-key').toString();

// AES 解密
const decrypted = CryptoJS.AES.decrypt(encrypted, 'secret-key');
const originalText = decrypted.toString(CryptoJS.enc.Utf8);

// Base64
const base64 = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse('message'));

完整示例

vue
<script setup lang="ts">
import { ref } from 'vue';
import { EncryptUtils } from '@vben/utils';
import { message } from 'ant-design-vue';

const inputText = ref('');
const secretKey = ref('my-secret-key');
const encryptedText = ref('');
const decryptedText = ref('');

function handleMD5() {
  const hash = EncryptUtils.md5(inputText.value);
  message.success('MD5: ' + hash);
}

function handleAESEncrypt() {
  try {
    encryptedText.value = EncryptUtils.aesEncrypt(inputText.value, secretKey.value);
    message.success('加密成功');
  } catch (error) {
    message.error('加密失败');
  }
}

function handleAESDecrypt() {
  try {
    decryptedText.value = EncryptUtils.aesDecrypt(encryptedText.value, secretKey.value);
    message.success('解密成功');
  } catch (error) {
    message.error('解密失败');
  }
}

function handleBase64Encode() {
  const encoded = EncryptUtils.base64Encode(inputText.value);
  message.success('Base64: ' + encoded);
}
</script>

<template>
  <div class="encrypt-demo">
    <a-space direction="vertical" style="width: 100%">
      <a-input
        v-model:value="inputText"
        placeholder="输入要加密的内容"
      />
      
      <a-input
        v-model:value="secretKey"
        placeholder="密钥"
      />
      
      <a-space>
        <a-button type="primary" @click="handleMD5">
          MD5 加密
        </a-button>
        <a-button @click="handleAESEncrypt">
          AES 加密
        </a-button>
        <a-button @click="handleAESDecrypt">
          AES 解密
        </a-button>
        <a-button @click="handleBase64Encode">
          Base64 编码
        </a-button>
      </a-space>
      
      <a-textarea
        v-model:value="encryptedText"
        placeholder="加密结果"
        :rows="3"
        readonly
      />
      
      <a-textarea
        v-model:value="decryptedText"
        placeholder="解密结果"
        :rows="3"
        readonly
      />
    </a-space>
  </div>
</template>

密码加密最佳实践

typescript
import CryptoJS from 'crypto-js';

// 生成随机盐值
function generateSalt(length = 16) {
  return CryptoJS.lib.WordArray.random(length).toString();
}

// 加密密码(使用盐值)
function hashPassword(password: string, salt?: string) {
  if (!salt) {
    salt = generateSalt();
  }
  const hash = CryptoJS.SHA256(password + salt).toString();
  return { hash, salt };
}

// 验证密码
function verifyPassword(password: string, hash: string, salt: string) {
  const { hash: newHash } = hashPassword(password, salt);
  return newHash === hash;
}

// 使用示例
const password = 'user-password-123';

// 注册时
const { hash, salt } = hashPassword(password);
// 保存 hash 和 salt 到数据库

// 登录时
const isValid = verifyPassword(password, hash, salt);

API 参考

md5

MD5 加密。

typescript
EncryptUtils.md5(text: string): string

aesEncrypt

AES 加密。

typescript
EncryptUtils.aesEncrypt(text: string, key: string): string

aesDecrypt

AES 解密。

typescript
EncryptUtils.aesDecrypt(encrypted: string, key: string): string

sha256

SHA256 加密。

typescript
EncryptUtils.sha256(text: string): string

base64Encode

Base64 编码。

typescript
EncryptUtils.base64Encode(text: string): string

base64Decode

Base64 解码。

typescript
EncryptUtils.base64Decode(encoded: string): string

安全建议

  1. 不要在前端存储敏感密钥
  2. 使用 HTTPS 传输加密数据
  3. 密码加密使用盐值
  4. 定期更换密钥
  5. 使用强密码策略

参考资源

MIT License