记住密码 AES加密解密

记住密码

AES加密存储,解密获取

1、引入文件

import CryptoJS from 'crypto-js/crypto-js'; // 引入crypto-js/crypto-js 加密算法类库
import { setStore,getStore } from '@/util/store';

2、定义默认值

// 默认的 KEY 与 iv 如果没有给
const KEY = CryptoJS.enc.Utf8.parse('QwdsFGxcV18529Uq'); // 十六位十六进制数作为密钥
const IV = CryptoJS.enc.Utf8.parse('ASDFtgd1234567FG'); // 十六位十六进制数作为密钥偏移量

3、加密存储

//加密
Encrypt(word, keyStr, ivStr) {
  let key = KEY;
  let iv = IV;

  if (keyStr) {
    key = CryptoJS.enc.Utf8.parse(keyStr);
    iv = CryptoJS.enc.Utf8.parse(ivStr);
  }

  const srcs = CryptoJS.enc.Utf8.parse(word);
  const encrypted = CryptoJS.AES.encrypt(srcs, key, {
    iv, // 偏移量
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.ZeroPadding
  });
  return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
}

......
//如果勾选记住密码,则加密存到本地,否则清空本地存储
if (userInfo.checked) {
              localStorage.setItem(
                'psd',
                this.Encrypt(user.password)
              );
              localStorage.setItem(
                'name',
                this.Encrypt(user.username)
              );
              localStorage.setItem(
                'check',
                this.Encrypt(user.checked)
              );
            } else {
              localStorage.removeItem('psd');
              localStorage.removeItem('name');
              localStorage.removeItem('check');
            }

4、解密获取

    //解密
    Decrypt(word, keyStr, ivStr) {
      let key = KEY;
      let iv = IV;

      if (keyStr) {
        key = CryptoJS.enc.Utf8.parse(keyStr);
        iv = CryptoJS.enc.Utf8.parse(ivStr);
      }

      const base64 = CryptoJS.enc.Base64.parse(word);
      const src = CryptoJS.enc.Base64.stringify(base64);

      const decrypt = CryptoJS.AES.decrypt(src, key, {
        iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
      });

      const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
      return decryptedStr.toString();
    }
    
.......
    const psd = this.Decrypt(localStorage.getItem('psd'));
    const name = this.Decrypt(localStorage.getItem('name'));
    const checked = this.Decrypt(localStorage.getItem('check'));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt是一种流行的跨平台应用程序开发框架,提供了各种功能丰富的类库和工具,包括AES加密解密算法的支持。 AES(Advanced Encryption Standard)是一种对称加密算法,被广泛应用于保护数据的机密性和安全性。在Qt中,我们可以使用QCryptographic类库来实现AES加密解密操作。 首先,我们需要在项目中包含QCryptographic库的头文件: #include <QCryptographicHash> 然后,使用该库中的相关方法来进行加密解密操作。例如,使用AES-128加密算法: QString plaintext = "Hello, World!"; QString password = "SecretPassword"; // 将明文转换为字节数组 QByteArray plaintextBytes = plaintext.toUtf8(); // 创建AES加密对象 QAESEncryption aesEncrypt(QAESEncryption::AES_128, QAESEncryption::CBC); // 设置加密密码 aesEncrypt.setKey(password.toUtf8()); // 加密明文数据 QByteArray encryptedData = aesEncrypt.encode(plaintextBytes); // 将加密数据转换为十六进制字符串 QString encryptedText = encryptedData.toHex(); // 输出加密后的结果 qDebug() << "Encrypted text: " << encryptedText; 上述代码将明文字符串"Hello, World!"使用AES-128算法加密加密密码为"SecretPassword",然后将加密后的数据转换为十六进制字符串并输出。 解密操作可以使用相同的加密密码加密算法: // 将加密后的字符串转换为字节数组 QByteArray encryptedData = QByteArray::fromHex(encryptedText.toUtf8()); // 创建AES解密对象 QAESEncryption aesDecrypt(QAESEncryption::AES_128, QAESEncryption::CBC); // 设置解密密码 aesDecrypt.setKey(password.toUtf8()); // 解密数据 QByteArray decryptedData = aesDecrypt.decode(encryptedData); // 将解密后的字节数组转换为明文字符串 QString decryptedText = QString::fromUtf8(decryptedData); // 输出解密结果 qDebug() << "Decrypted text: " << decryptedText; 上述代码将加密后的十六进制字符串转换为字节数组,然后使用AES-128算法和密码"SecretPassword"进行解密操作,最后将解密后的字节数组转换为明文字符串并输出。 通过以上代码片段,我们可以在Qt中使用AES算法进行加密解密操作,保护我们的数据的机密性和安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值