Files
MoonTVPlus/src/lib/crypto.ts

59 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-08-15 13:49:08 +08:00
import CryptoJS from 'crypto-js';
/**
*
* 使 AES
*/
export class SimpleCrypto {
/**
*
* @param data
* @param password
* @returns
*/
static encrypt(data: string, password: string): string {
try {
const encrypted = CryptoJS.AES.encrypt(data, password).toString();
return encrypted;
} catch (error) {
throw new Error('加密失败');
}
}
/**
*
* @param encryptedData
* @param password
* @returns
*/
static decrypt(encryptedData: string, password: string): string {
try {
const bytes = CryptoJS.AES.decrypt(encryptedData, password);
const decrypted = bytes.toString(CryptoJS.enc.Utf8);
if (!decrypted) {
throw new Error('解密失败,请检查密码是否正确');
}
return decrypted;
} catch (error) {
throw new Error('解密失败,请检查密码是否正确');
}
}
/**
*
* @param encryptedData
* @param password
* @returns
*/
static canDecrypt(encryptedData: string, password: string): boolean {
try {
const decrypted = this.decrypt(encryptedData, password);
return decrypted.length > 0;
} catch {
return false;
}
}
}