You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.3 KiB
34 lines
1.3 KiB
import CryptoJS from 'crypto-js'; |
|
// 使用密钥长度128的RIJNDAEL算法解码数据,加密模式:ECB |
|
|
|
const key = 'hbsaas1mobile2a6f8'; |
|
|
|
// 解密函数 |
|
export function decryptData(encryptedData) { |
|
// Step 1: 替换 base64 字符中的 "+" 和 "/" 为 URL 安全的字符 |
|
encryptedData = encryptedData.replace(/-/g, '+').replace(/_/g, '/'); |
|
|
|
// Step 2: 解码 base64 数据 |
|
const decodedData = CryptoJS.enc.Base64.parse(encryptedData); |
|
|
|
// Step 3: 填充 key,确保它的长度为 24 字节(128 位) |
|
let paddedKey = key; |
|
const keyLength = paddedKey.length; |
|
if (keyLength < 24) { |
|
paddedKey = paddedKey.padEnd(24, '\0'); // 用零填充至 16 字节 |
|
} |
|
|
|
// Step 4: 使用 AES ECB 模式解密 |
|
const decryptedData = CryptoJS.AES.decrypt({ ciphertext: decodedData }, CryptoJS.enc.Utf8.parse(paddedKey), { |
|
mode: CryptoJS.mode.ECB, |
|
padding: CryptoJS.pad.Pkcs7, // 与 PHP 中的 PKCS5 padding 对应 |
|
}); |
|
|
|
// Step 5: 转换解密结果为明文字符串 |
|
return decryptedData.toString(CryptoJS.enc.Utf8); |
|
} |
|
|
|
// 示例使用: |
|
// const encryptedData = "6QUbssj247RF6oIMf9nqT6F5LoYpAkFENy5wc572gws"; // 从 PHP 获取的加密数据 |
|
// const decrypted = decryptData(encryptedData, key); |
|
// console.log(decrypted); // 输出解密后的数据
|
|
|