crypto-js 加密、解密使用方法

一、安装crypto-js

npm install crypto-js

二、引入crypto-js

支持ES6导入、Modular

import CryptoJS from "crypto-js";

或者

const CryptoJS = require("crypto-js");

三、设置密钥和密钥偏移量

// 十六位十六进制数作为密钥
const SECRET_KEY = CryptoJS.enc.Utf8.parse("1234123412341234");
// 十六位十六进制数作为密钥偏移量
const SECRET_IV = CryptoJS.enc.Utf8.parse("1234123412341234");

四、封装加密方法

/**
 * 加密方法
 * @param data
 * @returns {string}
 */
export function encrypt(data) {
  if (typeof data === "object") {
    try {
      // eslint-disable-next-line no-param-reassign
      data = JSON.stringify(data);
    } catch (error) {
      console.log("encrypt error:", error);
    }
  }
  const dataHex = CryptoJS.enc.Utf8.parse(data);
  const encrypted = CryptoJS.AES.encrypt(dataHex, SECRET_KEY, {
    iv: SECRET_IV,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  return encrypted.ciphertext.toString();
}

五、封装解密方法

/**
 * 解密方法
 * @param data
 * @returns {string}
 */
export function decrypt(data) {
  const encryptedHexStr = CryptoJS.enc.Hex.parse(data);
  const str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
  const decrypt = CryptoJS.AES.decrypt(str, SECRET_KEY, {
    iv: SECRET_IV,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
  return decryptedStr.toString();
}

六、使用方法

import { decrypt, encrypt } from "@/utils/encrypt";

const data = "13172"

const encryptText = encrypt(data);
console.log("加密", encryptText);

const decryptText = decrypt(encryptText);
console.log("解密", decryptText);

### 倒计时机制的实现 在 Unity 中创建倒计时机制可以通过多种方式完成,其中一种常见的方式是利用 `Update` 方法不断减少时间变量直到达到零。下面是一个基于引用中的 `OneShotTimer` 类扩展而来的倒计时实现方案。 #### 使用自定义定时器类 可以继承并修改 `OneShotTimer` 来适应更复杂的场景需求: ```csharp using UnityEngine; public class CountdownTimer : MonoBehaviour { private float totalTime; private float currentTime; private bool isRunning = false; public void StartCountdown(float duration, System.Action onCompletion) { if (!isRunning) { totalTime = duration; currentTime = totalTime; isRunning = true; StartCoroutine(UpdateTime(onCompletion)); } } private System.Collections.IEnumerator UpdateTime(System.Action onCompletion) { while (currentTime > 0 && isRunning) { yield return null; // Wait for the next frame. currentTime -= Time.deltaTime; Debug.Log($"Remaining time: {currentTime:F2} seconds."); // Log remaining time. if (currentTime <= 0 && isRunning) { isRunning = false; onCompletion?.Invoke(); } } } public void Pause() { isRunning = false; } public void Resume() { if (!isRunning) { isRunning = true; StartCoroutine(UpdateTime(null)); // Continue without a new completion action unless specified. } } public void Reset(float? newDuration = null) { isRunning = false; if (newDuration.HasValue) { totalTime = newDuration.Value; } currentTime = totalTime; } } ``` 此代码片段展示了如何通过协程控制倒计时逻辑[^1]。每次调用 `StartCountdown` 函数时,都会启动一个新的倒计时过程,并允许指定回调函数,在倒计时结束时触发特定操作。 #### 结合 OnMouseDown 实现交互 如果希望玩家能够通过点击按钮或其他对象来启动或暂停倒计时,则可结合 `OnMouseDown()` 方法实现这一功能[^2]: ```csharp public class TimerController : MonoBehaviour { public CountdownTimer countdownTimer; private void OnMouseDown() { if (countdownTimer != null) { if (!countdownTimer.isRunning) { countdownTimer.StartCountdown(10f, () => Debug.Log("Countdown finished!")); // Example of starting a 10-second timer. } else { countdownTimer.Pause(); // If already running, pause it instead. } } } } ``` 上述脚本会在检测到鼠标按下事件时检查当前状态——如果是停止状态则重新开始;如果是运行中则将其暂停。 #### 处理碰撞与触发事件 除了简单的点击响应外,还可以利用 Unity 提供的各种物理事件(如 `OnCollisionEnter`, `OnTriggerEnter` 等)作为条件激活倒计时[^3] : ```csharp private void OnTriggerEnter(Collider other) { if(other.CompareTag("Player")) { countdownTimer?.StartCountdown(5f, HandleEvent); } } void HandleEvent() { Debug.Log("An event has been triggered after countdown."); } ``` 这里假设有一个标记为 `"Player"` 的实体进入某个区域范围后自动开启一个短暂的倒数周期。 --- ###
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱宇阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值